github.com/prebid/prebid-server@v0.275.0/metrics/metrics.go (about)

     1  package metrics
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/prebid/prebid-server/openrtb_ext"
     7  )
     8  
     9  // Labels defines the labels that can be attached to the metrics.
    10  type Labels struct {
    11  	Source        DemandSource
    12  	RType         RequestType
    13  	PubID         string // exchange specific ID, so we cannot compile in values
    14  	CookieFlag    CookieFlag
    15  	RequestStatus RequestStatus
    16  }
    17  
    18  // AdapterLabels defines the labels that can be attached to the adapter metrics.
    19  type AdapterLabels struct {
    20  	Source        DemandSource
    21  	RType         RequestType
    22  	Adapter       openrtb_ext.BidderName
    23  	PubID         string // exchange specific ID, so we cannot compile in values
    24  	CookieFlag    CookieFlag
    25  	AdapterBids   AdapterBid
    26  	AdapterErrors map[AdapterError]struct{}
    27  }
    28  
    29  // OverheadType: overhead type enumeration
    30  type OverheadType string
    31  
    32  const (
    33  	// PreBidder - measures the time needed to execute the adapter's MakeRequests() implementation, build Prebid headers and apply GZip compression if needed
    34  	PreBidder OverheadType = "pre-bidder"
    35  	// MakeAuctionResponse - measures the amount of time spent doing all the MakeBids() calls as well as preparing PBS's response
    36  	MakeAuctionResponse OverheadType = "make-auction-response"
    37  	// MakeBidderRequests - measures the time needed to fetch a stored request (if needed), parse, unmarshal, and validate the OpenRTB request, interpret its privacy policies, and split it into multiple requests sanitized for each bidder
    38  	MakeBidderRequests OverheadType = "make-bidder-requests"
    39  )
    40  
    41  func (t OverheadType) String() string {
    42  	return string(t)
    43  }
    44  
    45  func OverheadTypes() []OverheadType {
    46  	return []OverheadType{PreBidder, MakeAuctionResponse, MakeBidderRequests}
    47  }
    48  
    49  // ImpLabels defines metric labels describing the impression type.
    50  type ImpLabels struct {
    51  	BannerImps bool
    52  	VideoImps  bool
    53  	AudioImps  bool
    54  	NativeImps bool
    55  }
    56  
    57  // RequestLabels defines metric labels describing the result of a network request.
    58  type RequestLabels struct {
    59  	RequestStatus RequestStatus
    60  }
    61  
    62  // PrivacyLabels defines metrics describing the result of privacy enforcement.
    63  type PrivacyLabels struct {
    64  	CCPAEnforced   bool
    65  	CCPAProvided   bool
    66  	COPPAEnforced  bool
    67  	GDPREnforced   bool
    68  	GDPRTCFVersion TCFVersionValue
    69  	LMTEnforced    bool
    70  }
    71  
    72  type ModuleLabels struct {
    73  	Module    string
    74  	Stage     string
    75  	AccountID string
    76  }
    77  
    78  type StoredDataType string
    79  
    80  const (
    81  	AccountDataType  StoredDataType = "account"
    82  	AMPDataType      StoredDataType = "amp"
    83  	CategoryDataType StoredDataType = "category"
    84  	RequestDataType  StoredDataType = "request"
    85  	VideoDataType    StoredDataType = "video"
    86  	ResponseDataType StoredDataType = "response"
    87  )
    88  
    89  func StoredDataTypes() []StoredDataType {
    90  	return []StoredDataType{
    91  		AccountDataType,
    92  		AMPDataType,
    93  		CategoryDataType,
    94  		RequestDataType,
    95  		VideoDataType,
    96  		ResponseDataType,
    97  	}
    98  }
    99  
   100  type StoredDataFetchType string
   101  
   102  const (
   103  	FetchAll   StoredDataFetchType = "all"
   104  	FetchDelta StoredDataFetchType = "delta"
   105  )
   106  
   107  func StoredDataFetchTypes() []StoredDataFetchType {
   108  	return []StoredDataFetchType{
   109  		FetchAll,
   110  		FetchDelta,
   111  	}
   112  }
   113  
   114  type StoredDataLabels struct {
   115  	DataType      StoredDataType
   116  	DataFetchType StoredDataFetchType
   117  	Error         StoredDataError
   118  }
   119  
   120  type StoredDataError string
   121  
   122  const (
   123  	StoredDataErrorNetwork   StoredDataError = "network"
   124  	StoredDataErrorUndefined StoredDataError = "undefined"
   125  )
   126  
   127  func StoredDataErrors() []StoredDataError {
   128  	return []StoredDataError{
   129  		StoredDataErrorNetwork,
   130  		StoredDataErrorUndefined,
   131  	}
   132  }
   133  
   134  // Label typecasting. Se below the type definitions for possible values
   135  
   136  // DemandSource : Demand source enumeration
   137  type DemandSource string
   138  
   139  // ImpMediaType : Media type described in the "imp" JSON object  TODO is this still needed?
   140  type ImpMediaType string
   141  
   142  // RequestType : Request type enumeration
   143  type RequestType string
   144  
   145  // CookieFlag : User ID cookie exists flag
   146  type CookieFlag string
   147  
   148  // RequestStatus : The request return status
   149  type RequestStatus string
   150  
   151  // AdapterBid : Whether or not the adapter returned bids
   152  type AdapterBid string
   153  
   154  // AdapterError : Errors which may have occurred during the adapter's execution
   155  type AdapterError string
   156  
   157  // CacheResult : Cache hit/miss
   158  type CacheResult string
   159  
   160  // PublisherUnknown : Default value for Labels.PubID
   161  const PublisherUnknown = "unknown"
   162  
   163  // The demand sources
   164  const (
   165  	DemandWeb     DemandSource = "web"
   166  	DemandApp     DemandSource = "app"
   167  	DemandUnknown DemandSource = "unknown"
   168  )
   169  
   170  func DemandTypes() []DemandSource {
   171  	return []DemandSource{
   172  		DemandWeb,
   173  		DemandApp,
   174  		DemandUnknown,
   175  	}
   176  }
   177  
   178  // The request types (endpoints)
   179  const (
   180  	ReqTypeORTB2Web RequestType = "openrtb2-web"
   181  	ReqTypeORTB2App RequestType = "openrtb2-app"
   182  	ReqTypeAMP      RequestType = "amp"
   183  	ReqTypeVideo    RequestType = "video"
   184  )
   185  
   186  func RequestTypes() []RequestType {
   187  	return []RequestType{
   188  		ReqTypeORTB2Web,
   189  		ReqTypeORTB2App,
   190  		ReqTypeAMP,
   191  		ReqTypeVideo,
   192  	}
   193  }
   194  
   195  // The media types described in the "imp" json objects
   196  const (
   197  	ImpTypeBanner ImpMediaType = "banner"
   198  	ImpTypeVideo  ImpMediaType = "video"
   199  	ImpTypeAudio  ImpMediaType = "audio"
   200  	ImpTypeNative ImpMediaType = "native"
   201  )
   202  
   203  func ImpTypes() []ImpMediaType {
   204  	return []ImpMediaType{
   205  		ImpTypeBanner,
   206  		ImpTypeVideo,
   207  		ImpTypeAudio,
   208  		ImpTypeNative,
   209  	}
   210  }
   211  
   212  // Cookie flag
   213  const (
   214  	CookieFlagYes     CookieFlag = "exists"
   215  	CookieFlagNo      CookieFlag = "no"
   216  	CookieFlagUnknown CookieFlag = "unknown"
   217  )
   218  
   219  func CookieTypes() []CookieFlag {
   220  	return []CookieFlag{
   221  		CookieFlagYes,
   222  		CookieFlagNo,
   223  		CookieFlagUnknown,
   224  	}
   225  }
   226  
   227  // Request/return status
   228  const (
   229  	RequestStatusOK               RequestStatus = "ok"
   230  	RequestStatusBadInput         RequestStatus = "badinput"
   231  	RequestStatusErr              RequestStatus = "err"
   232  	RequestStatusNetworkErr       RequestStatus = "networkerr"
   233  	RequestStatusBlacklisted      RequestStatus = "blacklistedacctorapp"
   234  	RequestStatusQueueTimeout     RequestStatus = "queuetimeout"
   235  	RequestStatusAccountConfigErr RequestStatus = "acctconfigerr"
   236  )
   237  
   238  func RequestStatuses() []RequestStatus {
   239  	return []RequestStatus{
   240  		RequestStatusOK,
   241  		RequestStatusBadInput,
   242  		RequestStatusErr,
   243  		RequestStatusNetworkErr,
   244  		RequestStatusBlacklisted,
   245  		RequestStatusQueueTimeout,
   246  		RequestStatusAccountConfigErr,
   247  	}
   248  }
   249  
   250  // Adapter bid response status.
   251  const (
   252  	AdapterBidPresent AdapterBid = "bid"
   253  	AdapterBidNone    AdapterBid = "nobid"
   254  )
   255  
   256  func AdapterBids() []AdapterBid {
   257  	return []AdapterBid{
   258  		AdapterBidPresent,
   259  		AdapterBidNone,
   260  	}
   261  }
   262  
   263  // Adapter execution status
   264  const (
   265  	AdapterErrorBadInput            AdapterError = "badinput"
   266  	AdapterErrorBadServerResponse   AdapterError = "badserverresponse"
   267  	AdapterErrorTimeout             AdapterError = "timeout"
   268  	AdapterErrorFailedToRequestBids AdapterError = "failedtorequestbid"
   269  	AdapterErrorValidation          AdapterError = "validation"
   270  	AdapterErrorUnknown             AdapterError = "unknown_error"
   271  )
   272  
   273  func AdapterErrors() []AdapterError {
   274  	return []AdapterError{
   275  		AdapterErrorBadInput,
   276  		AdapterErrorBadServerResponse,
   277  		AdapterErrorTimeout,
   278  		AdapterErrorFailedToRequestBids,
   279  		AdapterErrorValidation,
   280  		AdapterErrorUnknown,
   281  	}
   282  }
   283  
   284  const (
   285  	// CacheHit represents a cache hit i.e the key was found in cache
   286  	CacheHit CacheResult = "hit"
   287  	// CacheMiss represents a cache miss i.e that key wasn't found in cache
   288  	// and had to be fetched from the backend
   289  	CacheMiss CacheResult = "miss"
   290  )
   291  
   292  // CacheResults returns possible cache results i.e. cache hit or miss
   293  func CacheResults() []CacheResult {
   294  	return []CacheResult{
   295  		CacheHit,
   296  		CacheMiss,
   297  	}
   298  }
   299  
   300  // TCFVersionValue : The possible values for TCF versions
   301  type TCFVersionValue string
   302  
   303  const (
   304  	TCFVersionErr TCFVersionValue = "err"
   305  	TCFVersionV2  TCFVersionValue = "v2"
   306  )
   307  
   308  // TCFVersions returns the possible values for the TCF version
   309  func TCFVersions() []TCFVersionValue {
   310  	return []TCFVersionValue{
   311  		TCFVersionErr,
   312  		TCFVersionV2,
   313  	}
   314  }
   315  
   316  // TCFVersionToValue takes an integer TCF version and returns the corresponding TCFVersionValue
   317  func TCFVersionToValue(version int) TCFVersionValue {
   318  	switch {
   319  	case version == 2:
   320  		return TCFVersionV2
   321  	}
   322  	return TCFVersionErr
   323  }
   324  
   325  // CookieSyncStatus is a status code resulting from a call to the /cookie_sync endpoint.
   326  type CookieSyncStatus string
   327  
   328  const (
   329  	CookieSyncOK                     CookieSyncStatus = "ok"
   330  	CookieSyncBadRequest             CookieSyncStatus = "bad_request"
   331  	CookieSyncOptOut                 CookieSyncStatus = "opt_out"
   332  	CookieSyncGDPRHostCookieBlocked  CookieSyncStatus = "gdpr_blocked_host_cookie"
   333  	CookieSyncAccountBlocked         CookieSyncStatus = "acct_blocked"
   334  	CookieSyncAccountConfigMalformed CookieSyncStatus = "acct_config_malformed"
   335  	CookieSyncAccountInvalid         CookieSyncStatus = "acct_invalid"
   336  )
   337  
   338  // CookieSyncStatuses returns possible cookie sync statuses.
   339  func CookieSyncStatuses() []CookieSyncStatus {
   340  	return []CookieSyncStatus{
   341  		CookieSyncOK,
   342  		CookieSyncBadRequest,
   343  		CookieSyncOptOut,
   344  		CookieSyncGDPRHostCookieBlocked,
   345  		CookieSyncAccountBlocked,
   346  		CookieSyncAccountConfigMalformed,
   347  		CookieSyncAccountInvalid,
   348  	}
   349  }
   350  
   351  // SyncerCookieSyncStatus is a status code from an invocation of a syncer resulting from a call to the /cookie_sync endpoint.
   352  type SyncerCookieSyncStatus string
   353  
   354  const (
   355  	SyncerCookieSyncOK               SyncerCookieSyncStatus = "ok"
   356  	SyncerCookieSyncPrivacyBlocked   SyncerCookieSyncStatus = "privacy_blocked"
   357  	SyncerCookieSyncAlreadySynced    SyncerCookieSyncStatus = "already_synced"
   358  	SyncerCookieSyncTypeNotSupported SyncerCookieSyncStatus = "type_not_supported"
   359  )
   360  
   361  // SyncerRequestStatuses returns possible syncer statuses.
   362  func SyncerRequestStatuses() []SyncerCookieSyncStatus {
   363  	return []SyncerCookieSyncStatus{
   364  		SyncerCookieSyncOK,
   365  		SyncerCookieSyncPrivacyBlocked,
   366  		SyncerCookieSyncAlreadySynced,
   367  		SyncerCookieSyncTypeNotSupported,
   368  	}
   369  }
   370  
   371  // SetUidStatus is a status code resulting from a call to the /setuid endpoint.
   372  type SetUidStatus string
   373  
   374  // /setuid action labels
   375  const (
   376  	SetUidOK                     SetUidStatus = "ok"
   377  	SetUidBadRequest             SetUidStatus = "bad_request"
   378  	SetUidOptOut                 SetUidStatus = "opt_out"
   379  	SetUidGDPRHostCookieBlocked  SetUidStatus = "gdpr_blocked_host_cookie"
   380  	SetUidAccountBlocked         SetUidStatus = "acct_blocked"
   381  	SetUidAccountConfigMalformed SetUidStatus = "acct_config_malformed"
   382  	SetUidAccountInvalid         SetUidStatus = "acct_invalid"
   383  	SetUidSyncerUnknown          SetUidStatus = "syncer_unknown"
   384  )
   385  
   386  // SetUidStatuses returns possible setuid statuses.
   387  func SetUidStatuses() []SetUidStatus {
   388  	return []SetUidStatus{
   389  		SetUidOK,
   390  		SetUidBadRequest,
   391  		SetUidOptOut,
   392  		SetUidGDPRHostCookieBlocked,
   393  		SetUidAccountBlocked,
   394  		SetUidAccountConfigMalformed,
   395  		SetUidAccountInvalid,
   396  		SetUidSyncerUnknown,
   397  	}
   398  }
   399  
   400  // SyncerSetUidStatus is a status code from an invocation of a syncer resulting from a call to the /setuid endpoint.
   401  type SyncerSetUidStatus string
   402  
   403  const (
   404  	SyncerSetUidOK      SyncerSetUidStatus = "ok"
   405  	SyncerSetUidCleared SyncerSetUidStatus = "cleared"
   406  )
   407  
   408  // SyncerSetUidStatuses returns possible syncer set statuses.
   409  func SyncerSetUidStatuses() []SyncerSetUidStatus {
   410  	return []SyncerSetUidStatus{
   411  		SyncerSetUidOK,
   412  		SyncerSetUidCleared,
   413  	}
   414  }
   415  
   416  // MetricsEngine is a generic interface to record PBS metrics into the desired backend
   417  // The first three metrics function fire off once per incoming request, so total metrics
   418  // will equal the total number of incoming requests. The remaining 5 fire off per outgoing
   419  // request to a bidder adapter, so will record a number of hits per incoming request. The
   420  // two groups should be consistent within themselves, but comparing numbers between groups
   421  // is generally not useful.
   422  type MetricsEngine interface {
   423  	RecordConnectionAccept(success bool)
   424  	RecordTMaxTimeout()
   425  	RecordConnectionClose(success bool)
   426  	RecordRequest(labels Labels)                           // ignores adapter. only statusOk and statusErr fom status
   427  	RecordImps(labels ImpLabels)                           // RecordImps across openRTB2 engines that support the 'Native' Imp Type
   428  	RecordRequestTime(labels Labels, length time.Duration) // ignores adapter. only statusOk and statusErr fom status
   429  	RecordOverheadTime(overHead OverheadType, length time.Duration)
   430  	RecordAdapterRequest(labels AdapterLabels)
   431  	RecordAdapterConnections(adapterName openrtb_ext.BidderName, connWasReused bool, connWaitTime time.Duration)
   432  	RecordDNSTime(dnsLookupTime time.Duration)
   433  	RecordTLSHandshakeTime(tlsHandshakeTime time.Duration)
   434  	RecordBidderServerResponseTime(bidderServerResponseTime time.Duration)
   435  	RecordAdapterPanic(labels AdapterLabels)
   436  	RecordAdapterBidReceived(labels AdapterLabels, bidType openrtb_ext.BidType, hasAdm bool)
   437  	RecordAdapterPrice(labels AdapterLabels, cpm float64)
   438  	RecordAdapterTime(labels AdapterLabels, length time.Duration)
   439  	RecordCookieSync(status CookieSyncStatus)
   440  	RecordSyncerRequest(key string, status SyncerCookieSyncStatus)
   441  	RecordSetUid(status SetUidStatus)
   442  	RecordSyncerSet(key string, status SyncerSetUidStatus)
   443  	RecordStoredReqCacheResult(cacheResult CacheResult, inc int)
   444  	RecordStoredImpCacheResult(cacheResult CacheResult, inc int)
   445  	RecordAccountCacheResult(cacheResult CacheResult, inc int)
   446  	RecordStoredDataFetchTime(labels StoredDataLabels, length time.Duration)
   447  	RecordStoredDataError(labels StoredDataLabels)
   448  	RecordPrebidCacheRequestTime(success bool, length time.Duration)
   449  	RecordRequestQueueTime(success bool, requestType RequestType, length time.Duration)
   450  	RecordTimeoutNotice(success bool)
   451  	RecordRequestPrivacy(privacy PrivacyLabels)
   452  	RecordAdapterGDPRRequestBlocked(adapterName openrtb_ext.BidderName)
   453  	RecordDebugRequest(debugEnabled bool, pubId string)
   454  	RecordStoredResponse(pubId string)
   455  	RecordAdsCertReq(success bool)
   456  	RecordAdsCertSignTime(adsCertSignTime time.Duration)
   457  	RecordBidValidationCreativeSizeError(adapter openrtb_ext.BidderName, account string)
   458  	RecordBidValidationCreativeSizeWarn(adapter openrtb_ext.BidderName, account string)
   459  	RecordBidValidationSecureMarkupError(adapter openrtb_ext.BidderName, account string)
   460  	RecordBidValidationSecureMarkupWarn(adapter openrtb_ext.BidderName, account string)
   461  	RecordModuleCalled(labels ModuleLabels, duration time.Duration)
   462  	RecordModuleFailed(labels ModuleLabels)
   463  	RecordModuleSuccessNooped(labels ModuleLabels)
   464  	RecordModuleSuccessUpdated(labels ModuleLabels)
   465  	RecordModuleSuccessRejected(labels ModuleLabels)
   466  	RecordModuleExecutionError(labels ModuleLabels)
   467  	RecordModuleTimeout(labels ModuleLabels)
   468  	RecordAccountGDPRPurposeWarning(account string, purposeName string)
   469  	RecordAccountGDPRChannelEnabledWarning(account string)
   470  	RecordAccountCCPAChannelEnabledWarning(account string)
   471  	RecordAccountUpgradeStatus(account string)
   472  }