Skip to content

Commit 0dd1721

Browse files
authored
Merge pull request #73 from gojek/feat/connectionpool-metric
feat: add courier connection pooling metric
2 parents 5dc8fb5 + b941456 commit 0dd1721

5 files changed

Lines changed: 71 additions & 44 deletions

File tree

client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,8 @@ func (c *Client) ConnectTimeout() time.Duration {
407407
func (c *Client) AckTimeout() time.Duration {
408408
return c.options.ackTimeout
409409
}
410+
411+
// PoolSize returns the size of the connection pool configured for the client
412+
func (c *Client) PoolSize() int {
413+
return c.options.poolSize
414+
}

docs/docs/sdk/SDK.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Package courier contains the client that can be used to interact with the courie
2121
- [func \(c \*Client\) InfoHandler\(\) http.Handler](#Client.InfoHandler)
2222
- [func \(c \*Client\) IsConnected\(\) bool](#Client.IsConnected)
2323
- [func \(c \*Client\) KeepAlive\(\) time.Duration](#Client.KeepAlive)
24+
- [func \(c \*Client\) PoolSize\(\) int](#Client.PoolSize)
2425
- [func \(c \*Client\) Publish\(ctx context.Context, topic string, message interface\{\}, opts ...Option\) error](#Client.Publish)
2526
- [func \(c \*Client\) Run\(ctx context.Context\) error](#Client.Run)
2627
- [func \(c \*Client\) Start\(\) error](#Client.Start)
@@ -305,6 +306,15 @@ func (c *Client) KeepAlive() time.Duration
305306

306307
KeepAlive returns the keep alive duration configured for the client
307308

309+
<a name="Client.PoolSize"></a>
310+
### func \(\*Client\) [PoolSize](https://github.com/gojek/courier-go/blob/main/client.go#L412)
311+
312+
```go
313+
func (c *Client) PoolSize() int
314+
```
315+
316+
PoolSize returns the size of the connection pool configured for the client
317+
308318
<a name="Client.Publish"></a>
309319
### func \(\*Client\) [Publish](https://github.com/gojek/courier-go/blob/main/client_publish.go#L11)
310320

docs/docs/sdk/otelcourier.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ type BucketBoundaries struct {
102102
```
103103

104104
<a name="CourierConfig"></a>
105-
## type [CourierConfig](https://github.com/gojek/courier-go/blob/main/otelcourier/metric.go#L153-L158)
105+
## type [CourierConfig](https://github.com/gojek/courier-go/blob/main/otelcourier/metric.go#L153-L159)
106106

107107

108108

@@ -112,6 +112,7 @@ type CourierConfig interface {
112112
WriteTimeout() time.Duration
113113
KeepAlive() time.Duration
114114
AckTimeout() time.Duration
115+
PoolSize() int
115116
}
116117
```
117118

otelcourier/metric.go

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -155,58 +155,65 @@ type CourierConfig interface {
155155
WriteTimeout() time.Duration
156156
KeepAlive() time.Duration
157157
AckTimeout() time.Duration
158+
PoolSize() int
158159
}
159160

160161
func (t *OTel) initCourierConfig(c UseMiddleware) {
161-
baseAttrs := append([]attribute.KeyValue{
162-
attribute.String("service.name", t.service),
163-
}, t.attributes...)
164-
165-
ctx := context.Background()
166-
167162
cCfg, ok := c.(CourierConfig)
168163
if !ok {
169164
return
170165
}
171166

172-
connTimeout := cCfg.ConnectTimeout().Seconds()
173-
writeTimeout := cCfg.WriteTimeout().Seconds()
174-
keepAlive := cCfg.KeepAlive().Seconds()
175-
ackTimeout := cCfg.AckTimeout().Seconds()
167+
t.setupCourierMetrics(cCfg)
168+
}
176169

177-
connTimeoutGauge, err := t.meter.Float64UpDownCounter(
178-
"courier.client.connection_timeout",
179-
metric.WithDescription("MQTT connection timeout in seconds"),
180-
metric.WithUnit("s"),
181-
)
182-
if err != nil {
183-
panic(err)
184-
}
170+
func (t *OTel) setupCourierMetrics(cCfg CourierConfig) {
171+
baseAttrs := append([]attribute.KeyValue{
172+
attribute.String("service.name", t.service),
173+
}, t.attributes...)
185174

186-
connTimeoutGauge.Add(ctx, connTimeout, metric.WithAttributes(baseAttrs...))
175+
ctx := context.Background()
187176

188-
writeTimeoutGauge, err := t.meter.Float64UpDownCounter(
189-
"courier.client.write_timeout",
190-
metric.WithDescription("MQTT write timeout in seconds"),
191-
metric.WithUnit("s"),
192-
)
193-
if err != nil {
194-
panic(err)
177+
timeoutMetrics := []struct {
178+
name string
179+
description string
180+
value float64
181+
}{
182+
{
183+
name: "courier.client.connection_timeout",
184+
description: "MQTT connection timeout in seconds",
185+
value: cCfg.ConnectTimeout().Seconds(),
186+
},
187+
{
188+
name: "courier.client.write_timeout",
189+
description: "MQTT write timeout in seconds",
190+
value: cCfg.WriteTimeout().Seconds(),
191+
},
192+
{
193+
name: "courier.client.keep_alive",
194+
description: "MQTT keep alive in seconds",
195+
value: cCfg.KeepAlive().Seconds(),
196+
},
197+
{
198+
name: "courier.client.ack_timeout",
199+
description: "MQTT ack timeout in seconds",
200+
value: cCfg.AckTimeout().Seconds(),
201+
},
195202
}
196203

197-
writeTimeoutGauge.Add(ctx, writeTimeout, metric.WithAttributes(baseAttrs...))
204+
for _, tm := range timeoutMetrics {
205+
gauge, err := t.meter.Float64UpDownCounter(
206+
tm.name,
207+
metric.WithDescription(tm.description),
208+
metric.WithUnit("s"),
209+
)
210+
if err != nil {
211+
panic(err)
212+
}
198213

199-
keepAliveGauge, err := t.meter.Float64UpDownCounter(
200-
"courier.client.keep_alive",
201-
metric.WithDescription("MQTT keep alive in seconds"),
202-
metric.WithUnit("s"),
203-
)
204-
if err != nil {
205-
panic(err)
214+
gauge.Add(ctx, tm.value, metric.WithAttributes(baseAttrs...))
206215
}
207216

208-
keepAliveGauge.Add(ctx, keepAlive, metric.WithAttributes(baseAttrs...))
209-
210217
versionGauge, err := t.meter.Float64UpDownCounter(
211218
"courier.client.library_version",
212219
metric.WithDescription("Courier library version"),
@@ -216,17 +223,15 @@ func (t *OTel) initCourierConfig(c UseMiddleware) {
216223
}
217224

218225
versionAttrs := append(baseAttrs, attribute.String("version", courier.Version()))
219-
220226
versionGauge.Add(ctx, 1.0, metric.WithAttributes(versionAttrs...))
221227

222-
ackTimeoutGauge, err := t.meter.Float64UpDownCounter(
223-
"courier.client.ack_timeout",
224-
metric.WithDescription("MQTT ack timeout in seconds"),
225-
metric.WithUnit("s"),
228+
poolSizeGauge, err := t.meter.Float64UpDownCounter(
229+
"courier.client.pool_size",
230+
metric.WithDescription("Size of the MQTT connection pool"),
226231
)
227232
if err != nil {
228233
panic(err)
229234
}
230235

231-
ackTimeoutGauge.Add(ctx, ackTimeout, metric.WithAttributes(baseAttrs...))
236+
poolSizeGauge.Add(ctx, float64(cCfg.PoolSize()), metric.WithAttributes(baseAttrs...))
232237
}

otelcourier/metric_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func Test_courierConfigMetrics(t *testing.T) {
4646
courier.WithConnectTimeout(30),
4747
courier.WithKeepAlive(60),
4848
courier.WithWriteTimeout(30),
49+
courier.WithPoolSize(3),
4950
)
5051
require.NoError(t, err)
5152

@@ -68,14 +69,18 @@ courier_client_library_version{otel_scope_name="github.com/gojek/courier-go/otel
6869
# HELP courier_client_write_timeout_seconds MQTT write timeout in seconds
6970
# TYPE courier_client_write_timeout_seconds gauge
7071
courier_client_write_timeout_seconds{otel_scope_name="github.com/gojek/courier-go/otelcourier",otel_scope_version="semver:%s",service_name="test-service"} 3e-08
71-
`, vsn, vsn, vsn, vsn, vsn, vsn))
72+
# HELP courier_client_pool_size Size of the MQTT connection pool
73+
# TYPE courier_client_pool_size gauge
74+
courier_client_pool_size{otel_scope_name="github.com/gojek/courier-go/otelcourier",otel_scope_version="semver:%s",service_name="test-service"} 3
75+
`, vsn, vsn, vsn, vsn, vsn, vsn, vsn))
7276

7377
assert.NoError(t, testutil.GatherAndCompare(reg, buf,
7478
"courier_client_connection_timeout_seconds",
7579
"courier_client_write_timeout_seconds",
7680
"courier_client_keep_alive_seconds",
7781
"courier_client_ack_timeout_seconds",
7882
"courier_client_library_version",
83+
"courier_client_pool_size",
7984
))
8085
})
8186
}
@@ -102,6 +107,7 @@ func Test_courierConfigMetrics_NonCourierConfigType(t *testing.T) {
102107
assert.NotContains(t, metricName, "courier_client_keep_alive")
103108
assert.NotContains(t, metricName, "courier_client_ack_timeout")
104109
assert.NotContains(t, metricName, "courier_client_library_version")
110+
assert.NotContains(t, metricName, "courier_client_pool_size")
105111
}
106112
}
107113

0 commit comments

Comments
 (0)