Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
XSAM committed Mar 26, 2022
1 parent 5c0084e commit 55851ae
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 47 deletions.
88 changes: 46 additions & 42 deletions sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,51 +105,55 @@ func RegisterDBStatsMetrics(db *sql.DB, dbSystem string, opts ...Option) error {
}, func(ctx context.Context) {
dbStats := db.Stats()

instruments.connectionMaxOpen.Observe(
ctx,
int64(dbStats.MaxOpenConnections),
cfg.Attributes...,
)

instruments.connectionOpen.Observe(
ctx,
int64(dbStats.InUse),
append(cfg.Attributes, connectionStatusKey.String("inuse"))...,
)
instruments.connectionOpen.Observe(
ctx,
int64(dbStats.Idle),
append(cfg.Attributes, connectionStatusKey.String("idle"))...,
)

instruments.connectionWaitTotal.Observe(
ctx,
dbStats.WaitCount,
cfg.Attributes...,
)
instruments.connectionWaitDurationTotal.Observe(
ctx,
float64(dbStats.WaitDuration.Nanoseconds())/1e6,
cfg.Attributes...,
)
instruments.connectionClosedMaxIdleTotal.Observe(
ctx,
dbStats.MaxIdleClosed,
cfg.Attributes...,
)
instruments.connectionClosedMaxIdleTimeTotal.Observe(
ctx,
dbStats.MaxIdleTimeClosed,
cfg.Attributes...,
)
instruments.connectionClosedMaxLifetimeTotal.Observe(
ctx,
dbStats.MaxLifetimeClosed,
cfg.Attributes...,
)
recordDBStatsMetrics(ctx, dbStats, instruments, cfg)
})
if err != nil {
return err
}
return nil
}

func recordDBStatsMetrics(ctx context.Context, dbStats sql.DBStats, instruments *dbStatsInstruments, cfg config) {
instruments.connectionMaxOpen.Observe(
ctx,
int64(dbStats.MaxOpenConnections),
cfg.Attributes...,
)

instruments.connectionOpen.Observe(
ctx,
int64(dbStats.InUse),
append(cfg.Attributes, connectionStatusKey.String("inuse"))...,
)
instruments.connectionOpen.Observe(
ctx,
int64(dbStats.Idle),
append(cfg.Attributes, connectionStatusKey.String("idle"))...,
)

instruments.connectionWaitTotal.Observe(
ctx,
dbStats.WaitCount,
cfg.Attributes...,
)
instruments.connectionWaitDurationTotal.Observe(
ctx,
float64(dbStats.WaitDuration.Nanoseconds())/1e6,
cfg.Attributes...,
)
instruments.connectionClosedMaxIdleTotal.Observe(
ctx,
dbStats.MaxIdleClosed,
cfg.Attributes...,
)
instruments.connectionClosedMaxIdleTimeTotal.Observe(
ctx,
dbStats.MaxIdleTimeClosed,
cfg.Attributes...,
)
instruments.connectionClosedMaxLifetimeTotal.Observe(
ctx,
dbStats.MaxLifetimeClosed,
cfg.Attributes...,
)
}
40 changes: 35 additions & 5 deletions sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,37 @@
package otelsql

import (
"context"
"database/sql"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/nonrecording"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)

var driverName string

func init() {
sql.Register("test-driver", newMockDriver(false))
maxDriverSlot = 1
}

func TestRegister(t *testing.T) {
driverName, err := Register("test-driver", "test-db",
var err error
driverName, err = Register("test-driver", "test-db",
WithAttributes(attribute.String("foo", "bar")),
)
require.NoError(t, err)
assert.Equal(t, "test-driver-otelsql-0", driverName)
if err != nil {
panic(err)
}
if driverName != "test-driver-otelsql-0" {
panic(fmt.Sprintf("expect driver name: test-driver-otelsql-0, got %s", driverName))
}
}

func TestRegister(t *testing.T) {
// Expected driver
db, err := sql.Open(driverName, "")
require.NoError(t, err)
Expand Down Expand Up @@ -66,3 +76,23 @@ func TestWrapDriver(t *testing.T) {
attribute.String("foo", "bar"),
}, otelDriver.cfg.Attributes)
}

func TestRegisterDBStatsMetrics(t *testing.T) {
db, err := sql.Open(driverName, "")
require.NoError(t, err)

err = RegisterDBStatsMetrics(db, "test-db")
assert.NoError(t, err)
}

func TestRecordDBStatsMetricsNoPanic(t *testing.T) {
db, err := sql.Open(driverName, "")
require.NoError(t, err)

instruments, err := newDBStatsInstruments(nonrecording.NewNoopMeterProvider().Meter("test"))
require.NoError(t, err)

cfg := newConfig("db")

recordDBStatsMetrics(context.Background(), db.Stats(), instruments, cfg)
}

0 comments on commit 55851ae

Please sign in to comment.