Skip to content

Commit 01daa2c

Browse files
authored
Metrics: Record HTTP connections wanted and obtained (#4518)
1 parent 689c5c8 commit 01daa2c

File tree

9 files changed

+86
-0
lines changed

9 files changed

+86
-0
lines changed

exchange/bidder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ func (bidder *BidderAdapter) addClientTrace(ctx context.Context) context.Context
735735
// GetConn is called before a connection is created or retrieved from an idle pool
736736
GetConn: func(hostPort string) {
737737
connStart = time.Now()
738+
bidder.me.RecordConnectionWant()
738739
},
739740
// GotConn is called after a successful connection is obtained
740741
GotConn: func(info httptrace.GotConnInfo) {
@@ -752,6 +753,7 @@ func (bidder *BidderAdapter) addClientTrace(ctx context.Context) context.Context
752753
}
753754

754755
bidder.me.RecordAdapterConnections(bidder.BidderName, info.Reused, connWaitTime)
756+
bidder.me.RecordConnectionGot()
755757
},
756758
// DNSStart is called when a DNS lookup begins.
757759
DNSStart: func(info httptrace.DNSStartInfo) {

exchange/bidder_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,6 +2085,8 @@ func TestCallRecordAdapterConnections(t *testing.T) {
20852085
mockMetricEngine.On("RecordAdapterConnections", expectedAdapterName, false, mock.MatchedBy(compareConnWaitTime)).Once()
20862086
mockMetricEngine.On("RecordOverheadTime", metrics.PreBidder, mock.Anything).Once()
20872087
mockMetricEngine.On("RecordBidderServerResponseTime", mock.Anything).Once()
2088+
mockMetricEngine.On("RecordConnectionWant").Once()
2089+
mockMetricEngine.On("RecordConnectionGot").Once()
20882090

20892091
// Run requestBid using an http.Client with a mock handler
20902092
bidder := AdaptBidder(bidderImpl, server.Client(), &config.Configuration{}, mockMetricEngine, openrtb_ext.BidderAppnexus, nil, "")

metrics/config/metrics.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,18 @@ func (me *MultiMetricsEngine) RecordAdapterThrottled(adapter openrtb_ext.BidderN
384384
}
385385
}
386386

387+
func (me *MultiMetricsEngine) RecordConnectionWant() {
388+
for _, thisME := range *me {
389+
thisME.RecordConnectionWant()
390+
}
391+
}
392+
393+
func (me *MultiMetricsEngine) RecordConnectionGot() {
394+
for _, thisME := range *me {
395+
thisME.RecordConnectionGot()
396+
}
397+
}
398+
387399
// NilMetricsEngine implements the MetricsEngine interface where no metrics are actually captured. This is
388400
// used if no metric backend is configured and also for tests.
389401
type NilMetricsEngine struct{}
@@ -566,3 +578,9 @@ func (me *NilMetricsEngine) RecordModuleTimeout(labels metrics.ModuleLabels) {
566578
// RecordAdapterThrottled as a noop
567579
func (me *NilMetricsEngine) RecordAdapterThrottled(adapter openrtb_ext.BidderName) {
568580
}
581+
582+
func (me *NilMetricsEngine) RecordConnectionWant() {
583+
}
584+
585+
func (me *NilMetricsEngine) RecordConnectionGot() {
586+
}

metrics/go_metrics.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type Metrics struct {
1818
TMaxTimeoutCounter metrics.Counter
1919
ConnectionAcceptErrorMeter metrics.Meter
2020
ConnectionCloseErrorMeter metrics.Meter
21+
ConnectionWantCounter metrics.Counter
22+
ConnectionGotCounter metrics.Counter
2123
ImpMeter metrics.Meter
2224
AppRequestMeter metrics.Meter
2325
NoCookieMeter metrics.Meter
@@ -160,6 +162,8 @@ func NewBlankMetrics(registry metrics.Registry, exchanges []string, disabledMetr
160162
ConnectionCounter: metrics.NilCounter{},
161163
ConnectionAcceptErrorMeter: blankMeter,
162164
ConnectionCloseErrorMeter: blankMeter,
165+
ConnectionWantCounter: metrics.NilCounter{},
166+
ConnectionGotCounter: metrics.NilCounter{},
163167
ImpMeter: blankMeter,
164168
AppRequestMeter: blankMeter,
165169
DebugRequestMeter: blankMeter,
@@ -287,6 +291,8 @@ func NewMetrics(registry metrics.Registry, exchanges []openrtb_ext.BidderName, d
287291
newMetrics.TMaxTimeoutCounter = metrics.GetOrRegisterCounter("tmax_timeout", registry)
288292
newMetrics.ConnectionAcceptErrorMeter = metrics.GetOrRegisterMeter("connection_accept_errors", registry)
289293
newMetrics.ConnectionCloseErrorMeter = metrics.GetOrRegisterMeter("connection_close_errors", registry)
294+
newMetrics.ConnectionWantCounter = metrics.GetOrRegisterCounter("connection_want", registry)
295+
newMetrics.ConnectionGotCounter = metrics.GetOrRegisterCounter("connection_got", registry)
290296
newMetrics.ImpMeter = metrics.GetOrRegisterMeter("imps_requested", registry)
291297

292298
newMetrics.ImpsTypeBanner = metrics.GetOrRegisterMeter("imp_banner", registry)
@@ -670,6 +676,14 @@ func (me *Metrics) RecordConnectionClose(success bool) {
670676
}
671677
}
672678

679+
func (me *Metrics) RecordConnectionWant() {
680+
me.ConnectionWantCounter.Inc(1)
681+
}
682+
683+
func (me *Metrics) RecordConnectionGot() {
684+
me.ConnectionGotCounter.Inc(1)
685+
}
686+
673687
// RecordRequestTime implements a part of the MetricsEngine interface. The calling code is responsible
674688
// for determining the call duration.
675689
func (me *Metrics) RecordRequestTime(labels Labels, length time.Duration) {

metrics/go_metrics_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func TestNewMetrics(t *testing.T) {
8686
ensureContains(t, registry, "request_over_head_time.make-bidder-requests", m.OverheadTimer[MakeBidderRequests])
8787
ensureContains(t, registry, "bidder_server_response_time_seconds", m.BidderServerResponseTimer)
8888
ensureContains(t, registry, "tmax_timeout", m.TMaxTimeoutCounter)
89+
ensureContains(t, registry, "connection_want", m.ConnectionWantCounter)
90+
ensureContains(t, registry, "connection_got", m.ConnectionGotCounter)
8991

9092
for module, stages := range moduleStageNames {
9193
for _, stage := range stages {

metrics/metrics.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,6 @@ type MetricsEngine interface {
474474
RecordModuleExecutionError(labels ModuleLabels)
475475
RecordModuleTimeout(labels ModuleLabels)
476476
RecordAdapterThrottled(adapterName openrtb_ext.BidderName)
477+
RecordConnectionWant()
478+
RecordConnectionGot()
477479
}

metrics/metrics_mock.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,11 @@ func (me *MetricsEngineMock) RecordModuleTimeout(labels ModuleLabels) {
234234
func (me *MetricsEngineMock) RecordAdapterThrottled(adapterName openrtb_ext.BidderName) {
235235
me.Called(adapterName)
236236
}
237+
238+
func (me *MetricsEngineMock) RecordConnectionWant() {
239+
me.Called()
240+
}
241+
242+
func (me *MetricsEngineMock) RecordConnectionGot() {
243+
me.Called()
244+
}

metrics/prometheus/prometheus.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type Metrics struct {
2323
connectionsClosed prometheus.Counter
2424
connectionsError *prometheus.CounterVec
2525
connectionsOpened prometheus.Counter
26+
connectionWant prometheus.Counter
27+
connectionGot prometheus.Counter
2628
cookieSync *prometheus.CounterVec
2729
setUid *prometheus.CounterVec
2830
impressions *prometheus.CounterVec
@@ -187,6 +189,14 @@ func NewMetrics(cfg config.PrometheusMetrics, disabledMetrics config.DisabledMet
187189
"connections_opened",
188190
"Count of successful connections opened to Prebid Server.")
189191

192+
metrics.connectionWant = newCounterWithoutLabels(cfg, reg,
193+
"connections_want",
194+
"Count number of times client trace calls GetConn.")
195+
196+
metrics.connectionGot = newCounterWithoutLabels(cfg, reg,
197+
"connections_got",
198+
"Count number of times client trace calls GotConn.")
199+
190200
metrics.tmaxTimeout = newCounterWithoutLabels(cfg, reg,
191201
"tmax_timeout",
192202
"Count of requests rejected due to Tmax timeout exceed.")
@@ -1110,3 +1120,11 @@ func (m *Metrics) RecordAdapterThrottled(adapterName openrtb_ext.BidderName) {
11101120
adapterLabel: strings.ToLower(string(adapterName)),
11111121
}).Inc()
11121122
}
1123+
1124+
func (m *Metrics) RecordConnectionWant() {
1125+
m.connectionWant.Inc()
1126+
}
1127+
1128+
func (m *Metrics) RecordConnectionGot() {
1129+
m.connectionGot.Inc()
1130+
}

metrics/prometheus/prometheus_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func TestConnectionMetrics(t *testing.T) {
7575
expectedOpenedErrorCount float64
7676
expectedClosedCount float64
7777
expectedClosedErrorCount float64
78+
expectedConnectionWant float64
79+
expectedConnectionGot float64
7880
}{
7981
{
8082
description: "Open Success",
@@ -116,6 +118,20 @@ func TestConnectionMetrics(t *testing.T) {
116118
expectedClosedCount: 0,
117119
expectedClosedErrorCount: 1,
118120
},
121+
{
122+
description: "connection-want",
123+
testCase: func(m *Metrics) {
124+
m.RecordConnectionWant()
125+
},
126+
expectedConnectionWant: 1,
127+
},
128+
{
129+
description: "connection-got",
130+
testCase: func(m *Metrics) {
131+
m.RecordConnectionGot()
132+
},
133+
expectedConnectionGot: 1,
134+
},
119135
}
120136

121137
for _, test := range testCases {
@@ -135,6 +151,10 @@ func TestConnectionMetrics(t *testing.T) {
135151
test.expectedClosedErrorCount, prometheus.Labels{
136152
connectionErrorLabel: connectionCloseError,
137153
})
154+
assertCounterValue(t, test.description, "connectionWant", m.connectionWant,
155+
test.expectedConnectionWant)
156+
assertCounterValue(t, test.description, "connectionGot", m.connectionGot,
157+
test.expectedConnectionGot)
138158
}
139159
}
140160

0 commit comments

Comments
 (0)