Skip to content

Commit

Permalink
Move TCF metrics more in line with other metrics implementaion
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhjort committed Jun 24, 2020
1 parent 8144622 commit 5e46a52
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 23 deletions.
33 changes: 18 additions & 15 deletions pbsmetrics/go_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ type Metrics struct {
TimeoutNotificationFailure metrics.Meter

// TCF adaption metrics
TCF1ReqMeter metrics.Meter
TCF2ReqMeter metrics.Meter
TCFReqErrMeter metrics.Meter
TCFReqVersion []metrics.Meter

AdapterMetrics map[openrtb_ext.BidderName]*AdapterMetrics
// Don't export accountMetrics because we need helper functions here to insure its properly populated dynamically
Expand Down Expand Up @@ -143,9 +141,7 @@ func NewBlankMetrics(registry metrics.Registry, exchanges []openrtb_ext.BidderNa
TimeoutNotificationSuccess: blankMeter,
TimeoutNotificationFailure: blankMeter,

TCF1ReqMeter: blankMeter,
TCF2ReqMeter: blankMeter,
TCFReqErrMeter: blankMeter,
TCFReqVersion: make([]metrics.Meter, len(TCFVersions()), len(TCFVersions())),

AdapterMetrics: make(map[openrtb_ext.BidderName]*AdapterMetrics, len(exchanges)),
accountMetrics: make(map[string]*accountMetrics),
Expand All @@ -164,6 +160,15 @@ func NewBlankMetrics(registry metrics.Registry, exchanges []openrtb_ext.BidderNa
}
}

for _, c := range CacheResults() {
newMetrics.StoredReqCacheMeter[c] = blankMeter
newMetrics.StoredImpCacheMeter[c] = blankMeter
}

for i := range TCFVersions() {
newMetrics.TCFReqVersion[i] = blankMeter
}

//to minimize memory usage, queuedTimeout metric is now supported for video endpoint only
//boolean value represents 2 general request statuses: accepted and rejected
newMetrics.RequestsQueueTimer["video"] = make(map[bool]metrics.Timer)
Expand Down Expand Up @@ -229,9 +234,10 @@ func NewMetrics(registry metrics.Registry, exchanges []openrtb_ext.BidderName, d
newMetrics.TimeoutNotificationSuccess = metrics.GetOrRegisterMeter("timeout_notification.ok", registry)
newMetrics.TimeoutNotificationFailure = metrics.GetOrRegisterMeter("timeout_notification.failed", registry)

newMetrics.TCF1ReqMeter = metrics.GetOrRegisterMeter("privacy.request.tcf.v1", registry)
newMetrics.TCF2ReqMeter = metrics.GetOrRegisterMeter("privacy.request.tcf.v2", registry)
newMetrics.TCFReqErrMeter = metrics.GetOrRegisterMeter("privacy.request.tcf.err", registry)
for i, version := range TCFVersions() {
newMetrics.TCFReqVersion[i] = metrics.GetOrRegisterMeter(fmt.Sprintf("privacy.request.tcf.%s", string(version)), registry)
}

return newMetrics
}

Expand Down Expand Up @@ -577,13 +583,10 @@ func (me *Metrics) RecordTimeoutNotice(success bool) {
}

func (me *Metrics) RecordTCFReq(version int) {
if version == 1 {
me.TCF1ReqMeter.Mark(1)
} else if version == 2 {
me.TCF2ReqMeter.Mark(1)
} else {
me.TCFReqErrMeter.Mark(1)
if version >= 0 && version < len(me.TCFReqVersion) {
me.TCFReqVersion[version].Mark(1)
}
me.TCFReqVersion[0].Mark(1)
return
}

Expand Down
6 changes: 3 additions & 3 deletions pbsmetrics/go_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func TestNewMetrics(t *testing.T) {

ensureContains(t, registry, "timeout_notification.ok", m.TimeoutNotificationSuccess)
ensureContains(t, registry, "timeout_notification.failed", m.TimeoutNotificationFailure)
ensureContains(t, registry, "privacy.request.tcf.v1", m.TCF1ReqMeter)
ensureContains(t, registry, "privacy.request.tcf.v2", m.TCF2ReqMeter)
ensureContains(t, registry, "privacy.request.tcf.err", m.TCFReqErrMeter)
ensureContains(t, registry, "privacy.request.tcf.v1", m.TCFReqVersion[1])
ensureContains(t, registry, "privacy.request.tcf.v2", m.TCFReqVersion[2])
ensureContains(t, registry, "privacy.request.tcf.err", m.TCFReqVersion[0])

}

Expand Down
18 changes: 18 additions & 0 deletions pbsmetrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,24 @@ func RequestActions() []RequestAction {
}
}

// TCFVersionValue : The possible values for TCF versions
type TCFVersionValue string

const (
TCFVersionErr TCFVersionValue = "err"
TCFVersionV1 TCFVersionValue = "v1"
TCFVersionV2 TCFVersionValue = "v2"
)

// TCFVersions rtuens the possible values for the TCF version
func TCFVersions() []TCFVersionValue {
return []TCFVersionValue{
TCFVersionErr,
TCFVersionV1,
TCFVersionV2,
}
}

// MetricsEngine is a generic interface to record PBS metrics into the desired backend
// The first three metrics function fire off once per incoming request, so total metrics
// will equal the total number of incoming requests. The remaining 5 fire off per outgoing
Expand Down
2 changes: 1 addition & 1 deletion pbsmetrics/prometheus/preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func preloadLabelValues(m *Metrics) {
})

preloadLabelValuesForCounter(m.tcfMetrics, map[string][]string{
versionLabel: {"err", "v1", "v2"},
versionLabel: tcfVersionsAsString(),
sourceLabel: {string(sourceRequest)},
})
}
Expand Down
13 changes: 9 additions & 4 deletions pbsmetrics/prometheus/prometheus.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package prometheusmetrics

import (
"fmt"
"strconv"
"time"

Expand Down Expand Up @@ -44,6 +43,9 @@ type Metrics struct {

// Account Metrics
accountRequests *prometheus.CounterVec

// Lookup tables
tcfVersions []string
}

const (
Expand Down Expand Up @@ -170,6 +172,7 @@ func NewMetrics(cfg config.PrometheusMetrics) *Metrics {
"privacy_tcf",
"Count of TCF versions for requests where GDPR was enforced.",
[]string{versionLabel, sourceLabel})
metrics.tcfVersions = tcfVersionsAsString()

metrics.adapterBids = newCounter(cfg, metrics.Registry,
"adapter_bids",
Expand Down Expand Up @@ -436,9 +439,11 @@ func (m *Metrics) RecordTimeoutNotice(success bool) {
}

func (m *Metrics) RecordTCFReq(version int) {
var value string = "err"
if version > 0 {
value = fmt.Sprintf("v%d", version)
var value string
if version >= 0 && version < len(m.tcfVersions) {
value = m.tcfVersions[version]
} else {
value = m.tcfVersions[0]
}
m.tcfMetrics.With(prometheus.Labels{
versionLabel: value,
Expand Down
9 changes: 9 additions & 0 deletions pbsmetrics/prometheus/type_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,12 @@ func requestTypesAsString() []string {
}
return valuesAsString
}

func tcfVersionsAsString() []string {
values := pbsmetrics.TCFVersions()
valuesAsString := make([]string, len(values))
for i, v := range values {
valuesAsString[i] = string(v)
}
return valuesAsString
}

0 comments on commit 5e46a52

Please sign in to comment.