Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: automatically detect imported version #30

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions internal/constants.go

This file was deleted.

24 changes: 20 additions & 4 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"errors"
"fmt"
"runtime/debug"
"strings"

"github.com/jackc/pgx/v5"
Expand All @@ -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 (
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
obitech marked this conversation as resolved.
Show resolved Hide resolved
return dep.Version
}
}
}

return "unknown"
}
2 changes: 0 additions & 2 deletions tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down