uptrace / opentelemetry-go-extra

OpenTelemetry instrumentations for Go
https://uptrace.dev/get/instrument/
BSD 2-Clause "Simplified" License
314 stars 72 forks source link

otelsql doesn't pass the label attributes when reporting db stats #87

Closed scott20315 closed 1 year ago

scott20315 commented 1 year ago

I suspect the attributes provided to OpenDB are being dropped inside sqlOpenDB when setting up db.Stats reporting via ReportDBStatsMetrics.

func sqlOpenDB(connector driver.Connector, instrum *dbInstrum) *sql.DB {
    db := sql.OpenDB(connector)
    ReportDBStatsMetrics(db, WithMeterProvider(instrum.meterProvider))
    return db
}

should probably look something like this

func sqlOpenDB(connector driver.Connector, instrum *dbInstrum) *sql.DB {
    db := sql.OpenDB(connector)
    ReportDBStatsMetrics(db, WithMeterProvider(instrum.meterProvider), WithAttributes(instrum.attrs))
    return db
}

I'm not seeing the db.system label on any of the db.Stats metrics but it is still present as expected on the query.timing metric that comes from the tracing collection.

My current workaround is to direct register the db.Stats collection with ReportDBStatsMetrics but that doesn't disable the automatic collection started by OpenDB.

vmihailenco commented 1 year ago

@scott20315 looks reasonable. Could you try to prepare a PR?

scott20315 commented 1 year ago

https://github.com/uptrace/opentelemetry-go-extra/pull/89