This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"mtusi2026lab7/internal/config"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func Open(
|
||||
ctx context.Context,
|
||||
cfg config.Config,
|
||||
) (*pgxpool.Pool, error) {
|
||||
poolConfig, err := pgxpool.ParseConfig(cfg.DatabaseURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse PostgreSQL configuration: %w", err)
|
||||
}
|
||||
|
||||
poolConfig.MaxConns = cfg.DatabaseMaxConns
|
||||
poolConfig.MinConns = cfg.DatabaseMinConns
|
||||
poolConfig.MaxConnLifetime = cfg.DatabaseMaxConnLifetime
|
||||
poolConfig.MaxConnIdleTime = cfg.DatabaseMaxConnIdleTime
|
||||
poolConfig.HealthCheckPeriod = cfg.DatabaseHealthCheckPeriod
|
||||
poolConfig.ConnConfig.Tracer = pgxQueryTracer{}
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create PostgreSQL pool: %w", err)
|
||||
}
|
||||
|
||||
// NewWithConfig создаёт структуру пула, но не гарантирует,
|
||||
// что PostgreSQL уже доступен. Ping обеспечивает fail-fast.
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
pool.Close()
|
||||
return nil, fmt.Errorf("ping PostgreSQL: %w", err)
|
||||
}
|
||||
|
||||
return pool, nil
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type pgxQueryTracer struct{}
|
||||
|
||||
func (pgxQueryTracer) TraceAcquireStart(
|
||||
ctx context.Context,
|
||||
_ *pgxpool.Pool,
|
||||
_ pgxpool.TraceAcquireStartData,
|
||||
) context.Context {
|
||||
ctx, _ = otel.Tracer("mtusi2026lab7/postgresql").Start(
|
||||
ctx,
|
||||
"postgresql.acquire",
|
||||
trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(
|
||||
attribute.String("db.system.name", "postgresql"),
|
||||
attribute.String("db.operation.name", "ACQUIRE"),
|
||||
),
|
||||
)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (pgxQueryTracer) TraceAcquireEnd(
|
||||
ctx context.Context,
|
||||
_ *pgxpool.Pool,
|
||||
data pgxpool.TraceAcquireEndData,
|
||||
) {
|
||||
span := trace.SpanFromContext(ctx)
|
||||
if data.Err != nil {
|
||||
span.SetStatus(codes.Error, "database connection acquisition failed")
|
||||
} else {
|
||||
span.SetStatus(codes.Ok, "")
|
||||
}
|
||||
span.End()
|
||||
}
|
||||
|
||||
func (pgxQueryTracer) TraceQueryStart(
|
||||
ctx context.Context,
|
||||
_ *pgx.Conn,
|
||||
data pgx.TraceQueryStartData,
|
||||
) context.Context {
|
||||
operation := databaseOperation(data.SQL)
|
||||
ctx, _ = otel.Tracer("mtusi2026lab7/postgresql").Start(
|
||||
ctx,
|
||||
"postgresql."+strings.ToLower(operation),
|
||||
trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(
|
||||
attribute.String("db.system.name", "postgresql"),
|
||||
attribute.String("db.operation.name", operation),
|
||||
),
|
||||
)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (pgxQueryTracer) TraceQueryEnd(
|
||||
ctx context.Context,
|
||||
_ *pgx.Conn,
|
||||
data pgx.TraceQueryEndData,
|
||||
) {
|
||||
span := trace.SpanFromContext(ctx)
|
||||
if data.Err != nil {
|
||||
span.SetStatus(codes.Error, "database operation failed")
|
||||
} else {
|
||||
span.SetStatus(codes.Ok, "")
|
||||
}
|
||||
span.End()
|
||||
}
|
||||
|
||||
func databaseOperation(sql string) string {
|
||||
fields := strings.Fields(sql)
|
||||
if len(fields) == 0 {
|
||||
return "QUERY"
|
||||
}
|
||||
operation := strings.ToUpper(fields[0])
|
||||
switch operation {
|
||||
case "SELECT", "INSERT", "UPDATE", "DELETE", "BEGIN", "COMMIT", "ROLLBACK":
|
||||
return operation
|
||||
default:
|
||||
return "QUERY"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user