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

otelmemcache: Simplify config and span status setting #477

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 9 additions & 1 deletion 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

nit


## [0.14.0] - 2020-11-20

### Added
Expand All @@ -16,7 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The AWS detector now adds the cloud zone, host image ID, host type, and host name to the returned `Resource`. (#410)
- Add Amazon ECS Resource Detector for AWS X-Ray. (#466)
- Add propagator for AWS X-Ray (#462)

### Changed

- Add semantic version to `Tracer` / `Meter` created by instrumentation packages `otelsaram`, `otelrestful`, `otelmongo`, `otelhttp` and `otelhttptrace`. (#412)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,11 @@ import (
oteltrace "go.opentelemetry.io/otel/trace"
)

type config struct {
serviceName string
tracerProvider oteltrace.TracerProvider
}

// Option is used to configure the client.
type Option func(*config)
type Option oteltrace.TracerProvider
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably stick with a generic Option type like we had before. Even though we only have a single option now, we may want to add others in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, now I see this could potentially be a breaking change, good point 👍. I'll keep the type as it was.


// WithTracerProvider specifies a tracer provider to use for creating a tracer.
// If none is specified, the global provider is used.
func WithTracerProvider(provider oteltrace.TracerProvider) Option {
return func(cfg *config) {
cfg.tracerProvider = provider
}
}

// WithServiceName sets the service name.
func WithServiceName(serviceName string) Option {
return func(cfg *config) {
cfg.serviceName = serviceName
}
return provider
}
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 @@ -49,24 +47,19 @@ type Client struct {
// error code and message, if an error occurs). Optionally, client context can
// be set before an operation with the WithContext method.
func NewClientWithTracing(client *memcache.Client, opts ...Option) *Client {
cfg := &config{}
var tp oteltrace.TracerProvider
for _, o := range opts {
o(cfg)
tp = o
}

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

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

return &Client{
client,
cfg,
cfg.tracerProvider.Tracer(
defaultTracerName,
tp.Tracer(
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.