Skip to content

Commit

Permalink
make sure otel.Handle is called only if err != nil
Browse files Browse the repository at this point in the history
  • Loading branch information
auvn committed May 19, 2023
1 parent 2ffc8e2 commit 7a068fe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
10 changes: 2 additions & 8 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ func newConnConfig(opts driverOptions) connConfig {
metric.WithUnit(unitMilliseconds),
metric.WithDescription(`The distribution of latencies of various calls in milliseconds`),
)
handleErr(err)
mustHandleErr(err)

callsCounter, err := meter.Int64Counter(dbSQLClientCalls,
metric.WithUnit(unitDimensionless),
metric.WithDescription(`The number of various calls of methods`),
)
handleErr(err)
mustHandleErr(err)

latencyRecorder := newMethodRecorder(latencyMsHistogram.Record, callsCounter.Add, opts.defaultAttributes...)

Expand Down Expand Up @@ -214,9 +214,3 @@ func (d otDriver) Connect(ctx context.Context) (driver.Conn, error) {
func (d otDriver) Driver() driver.Driver {
return d
}

func handleErr(err error) {
if err != nil {
panic(err)
}
}
15 changes: 15 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package otelsql

import "go.opentelemetry.io/otel"

func handleErr(err error) {
if err != nil {
otel.Handle(err)
}
}

func mustHandleErr(err error) {
if err != nil {
panic(err)
}
}
10 changes: 9 additions & 1 deletion driver_internal_test.go → errors_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ func TestHandleError(t *testing.T) {
t.Parallel()

assert.Panics(t, func() {
handleErr(errors.New("error"))
mustHandleErr(errors.New("error"))
})

assert.NotPanics(t, func() {
mustHandleErr(nil)
})

assert.NotPanics(t, func() {
handleErr(nil)
})

assert.NotPanics(t, func() {
handleErr(assert.AnError)
})
}
15 changes: 7 additions & 8 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sync"
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
Expand Down Expand Up @@ -74,49 +73,49 @@ func recordStats(
metric.WithUnit(unitDimensionless),
metric.WithDescription("Count of open connections in the pool"),
)
otel.Handle(err)
handleErr(err)

idleConnections, err = meter.Int64ObservableGauge(
dbSQLConnectionsIdle,
metric.WithUnit(unitDimensionless),
metric.WithDescription("Count of idle connections in the pool"),
)
otel.Handle(err)
handleErr(err)

activeConnections, err = meter.Int64ObservableGauge(
dbSQLConnectionsActive,
metric.WithUnit(unitDimensionless),
metric.WithDescription("Count of active connections in the pool"),
)
otel.Handle(err)
handleErr(err)

waitCount, err = meter.Int64ObservableGauge(
dbSQLConnectionsWaitCount,
metric.WithUnit(unitDimensionless),
metric.WithDescription("The total number of connections waited for"),
)
otel.Handle(err)
handleErr(err)

waitDuration, err = meter.Float64ObservableGauge(
dbSQLConnectionsWaitDuration,
metric.WithUnit(unitMilliseconds),
metric.WithDescription("The total time blocked waiting for a new connection"),
)
otel.Handle(err)
handleErr(err)

idleClosed, err = meter.Int64ObservableGauge(
dbSQLConnectionsIdleClosed,
metric.WithUnit(unitDimensionless),
metric.WithDescription("The total number of connections closed due to SetMaxIdleConns"),
)
otel.Handle(err)
handleErr(err)

lifetimeClosed, err = meter.Int64ObservableGauge(
dbSQLConnectionsLifetimeClosed,
metric.WithUnit(unitDimensionless),
metric.WithDescription("The total number of connections closed due to SetConnMaxLifetime"),
)
otel.Handle(err)
handleErr(err)

_, err = meter.RegisterCallback(func(ctx context.Context, obs metric.Observer) error {
lock.Lock()
Expand Down

0 comments on commit 7a068fe

Please sign in to comment.