Skip to content

Commit

Permalink
otelmemcache: Simplify config and span status setting (#477)
Browse files Browse the repository at this point in the history
* otelmemcache: Simplify config - remove service name

- Service name no longer needed
- No need to keep config inside of client any longer

* otelmemcache: Do not set status and service name attribute

- Set status only in case of error

* otelmemcache: Adjust tests

* Update CHANGELOG

* PR feedback - revert to config func

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
  • Loading branch information
matej-g and Aneurysm9 committed Feb 18, 2021
1 parent 62c8535 commit 3349baf
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 70 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Fixed

- `otelmemcache` no longer sets span status to OK instead of leaving it unset. (#477)

### Removed

- Remove service name from `otelmemcache` configuration and span attributes. (#477)

## [0.17.0] - 2021-02-15

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
)

type config struct {
serviceName string
tracerProvider oteltrace.TracerProvider
}

Expand All @@ -33,10 +32,3 @@ func WithTracerProvider(provider oteltrace.TracerProvider) Option {
cfg.tracerProvider = provider
}
}

// WithServiceName sets the service name.
func WithServiceName(serviceName string) Option {
return func(cfg *config) {
cfg.serviceName = serviceName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@ import (

"go.opentelemetry.io/contrib"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/semconv"
oteltrace "go.opentelemetry.io/otel/trace"
)

const (
defaultTracerName = "go.opentelemetry.io/contrib/instrumentation/github.com/bradfitz/gomemcache/memcache/otelmemcache"
defaultServiceName = "memcached"
tracerName = "go.opentelemetry.io/contrib/instrumentation/github.com/bradfitz/gomemcache/memcache/otelmemcache"
)

// Client is a wrapper around *memcache.Client.
type Client struct {
*memcache.Client
cfg *config
tracer oteltrace.Tracer
ctx context.Context
}
Expand All @@ -54,19 +52,14 @@ func NewClientWithTracing(client *memcache.Client, opts ...Option) *Client {
o(cfg)
}

if cfg.serviceName == "" {
cfg.serviceName = defaultServiceName
}

if cfg.tracerProvider == nil {
cfg.tracerProvider = otel.GetTracerProvider()
}

return &Client{
client,
cfg,
cfg.tracerProvider.Tracer(
defaultTracerName,
tracerName,
oteltrace.WithInstrumentationVersion(contrib.SemVersion()),
),
context.Background(),
Expand All @@ -77,7 +70,6 @@ func NewClientWithTracing(client *memcache.Client, opts ...Option) *Client {
// of the operation name and item key(s) (if available)
func (c *Client) attrsByOperationAndItemKey(operation operation, key ...string) []label.KeyValue {
labels := []label.KeyValue{
semconv.ServiceNameKey.String(c.cfg.serviceName),
memcacheDBSystem(),
memcacheDBOperation(operation),
}
Expand Down Expand Up @@ -111,7 +103,7 @@ func (c *Client) startSpan(operationName operation, itemKey ...string) oteltrace
// Ends span and, if applicable, sets error status
func endSpan(s oteltrace.Span, err error) {
if err != nil {
s.SetStatus(memcacheErrToStatusCode(err), err.Error())
s.SetStatus(codes.Error, err.Error())
}
s.End()
}
Expand All @@ -121,7 +113,6 @@ func (c *Client) WithContext(ctx context.Context) *Client {
cc := c.Client
return &Client{
Client: cc,
cfg: c.cfg,
tracer: c.tracer,
ctx: ctx,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/oteltest"
"go.opentelemetry.io/otel/semconv"
oteltrace "go.opentelemetry.io/otel/trace"
)

Expand All @@ -41,10 +40,7 @@ func TestNewClientWithTracing(t *testing.T) {
)

assert.NotNil(t, c.Client)
assert.NotNil(t, c.cfg)
assert.NotNil(t, c.cfg.tracerProvider)
assert.NotNil(t, c.tracer)
assert.Equal(t, defaultServiceName, c.cfg.serviceName)
}

func TestOperation(t *testing.T) {
Expand All @@ -61,10 +57,9 @@ func TestOperation(t *testing.T) {
assert.Len(t, spans, 1)
assert.Equal(t, oteltrace.SpanKindClient, spans[0].SpanKind())
assert.Equal(t, string(operationAdd), spans[0].Name())
assert.Len(t, spans[0].Attributes(), 4)
assert.Len(t, spans[0].Attributes(), 3)

expectedLabelMap := map[label.Key]label.Value{
semconv.ServiceNameKey: semconv.ServiceNameKey.String(defaultServiceName).Value,
memcacheDBSystem().Key: memcacheDBSystem().Value,
memcacheDBOperation(operationAdd).Key: memcacheDBOperation(operationAdd).Value,
label.Key(memcacheDBItemKeyName).String(mi.Key).Key: label.Key(memcacheDBItemKeyName).String(mi.Key).Value,
Expand All @@ -83,10 +78,9 @@ func TestOperationWithCacheMissError(t *testing.T) {
assert.Len(t, spans, 1)
assert.Equal(t, oteltrace.SpanKindClient, spans[0].SpanKind())
assert.Equal(t, string(operationGet), spans[0].Name())
assert.Len(t, spans[0].Attributes(), 4)
assert.Len(t, spans[0].Attributes(), 3)

expectedLabelMap := map[label.Key]label.Value{
semconv.ServiceNameKey: semconv.ServiceNameKey.String(defaultServiceName).Value,
memcacheDBSystem().Key: memcacheDBSystem().Value,
memcacheDBOperation(operationGet).Key: memcacheDBOperation(operationGet).Value,
label.Key(memcacheDBItemKeyName).String(key).Key: label.Key(memcacheDBItemKeyName).String(key).Value,
Expand Down

This file was deleted.

0 comments on commit 3349baf

Please sign in to comment.