New: ServiceBase migrated from shortener
This commit is contained in:
22
database/postgres.go
Normal file
22
database/postgres.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/uptrace/opentelemetry-go-extra/otelsql"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
)
|
||||
|
||||
// OpenPostgres establishes a connection to a Postgres database and enables OpenTelemetry tracing and stats reporting.
|
||||
func OpenPostgres(dbURL string) (*sql.DB, error) {
|
||||
db, err := otelsql.Open("postgres", dbURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
otelsql.ReportDBStatsMetrics(db, otelsql.WithAttributes(
|
||||
attribute.String("db.system", "postgresql"),
|
||||
))
|
||||
|
||||
return db, nil
|
||||
}
|
||||
Reference in New Issue
Block a user