github.com/prebid/prebid-server/v2@v2.18.0/metrics/metrics.go (about)

     1  package metrics
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/prebid/prebid-server/v2/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  	DemandDOOH    DemandSource = "dooh"
   168  	DemandUnknown DemandSource = "unknown"
   169  )
   170  
   171  func DemandTypes() []DemandSource {
   172  	return []DemandSource{
   173  		DemandWeb,
   174  		DemandApp,
   175  		DemandDOOH,
   176  		DemandUnknown,
   177  	}
   178  }
   179  
   180  // The request types (endpoints)
   181  const (
   182  	ReqTypeORTB2Web  RequestType = "openrtb2-web"
   183  	ReqTypeORTB2App  RequestType = "openrtb2-app"
   184  	ReqTypeORTB2DOOH RequestType = "openrtb2-dooh"
   185  	ReqTypeAMP       RequestType = "amp"
   186  	ReqTypeVideo     RequestType = "video"
   187  )
   188  
   189  func RequestTypes() []RequestType {
   190  	return []RequestType{
   191  		ReqTypeORTB2Web,
   192  		ReqTypeORTB2App,
   193  		ReqTypeORTB2DOOH,
   194  		ReqTypeAMP,
   195  		ReqTypeVideo,
   196  	}
   197  }
   198  
   199  // The media types described in the "imp" json objects
   200  const (
   201  	ImpTypeBanner ImpMediaType = "banner"
   202  	ImpTypeVideo  ImpMediaType = "video"
   203  	ImpTypeAudio  ImpMediaType = "audio"
   204  	ImpTypeNative ImpMediaType = "native"
   205  )
   206  
   207  func ImpTypes() []ImpMediaType {
   208  	return []ImpMediaType{
   209  		ImpTypeBanner,
   210  		ImpTypeVideo,
   211  		ImpTypeAudio,
   212  		ImpTypeNative,
   213  	}
   214  }
   215  
   216  // Cookie flag
   217  const (
   218  	CookieFlagYes     CookieFlag = "exists"
   219  	CookieFlagNo      CookieFlag = "no"
   220  	CookieFlagUnknown CookieFlag = "unknown"
   221  )
   222  
   223  func CookieTypes() []CookieFlag {
   224  	return []CookieFlag{
   225  		CookieFlagYes,
   226  		CookieFlagNo,
   227  		CookieFlagUnknown,
   228  	}
   229  }
   230  
   231  // Request/return status
   232  const (
   233  	RequestStatusOK               RequestStatus = "ok"
   234  	RequestStatusBadInput         RequestStatus = "badinput"
   235  	RequestStatusErr              RequestStatus = "err"
   236  	RequestStatusNetworkErr       RequestStatus = "networkerr"
   237  	RequestStatusBlacklisted      RequestStatus = "blacklistedacctorapp"
   238  	RequestStatusQueueTimeout     RequestStatus = "queuetimeout"
   239  	RequestStatusAccountConfigErr RequestStatus = "acctconfigerr"
   240  )
   241  
   242  func RequestStatuses() []RequestStatus {
   243  	return []RequestStatus{
   244  		RequestStatusOK,
   245  		RequestStatusBadInput,
   246  		RequestStatusErr,
   247  		RequestStatusNetworkErr,
   248  		RequestStatusBlacklisted,
   249  		RequestStatusQueueTimeout,
   250  		RequestStatusAccountConfigErr,
   251  	}
   252  }
   253  
   254  // Adapter bid response status.
   255  const (
   256  	AdapterBidPresent AdapterBid = "bid"
   257  	AdapterBidNone    AdapterBid = "nobid"
   258  )
   259  
   260  func AdapterBids() []AdapterBid {
   261  	return []AdapterBid{
   262  		AdapterBidPresent,
   263  		AdapterBidNone,
   264  	}
   265  }
   266  
   267  // Adapter execution status
   268  const (
   269  	AdapterErrorBadInput            AdapterError = "badinput"
   270  	AdapterErrorBadServerResponse   AdapterError = "badserverresponse"
   271  	AdapterErrorTimeout             AdapterError = "timeout"
   272  	AdapterErrorFailedToRequestBids AdapterError = "failedtorequestbid"
   273  	AdapterErrorValidation          AdapterError = "validation"
   274  	AdapterErrorTmaxTimeout         AdapterError = "tmaxtimeout"
   275  	AdapterErrorUnknown             AdapterError = "unknown_error"
   276  )
   277  
   278  func AdapterErrors() []AdapterError {
   279  	return []AdapterError{
   280  		AdapterErrorBadInput,
   281  		AdapterErrorBadServerResponse,
   282  		AdapterErrorTimeout,
   283  		AdapterErrorFailedToRequestBids,
   284  		AdapterErrorValidation,
   285  		AdapterErrorTmaxTimeout,
   286  		AdapterErrorUnknown,
   287  	}
   288  }
   289  
   290  const (
   291  	// CacheHit represents a cache hit i.e the key was found in cache
   292  	CacheHit CacheResult = "hit"
   293  	// CacheMiss represents a cache miss i.e that key wasn't found in cache
   294  	// and had to be fetched from the backend
   295  	CacheMiss CacheResult = "miss"
   296  )
   297  
   298  // CacheResults returns possible cache results i.e. cache hit or miss
   299  func CacheResults() []CacheResult {
   300  	return []CacheResult{
   301  		CacheHit,
   302  		CacheMiss,
   303  	}
   304  }
   305  
   306  // TCFVersionValue : The possible values for TCF versions
   307  type TCFVersionValue string
   308  
   309  const (
   310  	TCFVersionErr TCFVersionValue = "err"
   311  	TCFVersionV2  TCFVersionValue = "v2"
   312  )
   313  
   314  // TCFVersions returns the possible values for the TCF version
   315  func TCFVersions() []TCFVersionValue {
   316  	return []TCFVersionValue{
   317  		TCFVersionErr,
   318  		TCFVersionV2,
   319  	}
   320  }
   321  
   322  // TCFVersionToValue takes an integer TCF version and returns the corresponding TCFVersionValue
   323  func TCFVersionToValue(version int) TCFVersionValue {
   324  	switch {
   325  	case version == 2:
   326  		return TCFVersionV2
   327  	}
   328  	return TCFVersionErr
   329  }
   330  
   331  // CookieSyncStatus is a status code resulting from a call to the /cookie_sync endpoint.
   332  type CookieSyncStatus string
   333  
   334  const (
   335  	CookieSyncOK                     CookieSyncStatus = "ok"
   336  	CookieSyncBadRequest             CookieSyncStatus = "bad_request"
   337  	CookieSyncOptOut                 CookieSyncStatus = "opt_out"
   338  	CookieSyncGDPRHostCookieBlocked  CookieSyncStatus = "gdpr_blocked_host_cookie"
   339  	CookieSyncAccountBlocked         CookieSyncStatus = "acct_blocked"
   340  	CookieSyncAccountConfigMalformed CookieSyncStatus = "acct_config_malformed"
   341  	CookieSyncAccountInvalid         CookieSyncStatus = "acct_invalid"
   342  )
   343  
   344  // CookieSyncStatuses returns possible cookie sync statuses.
   345  func CookieSyncStatuses() []CookieSyncStatus {
   346  	return []CookieSyncStatus{
   347  		CookieSyncOK,
   348  		CookieSyncBadRequest,
   349  		CookieSyncOptOut,
   350  		CookieSyncGDPRHostCookieBlocked,
   351  		CookieSyncAccountBlocked,
   352  		CookieSyncAccountConfigMalformed,
   353  		CookieSyncAccountInvalid,
   354  	}
   355  }
   356  
   357  // SyncerCookieSyncStatus is a status code from an invocation of a syncer resulting from a call to the /cookie_sync endpoint.
   358  type SyncerCookieSyncStatus string
   359  
   360  const (
   361  	SyncerCookieSyncOK               SyncerCookieSyncStatus = "ok"
   362  	SyncerCookieSyncPrivacyBlocked   SyncerCookieSyncStatus = "privacy_blocked"
   363  	SyncerCookieSyncAlreadySynced    SyncerCookieSyncStatus = "already_synced"
   364  	SyncerCookieSyncTypeNotSupported SyncerCookieSyncStatus = "type_not_supported"
   365  )
   366  
   367  // SyncerRequestStatuses returns possible syncer statuses.
   368  func SyncerRequestStatuses() []SyncerCookieSyncStatus {
   369  	return []SyncerCookieSyncStatus{
   370  		SyncerCookieSyncOK,
   371  		SyncerCookieSyncPrivacyBlocked,
   372  		SyncerCookieSyncAlreadySynced,
   373  		SyncerCookieSyncTypeNotSupported,
   374  	}
   375  }
   376  
   377  // SetUidStatus is a status code resulting from a call to the /setuid endpoint.
   378  type SetUidStatus string
   379  
   380  // /setuid action labels
   381  const (
   382  	SetUidOK                     SetUidStatus = "ok"
   383  	SetUidBadRequest             SetUidStatus = "bad_request"
   384  	SetUidOptOut                 SetUidStatus = "opt_out"
   385  	SetUidGDPRHostCookieBlocked  SetUidStatus = "gdpr_blocked_host_cookie"
   386  	SetUidAccountBlocked         SetUidStatus = "acct_blocked"
   387  	SetUidAccountConfigMalformed SetUidStatus = "acct_config_malformed"
   388  	SetUidAccountInvalid         SetUidStatus = "acct_invalid"
   389  	SetUidSyncerUnknown          SetUidStatus = "syncer_unknown"
   390  )
   391  
   392  // SetUidStatuses returns possible setuid statuses.
   393  func SetUidStatuses() []SetUidStatus {
   394  	return []SetUidStatus{
   395  		SetUidOK,
   396  		SetUidBadRequest,
   397  		SetUidOptOut,
   398  		SetUidGDPRHostCookieBlocked,
   399  		SetUidAccountBlocked,
   400  		SetUidAccountConfigMalformed,
   401  		SetUidAccountInvalid,
   402  		SetUidSyncerUnknown,
   403  	}
   404  }
   405  
   406  // SyncerSetUidStatus is a status code from an invocation of a syncer resulting from a call to the /setuid endpoint.
   407  type SyncerSetUidStatus string
   408  
   409  const (
   410  	SyncerSetUidOK      SyncerSetUidStatus = "ok"
   411  	SyncerSetUidCleared SyncerSetUidStatus = "cleared"
   412  )
   413  
   414  // SyncerSetUidStatuses returns possible syncer set statuses.
   415  func SyncerSetUidStatuses() []SyncerSetUidStatus {
   416  	return []SyncerSetUidStatus{
   417  		SyncerSetUidOK,
   418  		SyncerSetUidCleared,
   419  	}
   420  }
   421  
   422  // MetricsEngine is a generic interface to record PBS metrics into the desired backend
   423  // The first three metrics function fire off once per incoming request, so total metrics
   424  // will equal the total number of incoming requests. The remaining 5 fire off per outgoing
   425  // request to a bidder adapter, so will record a number of hits per incoming request. The
   426  // two groups should be consistent within themselves, but comparing numbers between groups
   427  // is generally not useful.
   428  type MetricsEngine interface {
   429  	RecordConnectionAccept(success bool)
   430  	RecordTMaxTimeout()
   431  	RecordConnectionClose(success bool)
   432  	RecordRequest(labels Labels)                           // ignores adapter. only statusOk and statusErr fom status
   433  	RecordImps(labels ImpLabels)                           // RecordImps across openRTB2 engines that support the 'Native' Imp Type
   434  	RecordRequestTime(labels Labels, length time.Duration) // ignores adapter. only statusOk and statusErr fom status
   435  	RecordOverheadTime(overHead OverheadType, length time.Duration)
   436  	RecordAdapterRequest(labels AdapterLabels)
   437  	RecordAdapterConnections(adapterName openrtb_ext.BidderName, connWasReused bool, connWaitTime time.Duration)
   438  	RecordDNSTime(dnsLookupTime time.Duration)
   439  	RecordTLSHandshakeTime(tlsHandshakeTime time.Duration)
   440  	RecordBidderServerResponseTime(bidderServerResponseTime time.Duration)
   441  	RecordAdapterPanic(labels AdapterLabels)
   442  	RecordAdapterBidReceived(labels AdapterLabels, bidType openrtb_ext.BidType, hasAdm bool)
   443  	RecordAdapterPrice(labels AdapterLabels, cpm float64)
   444  	RecordAdapterTime(labels AdapterLabels, length time.Duration)
   445  	RecordCookieSync(status CookieSyncStatus)
   446  	RecordSyncerRequest(key string, status SyncerCookieSyncStatus)
   447  	RecordSetUid(status SetUidStatus)
   448  	RecordSyncerSet(key string, status SyncerSetUidStatus)
   449  	RecordStoredReqCacheResult(cacheResult CacheResult, inc int)
   450  	RecordStoredImpCacheResult(cacheResult CacheResult, inc int)
   451  	RecordAccountCacheResult(cacheResult CacheResult, inc int)
   452  	RecordStoredDataFetchTime(labels StoredDataLabels, length time.Duration)
   453  	RecordStoredDataError(labels StoredDataLabels)
   454  	RecordPrebidCacheRequestTime(success bool, length time.Duration)
   455  	RecordRequestQueueTime(success bool, requestType RequestType, length time.Duration)
   456  	RecordTimeoutNotice(success bool)
   457  	RecordRequestPrivacy(privacy PrivacyLabels)
   458  	RecordAdapterBuyerUIDScrubbed(adapterName openrtb_ext.BidderName)
   459  	RecordAdapterGDPRRequestBlocked(adapterName openrtb_ext.BidderName)
   460  	RecordDebugRequest(debugEnabled bool, pubId string)
   461  	RecordStoredResponse(pubId string)
   462  	RecordAdsCertReq(success bool)
   463  	RecordAdsCertSignTime(adsCertSignTime time.Duration)
   464  	RecordBidValidationCreativeSizeError(adapter openrtb_ext.BidderName, account string)
   465  	RecordBidValidationCreativeSizeWarn(adapter openrtb_ext.BidderName, account string)
   466  	RecordBidValidationSecureMarkupError(adapter openrtb_ext.BidderName, account string)
   467  	RecordBidValidationSecureMarkupWarn(adapter openrtb_ext.BidderName, account string)
   468  	RecordModuleCalled(labels ModuleLabels, duration time.Duration)
   469  	RecordModuleFailed(labels ModuleLabels)
   470  	RecordModuleSuccessNooped(labels ModuleLabels)
   471  	RecordModuleSuccessUpdated(labels ModuleLabels)
   472  	RecordModuleSuccessRejected(labels ModuleLabels)
   473  	RecordModuleExecutionError(labels ModuleLabels)
   474  	RecordModuleTimeout(labels ModuleLabels)
   475  }