forked from signalfx/splunk-otel-go
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
165 additions
and
142 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
instrumentation/database/sql/splunksql/internal/config/config.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"database/sql/driver" | ||
|
||
splunkotel "github.com/signalfx/splunk-otel-go" | ||
"github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql/internal/moniker" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/codes" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
// InstrumentationName is the instrumentation library identifier for a Tracer. | ||
const InstrumentationName = "github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql" | ||
|
||
// Config contains configuration options. | ||
type Config struct { | ||
TracerProvider trace.TracerProvider | ||
} | ||
|
||
// NewConfig returns a new Config with default values. | ||
func NewConfig() Config { | ||
return Config{TracerProvider: otel.GetTracerProvider()} | ||
} | ||
|
||
// Tracer returns an OTel Tracer from the appropriate TracerProvider. | ||
// | ||
// If the passed context contains a span, the TracerProvider that created the | ||
// Tracer that created that span will be used. Otherwise, the TracerProvider | ||
// from c is used. | ||
func (c Config) Tracer(ctx context.Context) trace.Tracer { | ||
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() { | ||
return span.TracerProvider().Tracer( | ||
InstrumentationName, | ||
trace.WithInstrumentationVersion(splunkotel.Version()), | ||
) | ||
} | ||
return c.TracerProvider.Tracer( | ||
InstrumentationName, | ||
trace.WithInstrumentationVersion(splunkotel.Version()), | ||
) | ||
} | ||
|
||
// WithSpan wraps the function f with a span. | ||
func (c Config) WithSpan(ctx context.Context, name moniker.Span, f func(context.Context) error, opts ...trace.SpanStartOption) error { | ||
// From the specification: span kind MUST always be CLIENT. | ||
opts = append(opts, trace.WithSpanKind(trace.SpanKindClient)) | ||
|
||
var ( | ||
err error | ||
span trace.Span | ||
) | ||
ctx, span = c.Tracer(ctx).Start(ctx, name.String(), opts...) | ||
defer func() { | ||
handleErr(span, err) | ||
span.End() | ||
}() | ||
|
||
err = f(ctx) | ||
return err | ||
} | ||
|
||
func handleErr(span trace.Span, err error) { | ||
if span == nil { | ||
return | ||
} | ||
|
||
switch err { | ||
case nil: | ||
// Everything Okay. | ||
case driver.ErrSkip: | ||
// Expected if method not implemented, do not record these. | ||
default: | ||
span.RecordError(err) | ||
span.SetStatus(codes.Error, err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package splunksql | ||
|
||
import ( | ||
"github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql/internal/config" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
func newConfig(options ...Option) config.Config { | ||
c := config.NewConfig() | ||
for _, o := range options { | ||
o.apply(&c) | ||
} | ||
return c | ||
} | ||
|
||
type Option interface { | ||
apply(*config.Config) | ||
} | ||
|
||
type tracerProviderOption struct { | ||
tp trace.TracerProvider | ||
} | ||
|
||
func (o tracerProviderOption) apply(c *config.Config) { | ||
c.TracerProvider = o.tp | ||
} | ||
|
||
func WithTracerProvider(tp trace.TracerProvider) Option { | ||
return tracerProviderOption{tp: tp} | ||
} |
Oops, something went wrong.