gomemcache icon indicating copy to clipboard operation
gomemcache copied to clipboard

otelmemcache deprecation

Open pellared opened this issue 2 years ago • 1 comments

I am creating this issue to inform you that we (OpenTelemetry contributors) are planning to deprecate https://pkg.go.dev/go.opentelemetry.io/contrib/instrumentation/github.com/bradfitz/gomemcache/memcache/otelmemcache.

You can consider copying the OpenTelemetry instrumentation to this repository or organization. You can even consider instrumenting with OpenTelemetry "natively" (without requiring the users to use an instrumentation library). You can also check for more information here.

pellared avatar Aug 11 '23 10:08 pellared


package otelmemcache

import (
    "context"
    "github.com/bradfitz/gomemcache/memcache"
    "go.opentelemetry.io/otel"
    "go.opentelemetry.io/otel/attribute"
    "go.opentelemetry.io/otel/trace"
)

// Client wraps a memcache.Client with OpenTelemetry tracing.
type Client struct {
    *memcache.Client
    tracer trace.Tracer
    ctx    context.Context
}

// Option configures the Client.
type Option func(*Client)

// WithTracerProvider sets the tracer provider.
func WithTracerProvider(tp trace.TracerProvider) Option {
    return func(c *Client) {
        c.tracer = tp.Tracer("github.com/your-org/gomemcache-otel")
    }
}

// NewClientWithTracing creates a new traced Client.
func NewClientWithTracing(client *memcache.Client, opts ...Option) *Client {
    c := &Client{
        Client: client,
        tracer: otel.Tracer("github.com/your-org/gomemcache-otel"),
    }
    for _, opt := range opts {
        opt(c)
    }
    return c
}

// WithContext sets the context for tracing.
func (c *Client) WithContext(ctx context.Context) *Client {
    newClient := *c
    newClient.ctx = ctx
    return &newClient
}

// Add traces the Add operation.
func (c *Client) Add(item *memcache.Item) error {
    ctx, span := c.tracer.Start(c.ctx, "memcache.Add", trace.WithAttributes(
        attribute.String("memcache.key", item.Key),
    ))
    defer span.End()
    err := c.Client.Add(item)
    if err != nil {
        span.RecordError(err)
        span.SetStatus(codes.Error, err.Error())
    }
    return err
}

// Get traces the Get operation.
func (c *Client) Get(key string) (*memcache.Item, error) {
    ctx, span := c.tracer.Start(c.ctx, "memcache.Get", trace.WithAttributes(
        attribute.String("memcache.key", key),
    ))
    defer span.End()
    item, err := c.Client.Get(key)
    if err != nil {
        span.RecordError(err)
        span.SetStatus(codes.Error, err.Error())
    }
    return item, err
}

// Delete traces the Delete operation.
func (c *Client) Delete(key string) error {
    ctx, span := c.tracer.Start(c.ctx, "memcache.Delete", trace.WithAttributes(
        attribute.String("memcache.key", key),
    ))
    defer span.End()
    err := c.Client.Delete(key)
    if err != nil {
        span.RecordError(err)
        span.SetStatus(codes.Error, err.Error())
    }
    return err
}

// Ping traces the Ping operation.
func (c *Client) Ping() error {
    ctx, span := c.tracer.Start(c.ctx, "memcache.Ping")
    defer span.End()
    err := c.Client.Ping()
    if err != nil {
        span.RecordError(err)
        span.SetStatus(codes.Error, err.Error())
    }
    return err
}

ljluestc avatar May 10 '25 14:05 ljluestc