Skip to content

Commit

Permalink
feat: automatically detect imported version (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
costela committed Jan 19, 2024
1 parent ab9f011 commit cf6d021
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
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 {
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

0 comments on commit cf6d021

Please sign in to comment.