Skip to content

Commit

Permalink
chore: update OTel metrics to v0.35.0 (#161)
Browse files Browse the repository at this point in the history
* chore: update OTel metrics to v0.35.0

* Update README.md

* test: updated OTel in tests

* test: bumped prometheus exporter as well

---------

Co-authored-by: Nhat <[email protected]>
  • Loading branch information
vgarvardt and nhatthm authored Jan 29, 2023
1 parent 8deff50 commit 3db42ad
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 159 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ Compatibility

| `otelsql` | `go.opentelemetry.io/otel/trace` | `go.opentelemetry.io/otel/sdk/metric` |
|:--------------------------:|:--------------------------------:|:-------------------------------------:|
| `v0.7.*` <sup>&nbsp;</sup> | `v1.11.1` ~> `latest` | `v0.33.0` ~> `latest` |
| `v0.8.*` <sup>&nbsp;</sup> | `v1.12.0` ~> `latest` | `v0.35.0` ~> `latest` |
| `v0.7.*` <sup>&nbsp;</sup> | `v1.11.1` ~> `latest` | `v0.33.0` ~> `v0.34.x` |
| `v0.6.*` <sup>&nbsp;</sup> | `v1.10.0` ~> `latest` | `v0.32.x` |
| `v0.5.*` <sup>&nbsp;</sup> | `v1.10.0` ~> `latest` | `v0.31.x` |
| `v0.4.*` <sup>1</sup> | `v1.9.0` ~> `latest` | `v0.31.x` |
Expand Down
8 changes: 4 additions & 4 deletions begin_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
func BenchmarkBeginStats(b *testing.B) {
meter := metric.NewNoopMeter()

histogram, err := meter.SyncFloat64().Histogram("latency_ms")
histogram, err := meter.Float64Histogram("latency_ms")
require.NoError(b, err)

count, err := meter.SyncInt64().Counter("calls")
count, err := meter.Int64Counter("calls")
require.NoError(b, err)

r := newMethodRecorder(histogram.Record, count.Add,
Expand Down Expand Up @@ -183,10 +183,10 @@ func TestBeginStats(t *testing.T) {
Run(t, func(s oteltest.SuiteContext) {
meter := s.MeterProvider().Meter("begin_test")

histogram, err := meter.SyncFloat64().Histogram(dbSQLClientLatencyMs)
histogram, err := meter.Float64Histogram(dbSQLClientLatencyMs)
require.NoError(t, err)

count, err := meter.SyncInt64().Counter(dbSQLClientCalls)
count, err := meter.Int64Counter(dbSQLClientCalls)
require.NoError(t, err)

r := newMethodRecorder(histogram.Record, count.Add,
Expand Down
4 changes: 2 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ func newConnConfig(opts driverOptions) connConfig {
traceWithSpanNameFormatter(opts.trace.spanNameFormatter),
)

latencyMsHistogram, err := meter.SyncFloat64().Histogram(dbSQLClientLatencyMs,
latencyMsHistogram, err := meter.Float64Histogram(dbSQLClientLatencyMs,
instrument.WithUnit(unit.Milliseconds),
instrument.WithDescription(`The distribution of latencies of various calls in milliseconds`),
)
handleErr(err)

callsCounter, err := meter.SyncInt64().Counter(dbSQLClientCalls,
callsCounter, err := meter.Int64Counter(dbSQLClientCalls,
instrument.WithUnit(unit.Dimensionless),
instrument.WithDescription(`The number of various calls of methods`),
)
Expand Down
8 changes: 4 additions & 4 deletions exec_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
func BenchmarkExecStats(b *testing.B) {
meter := metric.NewNoopMeter()

histogram, err := meter.SyncFloat64().Histogram("latency_ms")
histogram, err := meter.Float64Histogram("latency_ms")
require.NoError(b, err)

count, err := meter.SyncInt64().Counter("calls")
count, err := meter.Int64Counter("calls")
require.NoError(b, err)

r := newMethodRecorder(histogram.Record, count.Add,
Expand Down Expand Up @@ -157,10 +157,10 @@ func TestExecStats(t *testing.T) {
Run(t, func(s oteltest.SuiteContext) {
meter := s.MeterProvider().Meter("exec_test")

histogram, err := meter.SyncFloat64().Histogram(dbSQLClientLatencyMs)
histogram, err := meter.Float64Histogram(dbSQLClientLatencyMs)
require.NoError(t, err)

count, err := meter.SyncInt64().Counter(dbSQLClientCalls)
count, err := meter.Int64Counter(dbSQLClientCalls)
require.NoError(t, err)

r := newMethodRecorder(histogram.Record, count.Add,
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/stretchr/testify v1.8.1
github.com/swaggest/assertjson v1.7.0
go.opentelemetry.io/otel v1.11.2
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.34.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.11.2
go.opentelemetry.io/otel/metric v0.34.0
go.opentelemetry.io/otel/sdk v1.11.2
go.opentelemetry.io/otel/sdk/metric v0.34.0
go.opentelemetry.io/otel/trace v1.11.2
go.opentelemetry.io/otel v1.12.0
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.35.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.12.0
go.opentelemetry.io/otel/metric v0.35.0
go.opentelemetry.io/otel/sdk v1.12.0
go.opentelemetry.io/otel/sdk/metric v0.35.0
go.opentelemetry.io/otel/trace v1.12.0
)

require (
Expand Down
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FB
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 h1:BHyfKlQyqbsFN5p3IfnEUduWvb9is428/nNb5L3U01M=
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDfVJdfcVVdX+jpBxNmX4rDAzaS45IcYoM=
github.com/yudai/pp v2.0.1+incompatible h1:Q4//iY4pNF6yPLZIigmvcl7k/bPgrcTPIFIcmawg5bI=
go.opentelemetry.io/otel v1.11.2 h1:YBZcQlsVekzFsFbjygXMOXSs6pialIZxcjfO/mBDmR0=
go.opentelemetry.io/otel v1.11.2/go.mod h1:7p4EUV+AqgdlNV9gL97IgUZiVR3yrFXYo53f9BM3tRI=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.34.0 h1:O1E9/qhspQSz3O6/dSGLNBND2TO9mUaSvlhcKJMv278=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.34.0/go.mod h1:Id0oYi2ARij/um3gFV+t5rH1MTFdJpfTimsFsqKS7pE=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.11.2 h1:BhEVgvuE1NWLLuMLvC6sif791F45KFHi5GhOs1KunZU=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.11.2/go.mod h1:bx//lU66dPzNT+Y0hHA12ciKoMOH9iixEwCqC1OeQWQ=
go.opentelemetry.io/otel/metric v0.34.0 h1:MCPoQxcg/26EuuJwpYN1mZTeCYAUGx8ABxfW07YkjP8=
go.opentelemetry.io/otel/metric v0.34.0/go.mod h1:ZFuI4yQGNCupurTXCwkeD/zHBt+C2bR7bw5JqUm/AP8=
go.opentelemetry.io/otel/sdk v1.11.2 h1:GF4JoaEx7iihdMFu30sOyRx52HDHOkl9xQ8SMqNXUiU=
go.opentelemetry.io/otel/sdk v1.11.2/go.mod h1:wZ1WxImwpq+lVRo4vsmSOxdd+xwoUJ6rqyLc3SyX9aU=
go.opentelemetry.io/otel/sdk/metric v0.34.0 h1:7ElxfQpXCFZlRTvVRTkcUvK8Gt5DC8QzmzsLsO2gdzo=
go.opentelemetry.io/otel/sdk/metric v0.34.0/go.mod h1:l4r16BIqiqPy5rd14kkxllPy/fOI4tWo1jkpD9Z3ffQ=
go.opentelemetry.io/otel/trace v1.11.2 h1:Xf7hWSF2Glv0DE3MH7fBHvtpSBsjcBUe5MYAmZM/+y0=
go.opentelemetry.io/otel/trace v1.11.2/go.mod h1:4N+yC7QEz7TTsG9BSRLNAa63eg5E06ObSbKPmxQ/pKA=
go.opentelemetry.io/otel v1.12.0 h1:IgfC7kqQrRccIKuB7Cl+SRUmsKbEwSGPr0Eu+/ht1SQ=
go.opentelemetry.io/otel v1.12.0/go.mod h1:geaoz0L0r1BEOR81k7/n9W4TCXYCJ7bPO7K374jQHG0=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.35.0 h1:4ItSNqXDmuPiYGIENWD2jeJECQRXwWxb3/HoSfKsa6U=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.35.0/go.mod h1:k/PEzF9FAu3Do2qFOaqF6F5rVy7Z0idyYtWKSbS8vRo=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.12.0 h1:FXwvCIXsrMas/reQkSUTPZVCqud1yDy441acn4Fdu6w=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.12.0/go.mod h1:TTDkohPFSX4LMcbmNMYDh0hMXV8YigEjw5WwD+nDn6U=
go.opentelemetry.io/otel/metric v0.35.0 h1:aPT5jk/w7F9zW51L7WgRqNKDElBdyRLGuBtI5MX34e8=
go.opentelemetry.io/otel/metric v0.35.0/go.mod h1:qAcbhaTRFU6uG8QM7dDo7XvFsWcugziq/5YI065TokQ=
go.opentelemetry.io/otel/sdk v1.12.0 h1:8npliVYV7qc0t1FKdpU08eMnOjgPFMnriPhn0HH4q3o=
go.opentelemetry.io/otel/sdk v1.12.0/go.mod h1:WYcvtgquYvgODEvxOry5owO2y9MyciW7JqMz6cpXShE=
go.opentelemetry.io/otel/sdk/metric v0.35.0 h1:gryV4W5GzpOhKK48/lZb8ldyWIs3DRugSVlQZmCwELA=
go.opentelemetry.io/otel/sdk/metric v0.35.0/go.mod h1:eDyp1GxSiwV98kr7w4pzrszQh/eze9MqBqPd2bCPmyE=
go.opentelemetry.io/otel/trace v1.12.0 h1:p28in++7Kd0r2d8gSt931O57fdjUyWxkVbESuILAeUc=
go.opentelemetry.io/otel/trace v1.12.0/go.mod h1:pHlgBynn6s25qJ2szD+Bv+iwKJttjHSI3lUAyf0GNuQ=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
8 changes: 4 additions & 4 deletions ping_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
func BenchmarkPingStats(b *testing.B) {
meter := metric.NewNoopMeter()

histogram, err := meter.SyncFloat64().Histogram("latency_ms")
histogram, err := meter.Float64Histogram("latency_ms")
require.NoError(b, err)

count, err := meter.SyncInt64().Counter("calls")
count, err := meter.Int64Counter("calls")
require.NoError(b, err)

r := newMethodRecorder(histogram.Record, count.Add,
Expand Down Expand Up @@ -144,10 +144,10 @@ func TestPingStats(t *testing.T) {
Run(t, func(s oteltest.SuiteContext) {
meter := s.MeterProvider().Meter("ping_test")

histogram, err := meter.SyncFloat64().Histogram(dbSQLClientLatencyMs)
histogram, err := meter.Float64Histogram(dbSQLClientLatencyMs)
require.NoError(t, err)

count, err := meter.SyncInt64().Counter(dbSQLClientCalls)
count, err := meter.Int64Counter(dbSQLClientCalls)
require.NoError(t, err)

r := newMethodRecorder(histogram.Record, count.Add,
Expand Down
8 changes: 4 additions & 4 deletions prepare_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
func BenchmarkPrepareStats(b *testing.B) {
meter := metric.NewNoopMeter()

histogram, err := meter.SyncFloat64().Histogram("latency_ms")
histogram, err := meter.Float64Histogram("latency_ms")
require.NoError(b, err)

count, err := meter.SyncInt64().Counter("calls")
count, err := meter.Int64Counter("calls")
require.NoError(b, err)

r := newMethodRecorder(histogram.Record, count.Add,
Expand Down Expand Up @@ -183,10 +183,10 @@ func TestPrepareStats(t *testing.T) {
Run(t, func(s oteltest.SuiteContext) {
meter := s.MeterProvider().Meter("prepare_test")

histogram, err := meter.SyncFloat64().Histogram(dbSQLClientLatencyMs)
histogram, err := meter.Float64Histogram(dbSQLClientLatencyMs)
require.NoError(t, err)

count, err := meter.SyncInt64().Counter(dbSQLClientCalls)
count, err := meter.Int64Counter(dbSQLClientCalls)
require.NoError(t, err)

r := newMethodRecorder(histogram.Record, count.Add,
Expand Down
8 changes: 4 additions & 4 deletions query_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
func BenchmarkQueryStats(b *testing.B) {
meter := metric.NewNoopMeter()

histogram, err := meter.SyncFloat64().Histogram("latency_ms")
histogram, err := meter.Float64Histogram("latency_ms")
require.NoError(b, err)

count, err := meter.SyncInt64().Counter("calls")
count, err := meter.Int64Counter("calls")
require.NoError(b, err)

r := newMethodRecorder(histogram.Record, count.Add,
Expand Down Expand Up @@ -157,10 +157,10 @@ func TestQueryStats(t *testing.T) {
Run(t, func(s oteltest.SuiteContext) {
meter := s.MeterProvider().Meter("query_test")

histogram, err := meter.SyncFloat64().Histogram(dbSQLClientLatencyMs)
histogram, err := meter.Float64Histogram(dbSQLClientLatencyMs)
require.NoError(t, err)

count, err := meter.SyncInt64().Counter(dbSQLClientCalls)
count, err := meter.Int64Counter(dbSQLClientCalls)
require.NoError(t, err)

r := newMethodRecorder(histogram.Record, count.Add,
Expand Down
68 changes: 35 additions & 33 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/asyncfloat64"
"go.opentelemetry.io/otel/metric/instrument/asyncint64"
"go.opentelemetry.io/otel/metric/unit"
)

Expand Down Expand Up @@ -55,13 +53,13 @@ func recordStats(
var (
err error

openConnections asyncint64.Gauge
idleConnections asyncint64.Gauge
activeConnections asyncint64.Gauge
waitCount asyncint64.Gauge
waitDuration asyncfloat64.Gauge
idleClosed asyncint64.Gauge
lifetimeClosed asyncint64.Gauge
openConnections instrument.Int64ObservableGauge
idleConnections instrument.Int64ObservableGauge
activeConnections instrument.Int64ObservableGauge
waitCount instrument.Int64ObservableGauge
waitDuration instrument.Float64ObservableGauge
idleClosed instrument.Int64ObservableGauge
lifetimeClosed instrument.Int64ObservableGauge

dbStats sql.DBStats
lastDBStats time.Time
Expand All @@ -73,64 +71,56 @@ func recordStats(
lock.Lock()
defer lock.Unlock()

openConnections, err = meter.AsyncInt64().Gauge(
openConnections, err = meter.Int64ObservableGauge(
dbSQLConnectionsOpen,
instrument.WithUnit(unit.Dimensionless),
instrument.WithDescription("Count of open connections in the pool"),
)
otel.Handle(err)

idleConnections, err = meter.AsyncInt64().Gauge(
idleConnections, err = meter.Int64ObservableGauge(
dbSQLConnectionsIdle,
instrument.WithUnit(unit.Dimensionless),
instrument.WithDescription("Count of idle connections in the pool"),
)
otel.Handle(err)

activeConnections, err = meter.AsyncInt64().Gauge(
activeConnections, err = meter.Int64ObservableGauge(
dbSQLConnectionsActive,
instrument.WithUnit(unit.Dimensionless),
instrument.WithDescription("Count of active connections in the pool"),
)
otel.Handle(err)

waitCount, err = meter.AsyncInt64().Gauge(
waitCount, err = meter.Int64ObservableGauge(
dbSQLConnectionsWaitCount,
instrument.WithUnit(unit.Dimensionless),
instrument.WithDescription("The total number of connections waited for"),
)
otel.Handle(err)

waitDuration, err = meter.AsyncFloat64().Gauge(
waitDuration, err = meter.Float64ObservableGauge(
dbSQLConnectionsWaitDuration,
instrument.WithUnit(unit.Milliseconds),
instrument.WithDescription("The total time blocked waiting for a new connection"),
)
otel.Handle(err)

idleClosed, err = meter.AsyncInt64().Gauge(
idleClosed, err = meter.Int64ObservableGauge(
dbSQLConnectionsIdleClosed,
instrument.WithUnit(unit.Dimensionless),
instrument.WithDescription("The total number of connections closed due to SetMaxIdleConns"),
)
otel.Handle(err)

lifetimeClosed, err = meter.AsyncInt64().Gauge(
lifetimeClosed, err = meter.Int64ObservableGauge(
dbSQLConnectionsLifetimeClosed,
instrument.WithUnit(unit.Dimensionless),
instrument.WithDescription("The total number of connections closed due to SetConnMaxLifetime"),
)
otel.Handle(err)

return meter.RegisterCallback([]instrument.Asynchronous{
openConnections,
idleConnections,
activeConnections,
waitCount,
waitDuration,
idleClosed,
lifetimeClosed,
}, func(ctx context.Context) {
_, err = meter.RegisterCallback(func(ctx context.Context, obs metric.Observer) error {
lock.Lock()
defer lock.Unlock()

Expand All @@ -140,12 +130,24 @@ func recordStats(
lastDBStats = now
}

openConnections.Observe(ctx, int64(dbStats.OpenConnections), attrs...)
idleConnections.Observe(ctx, int64(dbStats.Idle), attrs...)
activeConnections.Observe(ctx, int64(dbStats.InUse), attrs...)
waitCount.Observe(ctx, dbStats.WaitCount, attrs...)
waitDuration.Observe(ctx, float64(dbStats.WaitDuration.Nanoseconds())/1e6, attrs...)
idleClosed.Observe(ctx, dbStats.MaxIdleClosed, attrs...)
lifetimeClosed.Observe(ctx, dbStats.MaxLifetimeClosed, attrs...)
})
obs.ObserveInt64(openConnections, int64(dbStats.OpenConnections), attrs...)
obs.ObserveInt64(idleConnections, int64(dbStats.Idle), attrs...)
obs.ObserveInt64(activeConnections, int64(dbStats.InUse), attrs...)
obs.ObserveInt64(waitCount, dbStats.WaitCount, attrs...)
obs.ObserveFloat64(waitDuration, float64(dbStats.WaitDuration.Nanoseconds())/1e6, attrs...)
obs.ObserveInt64(idleClosed, dbStats.MaxIdleClosed, attrs...)
obs.ObserveInt64(lifetimeClosed, dbStats.MaxLifetimeClosed, attrs...)

return nil
},
openConnections,
idleConnections,
activeConnections,
waitCount,
waitDuration,
idleClosed,
lifetimeClosed,
)

return err
}
12 changes: 6 additions & 6 deletions tests/mssql/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ require (
go.nhat.io/otelsql v0.1.0 // indirect
go.nhat.io/testcontainers-registry v0.11.0 // indirect
go.nhat.io/timeparser v0.3.0 // indirect
go.opentelemetry.io/otel v1.11.2 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.34.0 // indirect
go.opentelemetry.io/otel/metric v0.34.0 // indirect
go.opentelemetry.io/otel/sdk v1.11.2 // indirect
go.opentelemetry.io/otel/sdk/metric v0.34.0 // indirect
go.opentelemetry.io/otel/trace v1.11.2 // indirect
go.opentelemetry.io/otel v1.12.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.35.0 // indirect
go.opentelemetry.io/otel/metric v0.35.0 // indirect
go.opentelemetry.io/otel/sdk v1.12.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.35.0 // indirect
go.opentelemetry.io/otel/trace v1.12.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/mod v0.7.0 // indirect
Expand Down
Loading

0 comments on commit 3db42ad

Please sign in to comment.