Skip to content

Commit

Permalink
Add log tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim committed Jun 21, 2021
1 parent 266aa0c commit e73cd23
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
17 changes: 17 additions & 0 deletions internal/datastore/postgres/log_tracer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package postgres

import (
"context"

"github.com/jackc/pgx/v4"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

type tracingLogger struct {
}

func (tl tracingLogger) Log(ctx context.Context, level pgx.LogLevel, msg string, data map[string]interface{}) {
span := trace.SpanFromContext(ctx)
span.AddEvent(msg, trace.WithAttributes(attribute.Stringer("level", level)))
}
5 changes: 2 additions & 3 deletions internal/datastore/postgres/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type postgresOptions struct {

enablePrometheusStats bool

driver string
logger *tracingLogger
}

const (
Expand All @@ -33,7 +33,6 @@ func generateConfig(options []PostgresOption) (postgresOptions, error) {
computed := postgresOptions{
gcWindow: 24 * time.Hour,
watchBufferLength: defaultWatchBufferLength,
driver: "postgres",
}

for _, option := range options {
Expand Down Expand Up @@ -108,6 +107,6 @@ func EnablePrometheusStats() PostgresOption {

func EnableTracing() PostgresOption {
return func(po *postgresOptions) {
po.driver = tracingDriverName
po.logger = &tracingLogger{}
}
}

0 comments on commit e73cd23

Please sign in to comment.