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

Add span name #49

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 28 additions & 12 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ func (c ocConn) Ping(ctx context.Context) (err error) {

if c.options.Ping && (c.options.AllowRoot || trace.FromContext(ctx) != nil) {
var span *trace.Span
ctx, span = trace.StartSpan(ctx, "sql:ping",
spanName := c.getSpanName(ctx, "sql:ping")
ctx, span = trace.StartSpan(ctx, spanName,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithSampler(c.options.Sampler),
)
Expand Down Expand Up @@ -191,7 +192,6 @@ func (c ocConn) Exec(query string, args []driver.Value) (res driver.Result, err
if !c.options.AllowRoot {
return exec.Exec(query, args)
}

ctx, span := trace.StartSpan(context.Background(), "sql:exec",
trace.WithSpanKind(trace.SpanKindClient),
trace.WithSampler(c.options.Sampler),
Expand Down Expand Up @@ -243,20 +243,21 @@ func (c ocConn) ExecContext(ctx context.Context, query string, args []driver.Nam
}

var span *trace.Span
spanName := c.getSpanName(ctx, "sql:exec")
if parentSpan == nil {
ctx, span = trace.StartSpan(ctx, "sql:exec",
ctx, span = trace.StartSpan(ctx, spanName,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithSampler(c.options.Sampler),
)
} else {
_, span = trace.StartSpan(ctx, "sql:exec",
_, span = trace.StartSpan(ctx, spanName,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithSampler(c.options.Sampler),
)
}
attrs := append([]trace.Attribute(nil), c.options.DefaultAttributes...)
if c.options.Query {
attrs = append(attrs, trace.StringAttribute("sql.query", query))
attrs = append(attrs, trace.StringAttribute(spanName, query))
if c.options.QueryParams {
attrs = append(attrs, namedParamsAttr(args)...)
}
Expand Down Expand Up @@ -343,20 +344,21 @@ func (c ocConn) QueryContext(ctx context.Context, query string, args []driver.Na
}

var span *trace.Span
spanName := c.getSpanName(ctx, "sql:query")
if parentSpan == nil {
ctx, span = trace.StartSpan(ctx, "sql:query",
ctx, span = trace.StartSpan(ctx, spanName,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithSampler(c.options.Sampler),
)
} else {
_, span = trace.StartSpan(ctx, "sql:query",
_, span = trace.StartSpan(ctx, spanName,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithSampler(c.options.Sampler),
)
}
attrs := append([]trace.Attribute(nil), c.options.DefaultAttributes...)
if c.options.Query {
attrs = append(attrs, trace.StringAttribute("sql.query", query))
attrs = append(attrs, trace.StringAttribute(spanName, query))
if c.options.QueryParams {
attrs = append(attrs, namedParamsAttr(args)...)
}
Expand Down Expand Up @@ -432,14 +434,15 @@ func (c *ocConn) PrepareContext(ctx context.Context, query string) (stmt driver.
}()

var span *trace.Span
spanName := c.getSpanName(ctx, "sql:prepare")
attrs := append([]trace.Attribute(nil), c.options.DefaultAttributes...)
if c.options.AllowRoot || trace.FromContext(ctx) != nil {
ctx, span = trace.StartSpan(ctx, "sql:prepare",
ctx, span = trace.StartSpan(ctx, spanName,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithSampler(c.options.Sampler),
)
if c.options.Query {
attrs = append(attrs, trace.StringAttribute("sql.query", query))
attrs = append(attrs, trace.StringAttribute(spanName, query))
}
defer func() {
setSpanStatus(span, c.options, err)
Expand Down Expand Up @@ -480,17 +483,18 @@ func (c *ocConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.
}

var span *trace.Span
spanName := c.getSpanName(ctx, "sql:begin_transaction")
attrs := append([]trace.Attribute(nil), c.options.DefaultAttributes...)

if ctx == nil || ctx == context.TODO() {
ctx = context.Background()
_, span = trace.StartSpan(ctx, "sql:begin_transaction",
_, span = trace.StartSpan(ctx, spanName,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithSampler(c.options.Sampler),
)
attrs = append(attrs, attrMissingContext)
} else {
_, span = trace.StartSpan(ctx, "sql:begin_transaction",
_, span = trace.StartSpan(ctx, spanName,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithSampler(c.options.Sampler),
)
Expand Down Expand Up @@ -1075,3 +1079,15 @@ func setSpanStatus(span *trace.Span, opts TraceOptions, err error) {
status.Message = err.Error()
span.SetStatus(status)
}

func (c ocConn) getSpanName(ctx context.Context, defaultName string) (spanName string) {
if c.options.FormatSpanName != nil && c.options.FormatSpanName(ctx) != "" {
spanName = fmt.Sprint(defaultName, ":", c.options.FormatSpanName(ctx))
}

if spanName == "" {
spanName = defaultName
}

return spanName
}
16 changes: 15 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ocsql

import (
"context"

"go.opencensus.io/trace"
)

Expand Down Expand Up @@ -61,6 +63,11 @@ type TraceOptions struct {

// Sampler to use when creating spans.
Sampler trace.Sampler

// FormatSpanName allows you to overwrite the default span name with a custom name from the context
// This only works with operations using context
// Will use default name if left nil.
FormatSpanName func(context.Context) string
}

// WithAllTraceOptions enables all available trace options.
Expand Down Expand Up @@ -168,7 +175,7 @@ func WithDefaultAttributes(attrs ...trace.Attribute) TraceOption {
}
}

// WithDisableErrSkip, if set to true, will suppress driver.ErrSkip errors in spans.
// WithDisableErrSkip if set to true, will suppress driver.ErrSkip errors in spans.
func WithDisableErrSkip(b bool) TraceOption {
return func(o *TraceOptions) {
o.DisableErrSkip = b
Expand All @@ -188,3 +195,10 @@ func WithInstanceName(instanceName string) TraceOption {
o.InstanceName = instanceName
}
}

// WithFormatSpanName sets the FormatSpanName func
func WithFormatSpanName(spanFunc func(context.Context) string) TraceOption {
return func(o *TraceOptions) {
o.FormatSpanName = spanFunc
}
}