Skip to content

Commit

Permalink
Bump otel dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
nhatthm committed Oct 13, 2022
1 parent c2c7488 commit 234ed02
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 252 deletions.
5 changes: 5 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const _maxDriver = 150

const instrumentationName = "go.nhat.io/otelsql"

const (
dbSQLClientLatencyMs = "db.sql.client.latency"
dbSQLClientCalls = "db.sql.client.calls"
)

var regMu sync.Mutex

// Register initializes and registers our otelsql wrapped database driver identified by its driverName and using provided
Expand Down
16 changes: 8 additions & 8 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.0
github.com/swaggest/assertjson v1.7.0
go.opentelemetry.io/otel v1.10.0
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.32.1
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.10.0
go.opentelemetry.io/otel/metric v0.32.1
go.opentelemetry.io/otel/sdk v1.10.0
go.opentelemetry.io/otel/sdk/metric v0.32.1
go.opentelemetry.io/otel/trace v1.10.0
go.opentelemetry.io/otel v1.11.0
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.32.3
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.11.0
go.opentelemetry.io/otel/metric v0.32.3
go.opentelemetry.io/otel/sdk v1.11.0
go.opentelemetry.io/otel/sdk/metric v0.32.3
go.opentelemetry.io/otel/trace v1.11.0
)

