Skip to main content

OpenTelemetry span exporters for AWS SQS and Azure Service Bus

Project description

OpenTelemetry Trace Exporter

A Python package for exporting OpenTelemetry traces to AWS SQS and Azure Service Bus in OTLP format (Protobuf or JSON).

Features

  • AWS SQS Exporter: Export traces to AWS SQS queues
  • Azure Service Bus Exporter: Export traces to Azure Service Bus queues
  • OTLP Format Support: Both Protobuf (base64 encoded) and JSON formats
  • Batch Processing: Efficient batch span processing
  • Environment-based Configuration: Flexible configuration for development and production
  • Error Handling: Graceful error handling to avoid blocking applications
  • Message Size Validation: Automatic validation against cloud provider limits

Installation

Base Installation

pip install otel-messagequeue-exporter

With AWS Support

pip install otel-messagequeue-exporter[aws]

With Azure Support

pip install otel-messagequeue-exporter[azure]

With All Exporters

pip install otel-messagequeue-exporter[all]

Quick Start

AWS SQS Exporter

import os
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.resources import Resource, SERVICE_NAME

from otel_messagequeue_exporter import SQSSpanExporter

# Create resource
resource = Resource.create(attributes={SERVICE_NAME: "my-service"})

# Create tracer provider
provider = TracerProvider(resource=resource)

# Create SQS exporter
exporter = SQSSpanExporter(
    queue_url="https://sqs.us-east-1.amazonaws.com/123456789/traces",
    region_name="us-east-1",
    encoding="otlp_proto",  # or "otlp_json"
)

# Add batch processor
processor = BatchSpanProcessor(exporter)
provider.add_span_processor(processor)

# Set as global tracer provider
trace.set_tracer_provider(provider)

# Use tracing
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("my_operation"):
    print("Doing work...")

Azure Service Bus Exporter

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.resources import Resource, SERVICE_NAME

from otel_messagequeue_exporter import AzureServiceBusSpanExporter

# Create resource
resource = Resource.create(attributes={SERVICE_NAME: "my-service"})

# Create tracer provider
provider = TracerProvider(resource=resource)

# Create Azure Service Bus exporter
exporter = AzureServiceBusSpanExporter(
    connection_string="Endpoint=sb://namespace.servicebus.windows.net/;SharedAccessKeyName=...",
    queue_name="traces",
    encoding="otlp_proto",  # or "otlp_json"
)

# Add batch processor
processor = BatchSpanProcessor(exporter)
provider.add_span_processor(processor)

# Set as global tracer provider
trace.set_tracer_provider(provider)

# Use tracing
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("my_operation"):
    print("Doing work...")

Configuration

SQSSpanExporter Parameters

  • queue_url (str, required): AWS SQS queue URL
  • region_name (str, default: "us-east-1"): AWS region name
  • encoding (str, default: "otlp_proto"): Message encoding format ("otlp_proto" or "otlp_json")
  • aws_access_key_id (str, optional): AWS access key ID (for development)
  • aws_secret_access_key (str, optional): AWS secret access key (for development)

AzureServiceBusSpanExporter Parameters

  • connection_string (str, required): Azure Service Bus connection string
  • queue_name (str, required): Azure Service Bus queue name
  • encoding (str, default: "otlp_proto"): Message encoding format ("otlp_proto" or "otlp_json")
  • servicebus_namespace (str, optional): Azure Service Bus namespace (for logging)

Environment-based Configuration

The SQS exporter supports environment-based authentication:

import os

# For development (uses explicit credentials)
os.environ["ENV"] = "DEV"
os.environ["SQS_AWS_ACCESS_KEY_ID"] = "your-access-key"
os.environ["SQS_AWS_SECRET_ACCESS_KEY"] = "your-secret-key"

# For production (uses IAM role)
# Don't set ENV="DEV", and the exporter will use default credentials

Advanced Usage

Batch Processor Configuration

from opentelemetry.sdk.trace.export import BatchSpanProcessor

processor = BatchSpanProcessor(
    exporter,
    max_queue_size=2048,  # Maximum queue size
    max_export_batch_size=512,  # Maximum batch size
    schedule_delay_millis=5000,  # Export interval in milliseconds
    export_timeout_millis=30000,  # Export timeout in milliseconds
)

Graceful Shutdown

import atexit

def shutdown_tracing():
    provider.force_flush(timeout_millis=30000)
    provider.shutdown()

atexit.register(shutdown_tracing)

Adding Span Attributes and Events

tracer = trace.get_tracer(__name__)

with tracer.start_as_current_span("my_operation") as span:
    # Add attributes
    span.set_attribute("user_id", "12345")
    span.set_attribute("operation_type", "example")

    # Add events
    span.add_event("processing_started", {"item_count": 10})

    # Your code here
    print("Doing work...")

    # Add another event
    span.add_event("processing_completed", {"items_processed": 10})

Message Format

OTLP Protobuf Format

When using encoding="otlp_proto", spans are serialized to OTLP protobuf format and base64 encoded before being sent to the message queue. This is the most efficient format and is recommended for production use.

OTLP JSON Format

When using encoding="otlp_json", spans are serialized to OTLP JSON format. This format is human-readable and useful for debugging, but less efficient than protobuf.

Message Size Limits

  • AWS SQS: 256KB maximum message size
  • Azure Service Bus: 256KB (Standard tier), 1MB (Premium tier)

The exporters automatically check message sizes and log warnings/errors if limits are exceeded.

Examples

See the examples directory for complete examples:

Development

Development Installation

# Clone the repository
git clone https://github.com/NeuralgoLyzr/otel-messagequeue-exporter.git
cd otel-messagequeue-exporter

# Install in editable mode with dev dependencies
pip install -e ".[dev]"

Running Tests

pytest

Code Formatting

black src/ tests/

Type Checking

mypy src/

Linting

flake8 src/ tests/

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

otel_messagequeue_exporter-0.1.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

otel_messagequeue_exporter-0.1.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file otel_messagequeue_exporter-0.1.0.tar.gz.

File metadata

  • Download URL: otel_messagequeue_exporter-0.1.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for otel_messagequeue_exporter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b2e4a501556f63220fddefa5d31b950780a885660a61d79f795cf63a10ec0076
MD5 e00495ca9331c55e69b2736cbcdc47be
BLAKE2b-256 31d842cce61df530435ec0b14761d94785ee667edd469c475ffac90728fd4f4b

See more details on using hashes here.

File details

Details for the file otel_messagequeue_exporter-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: otel_messagequeue_exporter-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for otel_messagequeue_exporter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 269eb95abdef134a04f90f437c6b2c0f928b6ddde19f516017090febcd6d6c34
MD5 ee7b73a1a4ae3b0d7411a968fefab1f3
BLAKE2b-256 9a3415c1cebe8156b9ad01260b7fa01ce49bfd679fd0ce6b4dedc973b4950833

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page