Skip to content

Commit 2007cde

Browse files
committed
add new example for multi-destination exporting
1 parent 9c270da commit 2007cde

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Multi-Destination Exporting
2+
===========================
3+
4+
This example shows how to export telemetry data to multiple destinations
5+
simultaneously. As described in the `OTLP specification
6+
<https://opentelemetry.io/docs/specs/otlp/#multi-destination-exporting>`_,
7+
each destination should have implemented its own queuing, acknowledgement
8+
handling, and retry logic to prevent one slow or unavailable destination
9+
from blocking the others.
10+
11+
The OpenTelemetry Python SDK achieves this by using a separate processor
12+
or reader per destination:
13+
14+
* **Traces**: Use one ``BatchSpanProcessor`` per destination, each wrapping
15+
its own ``SpanExporter``. Add each processor to the ``TracerProvider``
16+
via ``add_span_processor()``.
17+
18+
* **Metrics**: Pass multiple ``MetricReader`` instances (each wrapping its
19+
own ``MetricExporter``) to the ``MeterProvider`` constructor via the
20+
``metric_readers`` parameter.
21+
22+
* **Logs**: Use one ``BatchLogRecordProcessor`` per destination, each
23+
wrapping its own ``LogExporter``. Add each processor to the
24+
``LoggerProvider`` via ``add_log_record_processor()``.
25+
26+
.. note::
27+
28+
The **Profiles** signal is not yet supported in the Python SDK.
29+
When it becomes available, the same pattern will apply.
30+
31+
The source files of these examples are available :scm_web:`here <docs/examples/multi-destination-exporting/>`.
32+
33+
Installation
34+
------------
35+
36+
.. code-block:: sh
37+
38+
pip install opentelemetry-api
39+
pip install opentelemetry-sdk
40+
pip install opentelemetry-exporter-otlp-proto-grpc
41+
pip install opentelemetry-exporter-otlp-proto-http
42+
43+
Run the Example
44+
---------------
45+
46+
.. code-block:: sh
47+
48+
python multi_destination_traces.py
49+
python multi_destination_metrics.py
50+
python multi_destination_logs.py
51+
52+
The output will be shown in the console for the ``ConsoleSpanExporter``
53+
and ``ConsoleMetricExporter`` destinations. The OTLP destinations require
54+
a running collector.
55+
56+
Useful links
57+
------------
58+
59+
- `OTLP multi-destination exporting spec <https://opentelemetry.io/docs/specs/otlp/#multi-destination-exporting>`_
60+
- OpenTelemetry_
61+
- :doc:`../../api/trace`
62+
63+
.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
This example shows how to export logs to multiple destinations.
17+
Each BatchLogRecordProcessor has its own queue and retry logic, so
18+
destinations do not block each other.
19+
"""
20+
21+
import logging
22+
23+
from opentelemetry._logs import set_logger_provider
24+
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import (
25+
OTLPLogExporter as GrpcLogExporter,
26+
)
27+
from opentelemetry.exporter.otlp.proto.http._log_exporter import (
28+
OTLPLogExporter as HttpLogExporter,
29+
)
30+
from opentelemetry.sdk._logs import LoggerProvider
31+
from opentelemetry.sdk._logs.export import (
32+
BatchLogRecordProcessor,
33+
ConsoleLogRecordExporter,
34+
)
35+
36+
logger_provider = LoggerProvider()
37+
set_logger_provider(logger_provider)
38+
39+
# Destination 1: OTLP over gRPC
40+
grpc_exporter = GrpcLogExporter(
41+
endpoint="http://localhost:4317", insecure=True
42+
)
43+
logger_provider.add_log_record_processor(
44+
BatchLogRecordProcessor(grpc_exporter)
45+
)
46+
47+
# Destination 2: OTLP over HTTP
48+
http_exporter = HttpLogExporter(
49+
endpoint="http://localhost:4318/v1/logs"
50+
)
51+
logger_provider.add_log_record_processor(
52+
BatchLogRecordProcessor(http_exporter)
53+
)
54+
55+
# Destination 3: Console (for debugging)
56+
logger_provider.add_log_record_processor(
57+
BatchLogRecordProcessor(ConsoleLogRecordExporter())
58+
)
59+
60+
# Use Python's standard logging, bridged to OpenTelemetry
61+
logger = logging.getLogger("myapp")
62+
logger.setLevel(logging.INFO)
63+
logger.info("Logs are exported to all three destinations.")
64+
logger.warning("This warning also goes everywhere.")
65+
66+
logger_provider.shutdown()
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
This example shows how to export metrics to multiple destinations.
17+
Each PeriodicExportingMetricReader has its own collection interval
18+
and export queue, so destinations do not block each other.
19+
"""
20+
21+
from opentelemetry import metrics
22+
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import (
23+
OTLPMetricExporter as GrpcMetricExporter,
24+
)
25+
from opentelemetry.exporter.otlp.proto.http.metric_exporter import (
26+
OTLPMetricExporter as HttpMetricExporter,
27+
)
28+
from opentelemetry.sdk.metrics import MeterProvider
29+
from opentelemetry.sdk.metrics.export import (
30+
ConsoleMetricExporter,
31+
PeriodicExportingMetricReader,
32+
)
33+
34+
# Each reader has its own export interval and exporter
35+
grpc_reader = PeriodicExportingMetricReader(
36+
GrpcMetricExporter(endpoint="http://localhost:4317", insecure=True)
37+
)
38+
http_reader = PeriodicExportingMetricReader(
39+
HttpMetricExporter(endpoint="http://localhost:4318/v1/metrics")
40+
)
41+
console_reader = PeriodicExportingMetricReader(ConsoleMetricExporter())
42+
43+
# Pass all readers to the MeterProvider
44+
provider = MeterProvider(metric_readers=[grpc_reader, http_reader, console_reader])
45+
metrics.set_meter_provider(provider)
46+
47+
meter = metrics.get_meter(__name__)
48+
counter = meter.create_counter("request.count", description="Number of requests")
49+
50+
counter.add(1, {"endpoint": "/api/users"})
51+
counter.add(1, {"endpoint": "/api/orders"})
52+
53+
print("Metrics are exported to all three destinations.")
54+
55+
provider.shutdown()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
This example shows how to export traces to multiple destinations.
17+
Each BatchSpanProcessor has its own queue and retry logic, so
18+
destinations do not block each other.
19+
"""
20+
21+
from opentelemetry import trace
22+
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
23+
OTLPSpanExporter as GrpcSpanExporter,
24+
)
25+
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
26+
OTLPSpanExporter as HttpSpanExporter,
27+
)
28+
from opentelemetry.sdk.trace import TracerProvider
29+
from opentelemetry.sdk.trace.export import (
30+
BatchSpanProcessor,
31+
ConsoleSpanExporter,
32+
)
33+
34+
provider = TracerProvider()
35+
trace.set_tracer_provider(provider)
36+
37+
# Destination 1: OTLP over gRPC
38+
grpc_exporter = GrpcSpanExporter(
39+
endpoint="http://localhost:4317", insecure=True
40+
)
41+
provider.add_span_processor(BatchSpanProcessor(grpc_exporter))
42+
43+
# Destination 2: OTLP over HTTP
44+
http_exporter = HttpSpanExporter(
45+
endpoint="http://localhost:4318/v1/traces"
46+
)
47+
provider.add_span_processor(BatchSpanProcessor(http_exporter))
48+
49+
# Destination 3: Console (for debugging)
50+
provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
51+
52+
tracer = trace.get_tracer(__name__)
53+
54+
with tracer.start_as_current_span("example-request"):
55+
with tracer.start_as_current_span("fetch-data"):
56+
print("Spans are exported to all three destinations.")
57+
58+
provider.shutdown()

0 commit comments

Comments
 (0)