Skip to content

Commit

Permalink
Change the type of SpanNameFormatter from interface to function (#185)
Browse files Browse the repository at this point in the history
* Change the type of SpanNameFormatter from interface to function

* Update CHANGELOG

* Add breaking change notice

* Fix tests

* Fix linter
  • Loading branch information
XSAM authored Sep 14, 2023
1 parent 4dbdf2f commit 5b3eed6
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### ⚠️ Notice ⚠️

This update contains a breaking change of the type of `SpanNameFormatter`. If you use `SpanNameFormatter` in your code, you need to change the type of `SpanNameFormatter` to function.

### Changed

- Upgrade OTel to version `v1.18.0/v0.41.0`. (#184)
- The type of `SpanNameFormatter` has been changed to function for easier use. (#185)

## [0.24.0] - 2023-09-08

Expand Down
13 changes: 4 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ var (
queryMethodKey = attribute.Key("method")
)

// SpanNameFormatter is an interface that used to format span names.
// TODO(Sam): change this to function instead of interface.
type SpanNameFormatter interface {
Format(ctx context.Context, method Method, query string) string
}
// SpanNameFormatter supports formatting span names.
type SpanNameFormatter func(ctx context.Context, method Method, query string) string

// AttributesGetter provides additional attributes on spans creation.
type AttributesGetter func(ctx context.Context, method Method, query string, args []driver.NamedValue) []attribute.KeyValue
Expand Down Expand Up @@ -124,9 +121,7 @@ type SpanOptions struct {
SpanFilter SpanFilter
}

type defaultSpanNameFormatter struct{}

func (f *defaultSpanNameFormatter) Format(_ context.Context, method Method, _ string) string {
func defaultSpanNameFormatter(_ context.Context, method Method, _ string) string {
return string(method)
}

Expand All @@ -135,7 +130,7 @@ func newConfig(options ...Option) config {
cfg := config{
TracerProvider: otel.GetTracerProvider(),
MeterProvider: otel.GetMeterProvider(),
SpanNameFormatter: &defaultSpanNameFormatter{},
SpanNameFormatter: defaultSpanNameFormatter,
}
for _, opt := range options {
opt.Apply(&cfg)
Expand Down
12 changes: 9 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package otelsql

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -27,7 +28,13 @@ import (

func TestNewConfig(t *testing.T) {
cfg := newConfig(WithSpanOptions(SpanOptions{Ping: true}), WithAttributes(semconv.DBSystemMySQL))
assert.Equal(t, config{

// Compare function result
assert.Equal(t, defaultSpanNameFormatter(context.Background(), "foo", "bar"), cfg.SpanNameFormatter(context.Background(), "foo", "bar"))
// Ignore function compare
cfg.SpanNameFormatter = nil

assert.EqualValues(t, config{
TracerProvider: otel.GetTracerProvider(),
Tracer: otel.GetTracerProvider().Tracer(
instrumentationName,
Expand All @@ -44,8 +51,7 @@ func TestNewConfig(t *testing.T) {
Attributes: []attribute.KeyValue{
semconv.DBSystemMySQL,
},
SpanNameFormatter: &defaultSpanNameFormatter{},
SQLCommenter: newCommenter(false),
SQLCommenter: newCommenter(false),
}, cfg)
assert.NotNil(t, cfg.Instruments)
}
4 changes: 2 additions & 2 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func TestOptions(t *testing.T) {
},
{
name: "WithSpanNameFormatter",
option: WithSpanNameFormatter(&defaultSpanNameFormatter{}),
expectedConfig: config{SpanNameFormatter: &defaultSpanNameFormatter{}},
option: WithSpanNameFormatter(nil),
expectedConfig: config{SpanNameFormatter: nil},
},
{
name: "WithSpanOptions",
Expand Down
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func createSpan(
attrs = append(attrs, cfg.AttributesGetter(ctx, method, query, args)...)
}

return cfg.Tracer.Start(ctx, cfg.SpanNameFormatter.Format(ctx, method, query),
return cfg.Tracer.Start(ctx, cfg.SpanNameFormatter(ctx, method, query),
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attrs...),
)
Expand Down
2 changes: 1 addition & 1 deletion utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func newMockConfig(t *testing.T, tracer trace.Tracer) config {
Meter: meter,
Instruments: instruments,
Attributes: []attribute.KeyValue{defaultattribute},
SpanNameFormatter: &defaultSpanNameFormatter{},
SpanNameFormatter: defaultSpanNameFormatter,
SQLCommenter: newCommenter(false),
}
}
Expand Down

0 comments on commit 5b3eed6

Please sign in to comment.