Skip to content

Commit

Permalink
RecordStats improvements (#330)
Browse files Browse the repository at this point in the history
* Use the counter metric type for counters

Based on the docs in [DBStats](https://pkg.go.dev/database/sql#DBStats) (and testing) it seems that WaitCount, WaitDuration, MaxIdleClosed, MaxIdleTimeClosed, MaxLifetimeClosed are counters and not Gauges.
To avoid confusion the metric type is changed for these fields.

* Add the MaxIdleTimeClosed metric

* Update Connection Metrics

Update otelsql based on the prometheus exporter.
  • Loading branch information
boekkooi-impossiblecloud authored Jan 7, 2025
1 parent 39940ea commit 0f648e2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,15 +561,15 @@ The `interval` in `RecordStats()` is replaced with `WithMinimumReadDBStatsInterv

**Connection Metrics**

| `ocsql` | `otelsql` |
|:---------------------------------------------------------------|:--------------------------------------------------------------------|
| `go_sql_db_connections_active{go_sql_instance}` | `db_sql_connections_active{db_instance,db_system,db_name}` |
| `go_sql_db_connections_idle{go_sql_instance}` | `db_sql_connections_idle{db_instance,db_system,db_name}` |
| `go_sql_db_connections_idle_closed_count{go_sql_instance}` | `db_sql_connections_idle_closed{db_instance,db_system,db_name}` |
| `go_sql_db_connections_lifetime_closed_count{go_sql_instance}` | `db_sql_connections_lifetime_closed{db_instance,db_system,db_name}` |
| `go_sql_db_connections_open{go_sql_instance}` | `db_sql_connections_open{db_instance,db_system,db_name}` |
| `go_sql_db_connections_wait_count{go_sql_instance}` | `db_sql_connections_wait_count{db_instance,db_system,db_name}` |
| `go_sql_db_connections_wait_duration{go_sql_instance}` | `db_sql_connections_wait_duration{db_instance,db_system,db_name}` |
| `ocsql` | `otelsql` |
|:---------------------------------------------------------------|:-------------------------------------------------------------------------------------|
| `go_sql_db_connections_active{go_sql_instance}` | `db_sql_connections_active_ratio{db_instance,db_system,db_name}` |
| `go_sql_db_connections_idle{go_sql_instance}` | `db_sql_connections_idle_ratio{db_instance,db_system,db_name}` |
| `go_sql_db_connections_idle_closed_count{go_sql_instance}` | `db_sql_connections_idle_closed_ratio_total{db_instance,db_system,db_name}` |
| `go_sql_db_connections_lifetime_closed_count{go_sql_instance}` | `db_sql_connections_lifetime_closed_ratio_total{db_instance,db_system,db_name}` |
| `go_sql_db_connections_open{go_sql_instance}` | `db_sql_connections_open_ratio{db_instance,db_system,db_name}` |
| `go_sql_db_connections_wait_count{go_sql_instance}` | `db_sql_connections_wait_count_ratio_total{db_instance,db_system,db_name}` |
| `go_sql_db_connections_wait_duration{go_sql_instance}` | `db_sql_connections_wait_duration_milliseconds_total{db_instance,db_system,db_name}` |

[<sub><sup>[table of contents]</sup></sub>](#table-of-contents)

Expand Down
12 changes: 8 additions & 4 deletions resources/fixtures/metrics/stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,30 @@
},
{
"Name": "db.sql.connections.idle_closed{service.name=otelsql,instrumentation.name=go.nhat.io/otelsql,db.instance=default,db.system=postgresql}",
"Last": 0
"Sum": 0
},
{
"Name": "db.sql.connections.idle_time_closed{service.name=otelsql,instrumentation.name=go.nhat.io/otelsql,db.instance=default,db.system=postgresql}",
"Sum": 0
},
{
"Name": "db.sql.connections.idle{service.name=otelsql,instrumentation.name=go.nhat.io/otelsql,db.instance=default,db.system=postgresql}",
"Last": 1
},
{
"Name": "db.sql.connections.lifetime_closed{service.name=otelsql,instrumentation.name=go.nhat.io/otelsql,db.instance=default,db.system=postgresql}",
"Last": 0
"Sum": 0
},
{
"Name": "db.sql.connections.open{service.name=otelsql,instrumentation.name=go.nhat.io/otelsql,db.instance=default,db.system=postgresql}",
"Last": 1
},
{
"Name": "db.sql.connections.wait_count{service.name=otelsql,instrumentation.name=go.nhat.io/otelsql,db.instance=default,db.system=postgresql}",
"Last": 0
"Sum": 0
},
{
"Name": "db.sql.connections.wait_duration{service.name=otelsql,instrumentation.name=go.nhat.io/otelsql,db.instance=default,db.system=postgresql}",
"Last": 0
"Sum": 0
}
]
27 changes: 19 additions & 8 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
dbSQLConnectionsWaitCount = "db.sql.connections.wait_count"
dbSQLConnectionsWaitDuration = "db.sql.connections.wait_duration"
dbSQLConnectionsIdleClosed = "db.sql.connections.idle_closed"
dbSQLConnectionsIdleTimeClosed = "db.sql.connections.idle_time_closed"
dbSQLConnectionsLifetimeClosed = "db.sql.connections.lifetime_closed"
)

Expand Down Expand Up @@ -53,10 +54,11 @@ func recordStats(
openConnections metric.Int64ObservableGauge
idleConnections metric.Int64ObservableGauge
activeConnections metric.Int64ObservableGauge
waitCount metric.Int64ObservableGauge
waitDuration metric.Float64ObservableGauge
idleClosed metric.Int64ObservableGauge
lifetimeClosed metric.Int64ObservableGauge
waitCount metric.Int64ObservableCounter
waitDuration metric.Float64ObservableCounter
idleClosed metric.Int64ObservableCounter
idleTimeClosed metric.Int64ObservableCounter
lifetimeClosed metric.Int64ObservableCounter

dbStats sql.DBStats
lastDBStats time.Time
Expand Down Expand Up @@ -89,28 +91,35 @@ func recordStats(
)
handleErr(err)

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

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

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

lifetimeClosed, err = meter.Int64ObservableGauge(
idleTimeClosed, err = meter.Int64ObservableCounter(
dbSQLConnectionsIdleTimeClosed,
metric.WithUnit(unitDimensionless),
metric.WithDescription("The total number of connections closed due to SetConnMaxIdleTime"),
)
handleErr(err)

lifetimeClosed, err = meter.Int64ObservableCounter(
dbSQLConnectionsLifetimeClosed,
metric.WithUnit(unitDimensionless),
metric.WithDescription("The total number of connections closed due to SetConnMaxLifetime"),
Expand All @@ -133,6 +142,7 @@ func recordStats(
obs.ObserveInt64(waitCount, dbStats.WaitCount, metric.WithAttributes(attrs...))
obs.ObserveFloat64(waitDuration, float64(dbStats.WaitDuration.Nanoseconds())/1e6, metric.WithAttributes(attrs...))
obs.ObserveInt64(idleClosed, dbStats.MaxIdleClosed, metric.WithAttributes(attrs...))
obs.ObserveInt64(idleTimeClosed, dbStats.MaxIdleTimeClosed, metric.WithAttributes(attrs...))
obs.ObserveInt64(lifetimeClosed, dbStats.MaxLifetimeClosed, metric.WithAttributes(attrs...))

return nil
Expand All @@ -143,6 +153,7 @@ func recordStats(
waitCount,
waitDuration,
idleClosed,
idleTimeClosed,
lifetimeClosed,
)

Expand Down

0 comments on commit 0f648e2

Please sign in to comment.