From 1ce5c840ac64509d937fd510eef3915f7a69d2d6 Mon Sep 17 00:00:00 2001 From: Leo Antunes Date: Fri, 19 Jan 2024 16:02:29 +0100 Subject: [PATCH] feat: automatically detect imported version --- README.md | 6 ------ internal/constants.go | 11 ----------- tracer.go | 24 ++++++++++++++++++++---- tracer_test.go | 2 -- 4 files changed, 20 insertions(+), 23 deletions(-) delete mode 100644 internal/constants.go diff --git a/README.md b/README.md index 1ab6604..eaa0085 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,6 @@ instrumentation for the [jackc/pgx](https://github.com/jackc/pgx) library. ## Usage -Make sure you have a suitable pgx version: - -```bash -go get github.com/jackc/pgx/v5 -``` - Install the library: ```go diff --git a/internal/constants.go b/internal/constants.go deleted file mode 100644 index 3ec7946..0000000 --- a/internal/constants.go +++ /dev/null @@ -1,11 +0,0 @@ -package internal - -const ( - // TracerName is the name of the tracer. This will be used as an attribute - // on each span. - TracerName = "github.com/exaring/otelpgx" - - // InstrumentationVersion is the version of the otelpgx library. This will - // be used as an attribute on each span. - InstrumentationVersion = "v0.5.2" -) diff --git a/tracer.go b/tracer.go index 361be69..5a6df03 100644 --- a/tracer.go +++ b/tracer.go @@ -5,6 +5,7 @@ import ( "database/sql" "errors" "fmt" + "runtime/debug" "strings" "github.com/jackc/pgx/v5" @@ -14,8 +15,12 @@ import ( "go.opentelemetry.io/otel/codes" semconv "go.opentelemetry.io/otel/semconv/v1.17.0" "go.opentelemetry.io/otel/trace" +) + +const ( + tracerName = "github.com/exaring/otelpgx" - "github.com/exaring/otelpgx/internal" + sqlOperationUnknown = "UNKNOWN" ) const ( @@ -70,7 +75,7 @@ func NewTracer(opts ...Option) *Tracer { } return &Tracer{ - tracer: cfg.tp.Tracer(internal.TracerName, trace.WithInstrumentationVersion(internal.InstrumentationVersion)), + tracer: cfg.tp.Tracer(tracerName, trace.WithInstrumentationVersion(findOwnImportedVersion())), attrs: cfg.attrs, trimQuerySpanName: cfg.trimQuerySpanName, spanNameFunc: cfg.spanNameFunc, @@ -91,8 +96,6 @@ func recordError(span trace.Span, err error) { } } -const sqlOperationUnknown = "UNKNOWN" - // sqlOperationName attempts to get the first 'word' from a given SQL query, which usually // is the operation name (e.g. 'SELECT'). func (t *Tracer) sqlOperationName(stmt string) string { @@ -354,3 +357,16 @@ func makeParamsAttribute(args []any) attribute.KeyValue { } return QueryParametersKey.StringSlice(ss) } + +func findOwnImportedVersion() string { + buildInfo, ok := debug.ReadBuildInfo() + if ok { + for _, dep := range buildInfo.Deps { + if dep.Path == tracerName { + return dep.Version + } + } + } + + return "unknown" +} diff --git a/tracer_test.go b/tracer_test.go index 93a0d96..e821a92 100644 --- a/tracer_test.go +++ b/tracer_test.go @@ -97,10 +97,8 @@ var defaultSpanNameFunc SpanNameFunc = func(query string) string { switch { case strings.HasPrefix(line, "--"): prefix = "--" - case strings.HasPrefix(line, "/*"): prefix = "/*" - case strings.HasPrefix(line, "#"): prefix = "#" default: