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

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