feat: add OpenTelemetry instrumentation

This commit is contained in:
2026-11-01 17:06:32 +03:00
parent cea50c2cd6
commit 934dfefee7
49 changed files with 1113 additions and 286 deletions
+2 -1
View File
@@ -8,7 +8,7 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"gitea.privcloud.tech/dizi/mtusi2026lab5/migrations"
"gitea.privcloud.tech/dizi/mtusi2026lab6/migrations"
)
var (
@@ -40,6 +40,7 @@ func Open(ctx context.Context, config Config) (*pgxpool.Pool, error) {
if err != nil {
return nil, fmt.Errorf("parse database configuration: %w", err)
}
poolConfig.ConnConfig.Tracer = queryTracer{}
poolConfig.MinConns = config.MinConnections
poolConfig.MaxConns = config.MaxConnections
poolConfig.MaxConnLifetime = config.MaxConnectionAge
+28
View File
@@ -0,0 +1,28 @@
package database
import (
"context"
"github.com/jackc/pgx/v5"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
type queryTracer struct{}
func (queryTracer) TraceQueryStart(ctx context.Context, _ *pgx.Conn, _ pgx.TraceQueryStartData) context.Context {
ctx, _ = otel.Tracer("postgres").Start(ctx, "postgres query", trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attribute.String("db.system", "postgresql")))
return ctx
}
func (queryTracer) TraceQueryEnd(ctx context.Context, _ *pgx.Conn, data pgx.TraceQueryEndData) {
span := trace.SpanFromContext(ctx)
if data.Err != nil {
span.RecordError(data.Err)
span.SetStatus(codes.Error, "query failed")
}
span.End()
}