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

     1  package prometheusmetrics
     2  
     3  import (
     4  	"github.com/prebid/prebid-server/v2/metrics"
     5  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     6  	"github.com/prometheus/client_golang/prometheus"
     7  )
     8  
     9  func preloadLabelValues(m *Metrics, syncerKeys []string, moduleStageNames map[string][]string) {
    10  	var (
    11  		adapterErrorValues        = enumAsString(metrics.AdapterErrors())
    12  		adapterValues             = enumAsLowerCaseString(openrtb_ext.CoreBidderNames())
    13  		bidTypeValues             = []string{markupDeliveryAdm, markupDeliveryNurl}
    14  		boolValues                = boolValuesAsString()
    15  		cacheResultValues         = enumAsString(metrics.CacheResults())
    16  		connectionErrorValues     = []string{connectionAcceptError, connectionCloseError}
    17  		cookieSyncStatusValues    = enumAsString(metrics.CookieSyncStatuses())
    18  		cookieValues              = enumAsString(metrics.CookieTypes())
    19  		overheadTypes             = enumAsString(metrics.OverheadTypes())
    20  		requestStatusValues       = enumAsString(metrics.RequestStatuses())
    21  		requestTypeValues         = enumAsString(metrics.RequestTypes())
    22  		setUidStatusValues        = enumAsString(metrics.SetUidStatuses())
    23  		sourceValues              = []string{sourceRequest}
    24  		storedDataErrorValues     = enumAsString(metrics.StoredDataErrors())
    25  		storedDataFetchTypeValues = enumAsString(metrics.StoredDataFetchTypes())
    26  		syncerRequestStatusValues = enumAsString(metrics.SyncerRequestStatuses())
    27  		syncerSetsStatusValues    = enumAsString(metrics.SyncerSetUidStatuses())
    28  		tcfVersionValues          = enumAsString(metrics.TCFVersions())
    29  	)
    30  
    31  	preloadLabelValuesForCounter(m.connectionsError, map[string][]string{
    32  		connectionErrorLabel: connectionErrorValues,
    33  	})
    34  
    35  	preloadLabelValuesForCounter(m.cookieSync, map[string][]string{
    36  		statusLabel: cookieSyncStatusValues,
    37  	})
    38  
    39  	preloadLabelValuesForCounter(m.setUid, map[string][]string{
    40  		statusLabel: setUidStatusValues,
    41  	})
    42  
    43  	preloadLabelValuesForCounter(m.impressions, map[string][]string{
    44  		isBannerLabel: boolValues,
    45  		isVideoLabel:  boolValues,
    46  		isAudioLabel:  boolValues,
    47  		isNativeLabel: boolValues,
    48  	})
    49  
    50  	preloadLabelValuesForHistogram(m.prebidCacheWriteTimer, map[string][]string{
    51  		successLabel: boolValues,
    52  	})
    53  
    54  	preloadLabelValuesForCounter(m.requests, map[string][]string{
    55  		requestTypeLabel:   requestTypeValues,
    56  		requestStatusLabel: requestStatusValues,
    57  	})
    58  
    59  	preloadLabelValuesForHistogram(m.requestsTimer, map[string][]string{
    60  		requestTypeLabel: requestTypeValues,
    61  	})
    62  
    63  	preloadLabelValuesForHistogram(m.storedAccountFetchTimer, map[string][]string{
    64  		storedDataFetchTypeLabel: storedDataFetchTypeValues,
    65  	})
    66  
    67  	preloadLabelValuesForHistogram(m.storedAMPFetchTimer, map[string][]string{
    68  		storedDataFetchTypeLabel: storedDataFetchTypeValues,
    69  	})
    70  
    71  	preloadLabelValuesForHistogram(m.storedCategoryFetchTimer, map[string][]string{
    72  		storedDataFetchTypeLabel: storedDataFetchTypeValues,
    73  	})
    74  
    75  	preloadLabelValuesForHistogram(m.storedRequestFetchTimer, map[string][]string{
    76  		storedDataFetchTypeLabel: storedDataFetchTypeValues,
    77  	})
    78  
    79  	preloadLabelValuesForHistogram(m.storedVideoFetchTimer, map[string][]string{
    80  		storedDataFetchTypeLabel: storedDataFetchTypeValues,
    81  	})
    82  
    83  	preloadLabelValuesForHistogram(m.storedResponsesFetchTimer, map[string][]string{
    84  		storedDataFetchTypeLabel: storedDataFetchTypeValues,
    85  	})
    86  
    87  	preloadLabelValuesForCounter(m.storedAccountErrors, map[string][]string{
    88  		storedDataErrorLabel: storedDataErrorValues,
    89  	})
    90  
    91  	preloadLabelValuesForCounter(m.storedAMPErrors, map[string][]string{
    92  		storedDataErrorLabel: storedDataErrorValues,
    93  	})
    94  
    95  	preloadLabelValuesForCounter(m.storedCategoryErrors, map[string][]string{
    96  		storedDataErrorLabel: storedDataErrorValues,
    97  	})
    98  
    99  	preloadLabelValuesForCounter(m.storedRequestErrors, map[string][]string{
   100  		storedDataErrorLabel: storedDataErrorValues,
   101  	})
   102  
   103  	preloadLabelValuesForCounter(m.storedVideoErrors, map[string][]string{
   104  		storedDataErrorLabel: storedDataErrorValues,
   105  	})
   106  
   107  	preloadLabelValuesForCounter(m.storedResponsesErrors, map[string][]string{
   108  		storedDataErrorLabel: storedDataErrorValues,
   109  	})
   110  
   111  	preloadLabelValuesForCounter(m.requestsWithoutCookie, map[string][]string{
   112  		requestTypeLabel: requestTypeValues,
   113  	})
   114  
   115  	preloadLabelValuesForCounter(m.storedImpressionsCacheResult, map[string][]string{
   116  		cacheResultLabel: cacheResultValues,
   117  	})
   118  
   119  	preloadLabelValuesForCounter(m.storedRequestCacheResult, map[string][]string{
   120  		cacheResultLabel: cacheResultValues,
   121  	})
   122  
   123  	preloadLabelValuesForCounter(m.accountCacheResult, map[string][]string{
   124  		cacheResultLabel: cacheResultValues,
   125  	})
   126  
   127  	preloadLabelValuesForCounter(m.adapterBids, map[string][]string{
   128  		adapterLabel:        adapterValues,
   129  		markupDeliveryLabel: bidTypeValues,
   130  	})
   131  
   132  	preloadLabelValuesForCounter(m.adapterErrors, map[string][]string{
   133  		adapterLabel:      adapterValues,
   134  		adapterErrorLabel: adapterErrorValues,
   135  	})
   136  
   137  	preloadLabelValuesForCounter(m.adapterPanics, map[string][]string{
   138  		adapterLabel: adapterValues,
   139  	})
   140  
   141  	preloadLabelValuesForCounter(m.adapterBidResponseSecureMarkupError, map[string][]string{
   142  		adapterLabel: adapterValues,
   143  		successLabel: boolValues,
   144  	})
   145  
   146  	preloadLabelValuesForCounter(m.adapterBidResponseSecureMarkupWarn, map[string][]string{
   147  		adapterLabel: adapterValues,
   148  		successLabel: boolValues,
   149  	})
   150  
   151  	preloadLabelValuesForCounter(m.adapterBidResponseValidationSizeError, map[string][]string{
   152  		adapterLabel: adapterValues,
   153  		successLabel: boolValues,
   154  	})
   155  
   156  	preloadLabelValuesForCounter(m.adapterBidResponseValidationSizeWarn, map[string][]string{
   157  		adapterLabel: adapterValues,
   158  		successLabel: boolValues,
   159  	})
   160  
   161  	preloadLabelValuesForHistogram(m.adapterPrices, map[string][]string{
   162  		adapterLabel: adapterValues,
   163  	})
   164  
   165  	preloadLabelValuesForCounter(m.adapterRequests, map[string][]string{
   166  		adapterLabel: adapterValues,
   167  		cookieLabel:  cookieValues,
   168  		hasBidsLabel: boolValues,
   169  	})
   170  
   171  	preloadLabelValuesForCounter(m.adsCertRequests, map[string][]string{
   172  		successLabel: boolValues,
   173  	})
   174  
   175  	if !m.metricsDisabled.AdapterConnectionMetrics {
   176  		preloadLabelValuesForCounter(m.adapterCreatedConnections, map[string][]string{
   177  			adapterLabel: adapterValues,
   178  		})
   179  
   180  		preloadLabelValuesForCounter(m.adapterReusedConnections, map[string][]string{
   181  			adapterLabel: adapterValues,
   182  		})
   183  
   184  		preloadLabelValuesForHistogram(m.adapterConnectionWaitTime, map[string][]string{
   185  			adapterLabel: adapterValues,
   186  		})
   187  	}
   188  
   189  	preloadLabelValuesForHistogram(m.adapterRequestsTimer, map[string][]string{
   190  		adapterLabel: adapterValues,
   191  	})
   192  
   193  	preloadLabelValuesForHistogram(m.overheadTimer, map[string][]string{
   194  		overheadTypeLabel: overheadTypes,
   195  	})
   196  
   197  	preloadLabelValuesForCounter(m.syncerRequests, map[string][]string{
   198  		syncerLabel: syncerKeys,
   199  		statusLabel: syncerRequestStatusValues,
   200  	})
   201  
   202  	preloadLabelValuesForCounter(m.syncerSets, map[string][]string{
   203  		syncerLabel: syncerKeys,
   204  		statusLabel: syncerSetsStatusValues,
   205  	})
   206  
   207  	//to minimize memory usage, queuedTimeout metric is now supported for video endpoint only
   208  	//boolean value represents 2 general request statuses: accepted and rejected
   209  	preloadLabelValuesForHistogram(m.requestsQueueTimer, map[string][]string{
   210  		requestTypeLabel:   {string(metrics.ReqTypeVideo)},
   211  		requestStatusLabel: {requestSuccessLabel, requestRejectLabel},
   212  	})
   213  
   214  	preloadLabelValuesForCounter(m.privacyCCPA, map[string][]string{
   215  		sourceLabel: sourceValues,
   216  		optOutLabel: boolValues,
   217  	})
   218  
   219  	preloadLabelValuesForCounter(m.privacyCOPPA, map[string][]string{
   220  		sourceLabel: sourceValues,
   221  	})
   222  
   223  	preloadLabelValuesForCounter(m.privacyLMT, map[string][]string{
   224  		sourceLabel: sourceValues,
   225  	})
   226  
   227  	preloadLabelValuesForCounter(m.privacyTCF, map[string][]string{
   228  		sourceLabel:  sourceValues,
   229  		versionLabel: tcfVersionValues,
   230  	})
   231  
   232  	if !m.metricsDisabled.AdapterBuyerUIDScrubbed {
   233  		preloadLabelValuesForCounter(m.adapterScrubbedBuyerUIDs, map[string][]string{
   234  			adapterLabel: adapterValues,
   235  		})
   236  	}
   237  
   238  	if !m.metricsDisabled.AdapterGDPRRequestBlocked {
   239  		preloadLabelValuesForCounter(m.adapterGDPRBlockedRequests, map[string][]string{
   240  			adapterLabel: adapterValues,
   241  		})
   242  	}
   243  
   244  	for module, stageValues := range moduleStageNames {
   245  		preloadLabelValuesForHistogram(m.moduleDuration[module], map[string][]string{
   246  			stageLabel: stageValues,
   247  		})
   248  
   249  		preloadLabelValuesForCounter(m.moduleCalls[module], map[string][]string{
   250  			stageLabel: stageValues,
   251  		})
   252  
   253  		preloadLabelValuesForCounter(m.moduleFailures[module], map[string][]string{
   254  			stageLabel: stageValues,
   255  		})
   256  
   257  		preloadLabelValuesForCounter(m.moduleSuccessNoops[module], map[string][]string{
   258  			stageLabel: stageValues,
   259  		})
   260  
   261  		preloadLabelValuesForCounter(m.moduleSuccessUpdates[module], map[string][]string{
   262  			stageLabel: stageValues,
   263  		})
   264  
   265  		preloadLabelValuesForCounter(m.moduleSuccessRejects[module], map[string][]string{
   266  			stageLabel: stageValues,
   267  		})
   268  
   269  		preloadLabelValuesForCounter(m.moduleExecutionErrors[module], map[string][]string{
   270  			stageLabel: stageValues,
   271  		})
   272  
   273  		preloadLabelValuesForCounter(m.moduleTimeouts[module], map[string][]string{
   274  			stageLabel: stageValues,
   275  		})
   276  	}
   277  }
   278  
   279  func preloadLabelValuesForCounter(counter *prometheus.CounterVec, labelsWithValues map[string][]string) {
   280  	registerLabelPermutations(labelsWithValues, func(labels prometheus.Labels) {
   281  		counter.With(labels)
   282  	})
   283  }
   284  
   285  func preloadLabelValuesForHistogram(histogram *prometheus.HistogramVec, labelsWithValues map[string][]string) {
   286  	registerLabelPermutations(labelsWithValues, func(labels prometheus.Labels) {
   287  		histogram.With(labels)
   288  	})
   289  }
   290  
   291  func registerLabelPermutations(labelsWithValues map[string][]string, register func(prometheus.Labels)) {
   292  	if len(labelsWithValues) == 0 {
   293  		return
   294  	}
   295  
   296  	keys := make([]string, 0, len(labelsWithValues))
   297  	values := make([][]string, 0, len(labelsWithValues))
   298  	for k, v := range labelsWithValues {
   299  		keys = append(keys, k)
   300  		values = append(values, v)
   301  	}
   302  
   303  	labels := prometheus.Labels{}
   304  	registerLabelPermutationsRecursive(0, keys, values, labels, register)
   305  }
   306  
   307  func registerLabelPermutationsRecursive(depth int, keys []string, values [][]string, labels prometheus.Labels, register func(prometheus.Labels)) {
   308  	label := keys[depth]
   309  	isLeaf := depth == len(keys)-1
   310  
   311  	if isLeaf {
   312  		for _, v := range values[depth] {
   313  			labels[label] = v
   314  			register(cloneLabels(labels))
   315  		}
   316  	} else {
   317  		for _, v := range values[depth] {
   318  			labels[label] = v
   319  			registerLabelPermutationsRecursive(depth+1, keys, values, labels, register)
   320  		}
   321  	}
   322  }
   323  
   324  func cloneLabels(labels prometheus.Labels) prometheus.Labels {
   325  	clone := prometheus.Labels{}
   326  	for k, v := range labels {
   327  		clone[k] = v
   328  	}
   329  	return clone
   330  }