Skip to content

Commit

Permalink
go: add pgx observability
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Login <batazor@evrone.com>
  • Loading branch information
batazor committed Apr 8, 2023
1 parent 743cd8d commit f057b75
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 5 deletions.
6 changes: 4 additions & 2 deletions internal/pkg/db/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"github.com/johejo/golang-migrate-extra/source/iofs"
_ "github.com/lib/pq" // need for init PostgreSQL interface
"github.com/shortlink-org/shortlink/internal/pkg/db/options"
"github.com/spf13/viper"
"github.com/uptrace/opentelemetry-go-extra/otelsql"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"

"github.com/shortlink-org/shortlink/internal/pkg/db/options"
)

var (
Expand All @@ -43,6 +42,9 @@ func (p *Store) Init(ctx context.Context) error {
return err
}

// Instrument the pgxpool config with OpenTelemetry.
cnf.Tracer = &p.tracer

// Create pool config
cnfPool, err := pgxpool.ParseConfig("")
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions internal/pkg/db/postgres/tracer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package postgres

import (
"context"

"github.com/jackc/pgx/v5"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
"go.opentelemetry.io/otel/trace"
)

type Tracer struct{}

func (t *Tracer) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryStartData) context.Context {
span := trace.SpanFromContext(ctx)

span.SetAttributes(
semconv.DBSystemPostgreSQL,
semconv.DBStatementKey.String(data.SQL),
)

return ctx
}

func (t *Tracer) TraceQueryEnd(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData) {
span := trace.SpanFromContext(ctx)

if data.Err != nil {
span.RecordError(data.Err)
}

span.End()
}
2 changes: 2 additions & 0 deletions internal/pkg/db/postgres/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ type Config struct {
type Store struct {
client *pgxpool.Pool
config Config

tracer Tracer
}
2 changes: 1 addition & 1 deletion internal/services/api/application/grpc_web/v1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (api *API) Run(
return err
}

api.http = http_server.New(ctx, mux, config)
api.http = http_server.New(ctx, mux, config, tracer)

// Start HTTP server (and proxy calls to gRPC server endpoint)
log.Info(fmt.Sprintf("API run on port %d", config.Port))
Expand Down
2 changes: 1 addition & 1 deletion internal/services/api/application/http-chi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (api *API) Run(
r.Mount("/api/cqrs", cqrs_api.Routes(link_command, link_query))
r.Mount("/api/sitemap", sitemap_api.Routes(sitemap_rpc))

srv := http_server.New(ctx, r, config)
srv := http_server.New(ctx, r, config, tracer)

// start HTTP-server
log.Info(i18n.Sprintf("API run on port %d", config.Port))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (api *API) Run(
tariffRoutes.Routes(router)
}))

srv := http_server.New(ctx, r, config)
srv := http_server.New(ctx, r, config, tracer)

// start HTTP-server
log.Info(fmt.Sprintf("API run on port %d", config.Port))
Expand Down

0 comments on commit f057b75

Please sign in to comment.