Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure otel.Handle is called only if err != nil #206

Merged
merged 3 commits into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
mustNoError(err)

callsCounter, err := meter.Int64Counter(dbSQLClientCalls,
metric.WithUnit(unitDimensionless),
metric.WithDescription(`The number of various calls of methods`),
)
handleErr(err)
mustNoError(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 mustNoError(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"))
mustNoError(errors.New("error"))
})

assert.NotPanics(t, func() {
mustNoError(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