require (
Expand All @@ -29,7 +29,7 @@ require (
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
32 changes: 16 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,26 @@ 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.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4=
go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.32.1 h1:UD2Uaao2esCmjbWNjvSPCLzA9YynppA+Ue4XdCQUcRM=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.32.1/go.mod h1:eblT5A0Sq/6aWPVBg33YQM0YsqEHrNK+amNhsAsgIMo=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.10.0 h1:c9UtMu/qnbLlVwTwt+ABrURrioEruapIslTDYZHJe2w=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.10.0/go.mod h1:h3Lrh9t3Dnqp3NPwAZx7i37UFX7xrfnO1D+fuClREOA=
go.opentelemetry.io/otel/metric v0.32.1 h1:ftff5LSBCIDwL0UkhBuDg8j9NNxx2IusvJ18q9h6RC4=
go.opentelemetry.io/otel/metric v0.32.1/go.mod h1:iLPP7FaKMAD5BIxJ2VX7f2KTuz//0QK2hEUyti5psqQ=
go.opentelemetry.io/otel/sdk v1.10.0 h1:jZ6K7sVn04kk/3DNUdJ4mqRlGDiXAVuIG+MMENpTNdY=
go.opentelemetry.io/otel/sdk v1.10.0/go.mod h1:vO06iKzD5baltJz1zarxMCNHFpUlUiOy4s65ECtn6kE=
go.opentelemetry.io/otel/sdk/metric v0.32.1 h1:S6AqzulzGQl+sTpYeAoVLw1SJbc2LYuKCMUmfEKG+zM=
go.opentelemetry.io/otel/sdk/metric v0.32.1/go.mod h1:Nn+Nt/7cKzm5ISmvLzNO5RLf0Xuv8/Qo8fkpr0JDOzs=
go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E=
go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM=
go.opentelemetry.io/otel v1.11.0 h1:kfToEGMDq6TrVrJ9Vht84Y8y9enykSZzDDZglV0kIEk=
go.opentelemetry.io/otel v1.11.0/go.mod h1:H2KtuEphyMvlhZ+F7tg9GRhAOe60moNx61Ex+WmiKkk=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.32.3 h1:PZwuZa1NTkJUzjeBNVLcOTrzkrV7JuCUaDqUFOuiyVg=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.32.3/go.mod h1:yfe3XB569YeNtp73shyUNwaaA+5pdH9d/yAKLA2vSbk=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.11.0 h1:rzpQkvma82S+jQvJHqJaAGQdeRBtH6HASrgrZa45rx4=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.11.0/go.mod h1:nMt8nBu01qC+8LfJu4puk/OYHovohkISNuy/MMG8yRk=
go.opentelemetry.io/otel/metric v0.32.3 h1:dMpnJYk2KULXr0j8ph6N7+IcuiIQXlPXD4kix9t7L9c=
go.opentelemetry.io/otel/metric v0.32.3/go.mod h1:pgiGmKohxHyTPHGOff+vrtIH39/R9fiO/WoenUQ3kcc=
go.opentelemetry.io/otel/sdk v1.11.0 h1:ZnKIL9V9Ztaq+ME43IUi/eo22mNsb6a7tGfzaOWB5fo=
go.opentelemetry.io/otel/sdk v1.11.0/go.mod h1:REusa8RsyKaq0OlyangWXaw97t2VogoO4SSEeKkSTAk=
go.opentelemetry.io/otel/sdk/metric v0.32.3 h1:lY46wXBbo8IuPDlh1fpVPVy/bCT4wwo3RBYve6UaHOA=
go.opentelemetry.io/otel/sdk/metric v0.32.3/go.mod h1:nqJPheSpNDSGXhg22BQRgTQedRalfei6tZkmqTavDSk=
go.opentelemetry.io/otel/trace v1.11.0 h1:20U/Vj42SX+mASlXLmSGBg6jpI1jQtv682lZtTAOVFI=
go.opentelemetry.io/otel/trace v1.11.0/go.mod h1:nyYjis9jy0gytE9LXGU+/m1sHTKbRY0fX0hulNNDP1U=
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=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 h1:OK7RB6t2WQX54srQQYSXMW8dF5C6/8+oA/s5QBmmto4=
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
14 changes: 0 additions & 14 deletions metric.go

This file was deleted.

60 changes: 32 additions & 28 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
Expand All @@ -18,6 +19,16 @@ import (
// defaultMinimumReadDBStatsInterval is the default minimum interval between calls to db.Stats().
const defaultMinimumReadDBStatsInterval = time.Second

const (
dbSQLConnectionsOpen = "db.sql.connections.open"
dbSQLConnectionsIdle = "db.sql.connections.idle"
dbSQLConnectionsActive = "db.sql.connections.active"
dbSQLConnectionsWaitCount = "db.sql.connections.wait_count"
dbSQLConnectionsWaitDuration = "db.sql.connections.wait_duration"
dbSQLConnectionsIdleClosed = "db.sql.connections.idle_closed"
dbSQLConnectionsLifetimeClosed = "db.sql.connections.lifetime_closed"
)

// RecordStats records database statistics for provided sql.DB at the provided interval.
func RecordStats(db *sql.DB, opts ...StatsOption) error {
o := statsOptions{
Expand Down Expand Up @@ -62,61 +73,54 @@ func recordStats(
lock.Lock()
defer lock.Unlock()

if openConnections, err = meter.AsyncInt64().Gauge(
openConnections, err = meter.AsyncInt64().Gauge(
dbSQLConnectionsOpen,
instrument.WithUnit(unit.Dimensionless),
instrument.WithDescription("Count of open connections in the pool"),
); err != nil {
return err
}
)
otel.Handle(err)

if idleConnections, err = meter.AsyncInt64().Gauge(
idleConnections, err = meter.AsyncInt64().Gauge(
dbSQLConnectionsIdle,
instrument.WithUnit(unit.Dimensionless),
instrument.WithDescription("Count of idle connections in the pool"),
); err != nil {
return err
}
)
otel.Handle(err)

if activeConnections, err = meter.AsyncInt64().Gauge(
activeConnections, err = meter.AsyncInt64().Gauge(
dbSQLConnectionsActive,
instrument.WithUnit(unit.Dimensionless),
instrument.WithDescription("Count of active connections in the pool"),
); err != nil {
return err
}
)
otel.Handle(err)

if waitCount, err = meter.AsyncInt64().Gauge(
waitCount, err = meter.AsyncInt64().Gauge(
dbSQLConnectionsWaitCount,
instrument.WithUnit(unit.Dimensionless),
instrument.WithDescription("The total number of connections waited for"),
); err != nil {
return err
}
)
otel.Handle(err)

if waitDuration, err = meter.AsyncFloat64().Gauge(
waitDuration, err = meter.AsyncFloat64().Gauge(
dbSQLConnectionsWaitDuration,
instrument.WithUnit(unit.Milliseconds),
instrument.WithDescription("The total time blocked waiting for a new connection"),
); err != nil {
return err
}
)
otel.Handle(err)

if idleClosed, err = meter.AsyncInt64().Gauge(
idleClosed, err = meter.AsyncInt64().Gauge(
dbSQLConnectionsIdleClosed,
instrument.WithUnit(unit.Dimensionless),
instrument.WithDescription("The total number of connections closed due to SetMaxIdleConns"),
); err != nil {
return err
}
)
otel.Handle(err)

if lifetimeClosed, err = meter.AsyncInt64().Gauge(
lifetimeClosed, err = meter.AsyncInt64().Gauge(
dbSQLConnectionsLifetimeClosed,
instrument.WithUnit(unit.Dimensionless),
instrument.WithDescription("The total number of connections closed due to SetConnMaxLifetime"),
); err != nil {
return err
}
)
otel.Handle(err)

return meter.RegisterCallback([]instrument.Asynchronous{
openConnections,
Expand Down
46 changes: 0 additions & 46 deletions stats_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package otelsql_test

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/unit"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"

"go.nhat.io/otelsql"
"go.nhat.io/otelsql/internal/test/oteltest"
"go.nhat.io/otelsql/internal/test/sqlmock"
)

const instrumentationName = "go.nhat.io/otelsql"

func TestRecordStats(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -46,46 +40,6 @@ func TestRecordStats(t *testing.T) {
})
}

func TestRecordStats_Error(t *testing.T) {
t.Parallel()

testCases := []struct {
metric string
unit unit.Unit
}{
{metric: "db.sql.connections.open", unit: unit.Dimensionless},
{metric: "db.sql.connections.idle", unit: unit.Dimensionless},
{metric: "db.sql.connections.active", unit: unit.Dimensionless},
{metric: "db.sql.connections.wait_count", unit: unit.Dimensionless},
{metric: "db.sql.connections.wait_duration", unit: unit.Milliseconds},
{metric: "db.sql.connections.idle_closed", unit: unit.Dimensionless},
{metric: "db.sql.connections.lifetime_closed", unit: unit.Dimensionless},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.metric, func(t *testing.T) {
t.Parallel()

oteltest.New().
Run(t, func(sc oteltest.SuiteContext) {
db, err := newDB(sc.DatabaseDSN())
require.NoError(t, err)

_, err = sc.MeterProvider().Meter(instrumentationName).AsyncInt64().UpDownCounter(tc.metric, instrument.WithUnit(tc.unit))
require.NoError(t, err)

err = otelsql.RecordStats(db,
otelsql.WithMeterProvider(sc.MeterProvider()),
)
expected := fmt.Sprintf(`instrument already registered: name %s`, tc.metric)

assert.ErrorContains(t, err, expected)
})
})
}
}

func expectedStatsMetric() string {
return expectedMetricsFromFile("stats.json")
}
22 changes: 11 additions & 11 deletions tests/mssql/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ require (
github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc1 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
github.com/opencontainers/runc v1.1.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -87,20 +87,20 @@ require (
go.nhat.io/testcontainers-registry v0.8.0 // indirect
go.nhat.io/timeparser v0.3.0 // indirect
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/otel v1.10.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.32.1 // indirect
go.opentelemetry.io/otel/metric v0.32.1 // indirect
go.opentelemetry.io/otel/sdk v1.10.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.32.1 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect
go.opentelemetry.io/otel v1.11.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.32.3 // indirect
go.opentelemetry.io/otel/metric v0.32.3 // indirect
go.opentelemetry.io/otel/sdk v1.11.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.32.3 // indirect
go.opentelemetry.io/otel/trace v1.11.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220923203811-8be639271d50 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/net v0.0.0-20221012135044-0b7e1fb9d458 // indirect
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/genproto v0.0.0-20220923205249-dd2d53f1fffc // indirect
google.golang.org/grpc v1.49.0 // indirect
google.golang.org/genproto v0.0.0-20221010155953-15ba04fc1c0e // indirect
google.golang.org/grpc v1.50.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
Loading

0 comments on commit 234ed02

Please sign in to comment.