Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add geo and ip label to request #6

Merged
merged 1 commit into from
Sep 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func (deps *endpointDeps) Auction(w http.ResponseWriter, r *http.Request, _ http
OS: pbsmetrics.OSUnknown,
AppVersion: pbsmetrics.AppVersionUnknown,
IfaFlag: pbsmetrics.IfaFlagNo,
GeoFlag: pbsmetrics.GeoFlagNo,
IPFlag: pbsmetrics.IPFlagNo,
}
defer func() {
deps.metricsEngine.RecordRequest(labels)
Expand Down Expand Up @@ -134,6 +136,13 @@ func (deps *endpointDeps) Auction(w http.ResponseWriter, r *http.Request, _ http
if req.Device.IFA != "" {
labels.IfaFlag = pbsmetrics.IfaFlagYes
}
if req.Device.IP != "" {
labels.IPFlag = pbsmetrics.IPFlagYes
}

if req.Device.Geo != nil {
labels.GeoFlag = pbsmetrics.GeoFlagYes
}
}

if req.App != nil {
Expand Down
34 changes: 34 additions & 0 deletions pbsmetrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Labels struct {
IfaFlag IfaFlag
OS string
AppVersion string
GeoFlag GeoFlag
IPFlag IPFlag
}

// AdapterLabels defines the labels that can be attached to the adapter metrics.
Expand Down Expand Up @@ -64,6 +66,12 @@ type CookieFlag string
// IfaFlag : device ifa exists flag
type IfaFlag string

// GeoFlag : device geo exists flag
type GeoFlag string

// GeoFlag : device geo exists flag
type IPFlag string

// RequestStatus : The request return status
type RequestStatus string

Expand Down Expand Up @@ -177,6 +185,32 @@ func IfaTypes() []IfaFlag {
}
}

// geo flag
const (
GeoFlagYes GeoFlag = "exists"
GeoFlagNo GeoFlag = "no"
)

func GeoTypes() []GeoFlag {
return []GeoFlag{
GeoFlagYes,
GeoFlagNo,
}
}

// IP flag
const (
IPFlagYes IPFlag = "exists"
IPFlagNo IPFlag = "no"
)

func IPTypes() []IPFlag {
return []IPFlag{
IPFlagYes,
IPFlagNo,
}
}

// Request/return status
const (
RequestStatusOK RequestStatus = "ok"
Expand Down
4 changes: 4 additions & 0 deletions pbsmetrics/prometheus/preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func preloadLabelValues(m *Metrics) {
connectionErrorValues = []string{connectionAcceptError, connectionCloseError}
ifaValues = ifaTypesAsString()
osValues = []string{pbsmetrics.OSUnknown}
geoValues = geoTypesAsString()
IPValues = IPTypesAsString()
requestStatusValues = requestStatusesAsString()
requestTypeValues = requestTypesAsString()
)
Expand All @@ -43,6 +45,8 @@ func preloadLabelValues(m *Metrics) {
ifaLabel: ifaValues,
appVersionLabel: appVersionValues,
osLabel: osValues,
geoLabel: geoValues,
IPLabel: IPValues,
})

preloadLabelValuesForHistogram(m.requestsTimer, map[string][]string{
Expand Down
6 changes: 5 additions & 1 deletion pbsmetrics/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const (
cookieLabel = "cookie"
hasBidsLabel = "has_bids"
ifaLabel = "ifa"
geoLabel = "geo"
IPLabel = "ip"
isAudioLabel = "audio"
isBannerLabel = "banner"
isNativeLabel = "native"
Expand Down Expand Up @@ -134,7 +136,7 @@ func NewMetrics(cfg config.PrometheusMetrics) *Metrics {
metrics.requests = newCounter(cfg, metrics.Registry,
"requests",
"Count of total requests to Prebid Server labeled by type and status.",
[]string{requestTypeLabel, requestStatusLabel, ifaLabel, appVersionLabel, osLabel})
[]string{requestTypeLabel, requestStatusLabel, ifaLabel, appVersionLabel, osLabel, geoLabel, IPLabel})

metrics.requestsTimer = newHistogram(cfg, metrics.Registry,
"request_time_seconds",
Expand Down Expand Up @@ -287,6 +289,8 @@ func (m *Metrics) RecordRequest(labels pbsmetrics.Labels) {
appVersionLabel: string(labels.AppVersion),
ifaLabel: string(labels.IfaFlag),
osLabel: string(labels.OS),
geoLabel: string(labels.GeoFlag),
IPLabel: string(labels.IPFlag),
requestTypeLabel: string(labels.RType),
requestStatusLabel: string(labels.RequestStatus),
}).Inc()
Expand Down
6 changes: 6 additions & 0 deletions pbsmetrics/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,17 @@ func TestRequestMetric(t *testing.T) {
ifaFlag := pbsmetrics.IfaFlagNo
appVersion := "test_version"
os := "test_os"
geoFlag := pbsmetrics.GeoFlagNo
IPFlag := pbsmetrics.IPFlagNo

m.RecordRequest(pbsmetrics.Labels{
RType: requestType,
RequestStatus: requestStatus,
IfaFlag: ifaFlag,
AppVersion: appVersion,
OS: os,
GeoFlag: geoFlag,
IPFlag: IPFlag,
})

expectedCount := float64(1)
Expand All @@ -160,6 +164,8 @@ func TestRequestMetric(t *testing.T) {
ifaLabel: string(ifaFlag),
appVersionLabel: string(appVersion),
osLabel: string(os),
geoLabel: string(geoFlag),
IPLabel: string(IPFlag),
})
}

Expand Down
18 changes: 18 additions & 0 deletions pbsmetrics/prometheus/type_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ func ifaTypesAsString() []string {
return valuesAsString
}

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

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

func requestStatusesAsString() []string {
values := pbsmetrics.RequestStatuses()
valuesAsString := make([]string, len(values))
Expand Down