{"id":3199,"library":"opentelemetry-instrumentation-pymemcache","title":"OpenTelemetry pymemcache Instrumentation","description":"This library provides OpenTelemetry instrumentation for `pymemcache`, enabling automatic tracing of Memcached client operations. It is part of the `opentelemetry-python-contrib` project, which generally follows a monthly release cadence. As of version 0.62b0, the instrumentation libraries within `opentelemetry-python-contrib` are considered beta and may undergo breaking changes.","status":"active","version":"0.62b0","language":"en","source_language":"en","source_url":"https://github.com/open-telemetry/opentelemetry-python-contrib","tags":["opentelemetry","observability","metrics","tracing","instrumentation","memcached","pymemcache"],"install":[{"cmd":"pip install opentelemetry-instrumentation-pymemcache pymemcache opentelemetry-sdk opentelemetry-exporter-otlp","lang":"bash","label":"Install with core OTel SDK and OTLP exporter"}],"dependencies":[{"reason":"The underlying library being instrumented.","package":"pymemcache","optional":false},{"reason":"Core OpenTelemetry API for defining telemetry signals.","package":"opentelemetry-api","optional":false},{"reason":"OpenTelemetry Python SDK for processing and exporting telemetry. Applications should depend on this or another SDK.","package":"opentelemetry-sdk","optional":false},{"reason":"Required for exporting telemetry data via OTLP, as shown in the quickstart example.","package":"opentelemetry-exporter-otlp","optional":true}],"imports":[{"symbol":"PymemcacheInstrumentor","correct":"from opentelemetry.instrumentation.pymemcache import PymemcacheInstrumentor"}],"quickstart":{"code":"import os\nfrom opentelemetry import trace\nfrom opentelemetry.sdk.trace import TracerProvider\nfrom opentelemetry.sdk.trace.export import BatchSpanProcessor\nfrom opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter\nfrom opentelemetry.instrumentation.pymemcache import PymemcacheInstrumentor\nfrom pymemcache.client.base import Client\n\n# Configure OpenTelemetry Tracer Provider\nprovider = TracerProvider()\nprocessor = BatchSpanProcessor(OTLPSpanExporter(endpoint=os.environ.get('OTEL_EXPORTER_OTLP_ENDPOINT', 'localhost:4317')))\nprovider.add_span_processor(processor)\ntrace.set_tracer_provider(provider)\n\n# Instrument pymemcache\nPymemcacheInstrumentor().instrument()\n\n# Use pymemcache as usual\n# Ensure a memcached server is running at localhost:11211\ntry:\n    client = Client(('localhost', 11211))\n    client.set('my_key', 'my_value')\n    value = client.get('my_key')\n    print(f\"Retrieved value: {value.decode('utf-8') if value else None}\")\n    client.delete('my_key')\nexcept Exception as e:\n    print(f\"Error connecting to Memcached or during operation: {e}\")\n    print(\"Please ensure a memcached server is running at localhost:11211\")\n\n# Flush spans before exiting\nprovider.shutdown()","lang":"python","description":"This example sets up a basic OpenTelemetry tracing environment with an OTLP exporter, instruments the `pymemcache` library, and then demonstrates typical `pymemcache` client operations. Spans for `set`, `get`, and `delete` operations will be generated and sent to the configured OTLP endpoint (e.g., an OpenTelemetry Collector)."},"warnings":[{"fix":"Monitor release notes for breaking changes. Exercise caution when deploying to production and consider locking dependency versions.","message":"OpenTelemetry Python Contrib instrumentations are currently in beta, which means they are not recommended for production environments. Breaking changes to APIs or telemetry output may occur in future releases without major version bumps.","severity":"gotcha","affected_versions":"All versions < 1.0.0"},{"fix":"For Gunicorn, consider using a single worker (`--workers 1`), or implement programmatic instrumentation where you initialize the OpenTelemetry SDK in each worker process after forking. For metrics, using a `PeriodicExportingMetricReader` with one OTLP worker per process to push to a Prometheus server is also recommended.","message":"When using pre-forking web servers (e.g., Gunicorn with `workers > 1`), auto-instrumentation, particularly for metrics, can lead to inconsistent or broken telemetry due to issues with background threads and locks being duplicated or not properly initialized in child processes after forking.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Disable Flask's debug mode when using OpenTelemetry instrumentation, especially in environments where robust tracing is critical.","message":"Running applications with Flask's debug mode enabled (which uses a reloader) can interfere with OpenTelemetry's instrumentation, causing it to break or not function as expected.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Stay informed about semantic convention updates from the OpenTelemetry project. Be prepared to update monitoring configurations as semantic conventions stabilize and are adopted by the instrumentation.","message":"The OpenTelemetry semantic conventions, which define the names and attributes of telemetry data, are still evolving. This means that the attributes recorded by `opentelemetry-instrumentation-pymemcache` might change in future versions, potentially breaking dashboards, alerts, or analysis tools configured against current attribute names.","severity":"gotcha","affected_versions":"All versions < stable semantic conventions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}