Skip to content

Commit 491a0aa

Browse files
committed
Add span formatter
1 parent 13a2986 commit 491a0aa

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

coderd/tracing/postgres.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package tracing
22

33
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
48
"github.com/nhatthm/otelsql"
5-
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
9+
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
610
"go.opentelemetry.io/otel/trace"
711
"golang.org/x/xerrors"
812
)
@@ -17,10 +21,22 @@ func PostgresDriver(tp trace.TracerProvider, service string) (string, error) {
1721
otelsql.TraceQueryWithoutArgs(),
1822
otelsql.WithSystem(semconv.DBSystemPostgreSQL),
1923
otelsql.WithTracerProvider(tp),
24+
otelsql.WithSpanNameFormatter(formatPostgresSpan),
2025
)
2126
if err != nil {
2227
return "", xerrors.Errorf("registering postgres tracing driver: %w", err)
2328
}
2429

2530
return driverName, nil
2631
}
32+
33+
func formatPostgresSpan(ctx context.Context, op string) string {
34+
const qPrefix = "-- name: "
35+
q := otelsql.QueryFromContext(ctx)
36+
if q == "" || !strings.HasPrefix(q, qPrefix) {
37+
return strings.ToUpper(op)
38+
}
39+
40+
s := strings.Split(strings.TrimPrefix(q, qPrefix), " ")[0]
41+
return fmt.Sprintf("%s %s", strings.ToUpper(op), s)
42+
}

0 commit comments

Comments
 (0)