Skip to content

Commit a527618

Browse files
committed
Handle issues introduced by OTLP gRPC protocol
In this commit, we are handling issues that arise from gRPC. Essentially, if we build gRPC artifacts into our Docker image, it causes the Docker image to only be compatible with applications built using the same Python version. To solve this, we are doing two things: 1) we are removing gRPC artifacts from the docker image and 2) we are changing the default OTLP protocol to be HTTP.
1 parent 1219b5d commit a527618

File tree

4 files changed

+96
-23
lines changed

4 files changed

+96
-23
lines changed

Dockerfile

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
# The packages are installed in the `/autoinstrumentation` directory. This is required as when instrumenting the pod by CWOperator,
44
# one init container will be created to copy all the content in `/autoinstrumentation` directory to app's container. Then
55
# update the `PYTHONPATH` environment variable accordingly. Then in the second stage, copy the directory to `/autoinstrumentation`.
6-
7-
# Using Python 3.10 because we are utilizing the opentelemetry-exporter-otlp-proto-grpc exporter,
8-
# which relies on grpcio as a dependency. grpcio has strict dependencies on the OS and Python version.
9-
# Also mentioned in Docker build template in the upstream repository:
10-
# https://github.com/open-telemetry/opentelemetry-operator/blob/b5bb0ae34720d4be2d229dafecb87b61b37699b0/autoinstrumentation/python/requirements.txt#L2
11-
# For further details, please refer to: https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/azure-functions/recover-python-functions.md#the-python-interpre[…]tions-python-worker
12-
FROM python:3.10 AS build
6+
FROM python:3.11 AS build
137

148
WORKDIR /operator-build
159

1610
ADD aws-opentelemetry-distro/ ./aws-opentelemetry-distro/
1711

1812
RUN mkdir workspace && pip install --target workspace ./aws-opentelemetry-distro
1913

14+
# Remove opentelemetry-exporter-otlp-proto-grpc and grpcio, as grpcio has strict dependencies on the Python version and
15+
# will cause confusing failures if gRPC protocol is used. Now if gRPC protocol is requested by the user, instrumentation
16+
# will complain that grpc is not installed, which is more understandable. References:
17+
# * https://github.com/open-telemetry/opentelemetry-operator/blob/b5bb0ae34720d4be2d229dafecb87b61b37699b0/autoinstrumentation/python/requirements.txt#L2
18+
# * https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/azure-functions/recover-python-functions.md#troubleshoot-cannot-import-cygrpc
19+
RUN pip uninstall opentelemetry-exporter-otlp-proto-grpc -y
20+
RUN pip uninstall grpcio -y
21+
2022
FROM public.ecr.aws/amazonlinux/amazonlinux:minimal
2123

2224
# Required to copy attribute files to distributed docker images

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_configurator.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
)
1818
from amazon.opentelemetry.distro.aws_span_metrics_processor_builder import AwsSpanMetricsProcessorBuilder
1919
from amazon.opentelemetry.distro.sampler.aws_xray_remote_sampler import AwsXRayRemoteSampler
20-
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter as OTLPGrpcOTLPMetricExporter
2120
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter as OTLPHttpOTLPMetricExporter
2221
from opentelemetry.sdk._configuration import (
2322
_get_exporter_names,
@@ -274,13 +273,13 @@ def __new__(cls, *args, **kwargs):
274273
# pylint: disable=no-self-use
275274
def create_exporter(self):
276275
protocol = os.environ.get(
277-
OTEL_EXPORTER_OTLP_METRICS_PROTOCOL, os.environ.get(OTEL_EXPORTER_OTLP_PROTOCOL, "grpc")
276+
OTEL_EXPORTER_OTLP_METRICS_PROTOCOL, os.environ.get(OTEL_EXPORTER_OTLP_PROTOCOL, "http/protobuf")
278277
)
279278
_logger.debug("AWS Application Signals export protocol: %s", protocol)
280279

281280
application_signals_endpoint = os.environ.get(
282281
APPLICATION_SIGNALS_EXPORTER_ENDPOINT_CONFIG,
283-
os.environ.get(APP_SIGNALS_EXPORTER_ENDPOINT_CONFIG, "http://localhost:4315"),
282+
os.environ.get(APP_SIGNALS_EXPORTER_ENDPOINT_CONFIG, "http://localhost:4316"),
284283
)
285284

286285
_logger.debug("AWS Application Signals export endpoint: %s", application_signals_endpoint)
@@ -302,6 +301,13 @@ def create_exporter(self):
302301
endpoint=application_signals_endpoint, preferred_temporality=temporality_dict
303302
)
304303
if protocol == "grpc":
304+
# pylint: disable=import-outside-toplevel
305+
# Delay import to only occur if gRPC specifically requested. Vended Docker image will not have gRPC bundled,
306+
# so importing it at the class level can cause runtime failures.
307+
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import (
308+
OTLPMetricExporter as OTLPGrpcOTLPMetricExporter,
309+
)
310+
305311
return OTLPGrpcOTLPMetricExporter(
306312
endpoint=application_signals_endpoint, preferred_temporality=temporality_dict
307313
)

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_distro.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,27 @@
55
from amazon.opentelemetry.distro.patches._instrumentation_patch import apply_instrumentation_patches
66
from opentelemetry.distro import OpenTelemetryDistro
77
from opentelemetry.environment_variables import OTEL_PROPAGATORS, OTEL_PYTHON_ID_GENERATOR
8-
from opentelemetry.sdk.environment_variables import OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION
8+
from opentelemetry.sdk.environment_variables import OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION, \
9+
OTEL_EXPORTER_OTLP_PROTOCOL
910

1011

1112
class AwsOpenTelemetryDistro(OpenTelemetryDistro):
1213
def _configure(self, **kwargs):
13-
"""
14+
""" Sets up default environment variables and apply patches
15+
16+
Set default OTEL_EXPORTER_OTLP_PROTOCOL to be HTTP. This must be run before super(), which attempts to set the
17+
default to gRPC. If we run afterwards, we don't know if the default was set by base OpenTelemetryDistro or if it
18+
was set by the user. We are setting to HTTP as gRPC does not work out of the box for the vended docker image,
19+
due to gRPC having a strict dependency on the Python version the artifact was built for (OTEL observed this:
20+
https://github.com/open-telemetry/opentelemetry-operator/blob/461ba68e80e8ac6bf2603eb353547cd026119ed2/autoinstrumentation/python/requirements.txt#L2-L3)
21+
22+
Also sets default OTEL_PROPAGATORS, OTEL_PYTHON_ID_GENERATOR, and
23+
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION to ensure good compatibility with X-Ray and Application
24+
Signals.
25+
26+
Also applies patches to upstream instrumentation - usually these are stopgap measures until we can contribute
27+
long-term changes to upstream.
28+
1429
kwargs:
1530
apply_patches: bool - apply patches to upstream instrumentation. Default is True.
1631
@@ -19,13 +34,15 @@ def _configure(self, **kwargs):
1934
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION environment variable. Need to work with upstream to
2035
make it to be configurable.
2136
"""
37+
os.environ.setdefault(OTEL_EXPORTER_OTLP_PROTOCOL, "http/protobuf")
38+
2239
super(AwsOpenTelemetryDistro, self)._configure()
40+
41+
os.environ.setdefault(OTEL_PROPAGATORS, "xray,tracecontext,b3,b3multi")
42+
os.environ.setdefault(OTEL_PYTHON_ID_GENERATOR, "xray")
2343
os.environ.setdefault(
2444
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION, "base2_exponential_bucket_histogram"
2545
)
26-
os.environ.setdefault(OTEL_PROPAGATORS, "xray,tracecontext,b3,b3multi")
27-
os.environ.setdefault(OTEL_PYTHON_ID_GENERATOR, "xray")
2846

29-
# Apply patches to upstream instrumentation - usually stopgap measures until we can contribute long-term changes
3047
if kwargs.get("apply_patches", True):
3148
apply_instrumentation_patches()

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelementry_configurator.py

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from unittest import TestCase
66
from unittest.mock import MagicMock, patch
77

8+
from opentelemetry.exporter.otlp.proto.common._internal.metrics_encoder import OTLPMetricExporterMixin
9+
810
from amazon.opentelemetry.distro.always_record_sampler import AlwaysRecordSampler
911
from amazon.opentelemetry.distro.attribute_propagating_span_processor import AttributePropagatingSpanProcessor
1012
from amazon.opentelemetry.distro.aws_metric_attributes_span_exporter import AwsMetricAttributesSpanExporter
@@ -14,13 +16,15 @@
1416
_customize_exporter,
1517
_customize_sampler,
1618
_customize_span_processors,
17-
_is_application_signals_enabled,
19+
_is_application_signals_enabled, ApplicationSignalsExporterProvider,
1820
)
1921
from amazon.opentelemetry.distro.aws_opentelemetry_distro import AwsOpenTelemetryDistro
2022
from amazon.opentelemetry.distro.aws_span_metrics_processor import AwsSpanMetricsProcessor
2123
from amazon.opentelemetry.distro.sampler._aws_xray_sampling_client import _AwsXRaySamplingClient
2224
from amazon.opentelemetry.distro.sampler.aws_xray_remote_sampler import AwsXRayRemoteSampler
2325
from opentelemetry.environment_variables import OTEL_LOGS_EXPORTER, OTEL_METRICS_EXPORTER, OTEL_TRACES_EXPORTER
26+
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter as OTLPGrpcOTLPMetricExporter
27+
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter as OTLPHttpOTLPMetricExporter
2428
from opentelemetry.sdk.environment_variables import OTEL_TRACES_SAMPLER, OTEL_TRACES_SAMPLER_ARG
2529
from opentelemetry.sdk.resources import Resource
2630
from opentelemetry.sdk.trace import Span, SpanProcessor, Tracer, TracerProvider
@@ -29,18 +33,27 @@
2933
from opentelemetry.trace import get_tracer_provider
3034

3135

32-
# This class setup Tracer Provider Globally, which can only set once
33-
# if there is another setup for tracer provider, may cause issue
3436
class TestAwsOpenTelemetryConfigurator(TestCase):
37+
""" Tests AwsOpenTelemetryConfigurator and AwsOpenTelemetryDistro
38+
39+
NOTE: This class setup Tracer Provider Globally, which can only be set once. If there is another setup for tracer
40+
provider, it may cause issues for those tests.
41+
"""
3542
@classmethod
3643
def setUpClass(cls):
37-
os.environ.setdefault(OTEL_TRACES_EXPORTER, "none")
38-
os.environ.setdefault(OTEL_METRICS_EXPORTER, "none")
39-
os.environ.setdefault(OTEL_LOGS_EXPORTER, "none")
40-
os.environ.setdefault(OTEL_TRACES_SAMPLER, "traceidratio")
41-
os.environ.setdefault(OTEL_TRACES_SAMPLER_ARG, "0.01")
44+
# Run AwsOpenTelemetryDistro to set up environment, then validate expected env values.
4245
aws_open_telemetry_distro: AwsOpenTelemetryDistro = AwsOpenTelemetryDistro()
4346
aws_open_telemetry_distro.configure(apply_patches=False)
47+
validate_distro_environ()
48+
49+
# Overwrite exporter configs to keep tests clean, set sampler configs for tests
50+
os.environ[OTEL_TRACES_EXPORTER] = "none"
51+
os.environ[OTEL_METRICS_EXPORTER] = "none"
52+
os.environ[OTEL_LOGS_EXPORTER] = "none"
53+
os.environ[OTEL_TRACES_SAMPLER] = "traceidratio"
54+
os.environ[OTEL_TRACES_SAMPLER_ARG] = "0.01"
55+
56+
# Run configurator and get trace provider
4457
aws_otel_configurator: AwsOpenTelemetryConfigurator = AwsOpenTelemetryConfigurator()
4558
aws_otel_configurator.configure()
4659
cls.tracer_provider: TracerProvider = get_tracer_provider()
@@ -249,3 +262,38 @@ def test_customize_span_processors(self):
249262
second_processor: SpanProcessor = mock_tracer_provider.add_span_processor.call_args_list[1].args[0]
250263
self.assertIsInstance(second_processor, AwsSpanMetricsProcessor)
251264
os.environ.pop("OTEL_AWS_APPLICATION_SIGNALS_ENABLED", None)
265+
266+
def test_application_signals_exporter_provider(self):
267+
# Check default protocol - HTTP, as specified by AwsOpenTelemetryDistro
268+
exporter: OTLPMetricExporterMixin = ApplicationSignalsExporterProvider().create_exporter()
269+
self.assertIsInstance(exporter, OTLPHttpOTLPMetricExporter)
270+
self.assertEqual( "http://localhost:4316", exporter._endpoint)
271+
272+
# Overwrite protocol to gRPC. Note that this causes `http://` to be stripped from endpoint
273+
os.environ["OTEL_EXPORTER_OTLP_PROTOCOL"] = "grpc"
274+
exporter: SpanExporter = ApplicationSignalsExporterProvider().create_exporter()
275+
self.assertIsInstance(exporter, OTLPGrpcOTLPMetricExporter)
276+
self.assertEqual("localhost:4316", exporter._endpoint)
277+
278+
# Overwrite protocol back to HTTP. Note that `http://` comes back to endpoint
279+
os.environ["OTEL_EXPORTER_OTLP_PROTOCOL"] = "http/protobuf"
280+
exporter: SpanExporter = ApplicationSignalsExporterProvider().create_exporter()
281+
self.assertIsInstance(exporter, OTLPHttpOTLPMetricExporter)
282+
self.assertEqual("http://localhost:4316", exporter._endpoint)
283+
284+
def validate_distro_environ():
285+
tc: TestCase = TestCase()
286+
# Set by OpenTelemetryDistro
287+
tc.assertEqual("otlp", os.environ.get("OTEL_TRACES_EXPORTER"))
288+
tc.assertEqual("otlp", os.environ.get("OTEL_METRICS_EXPORTER"))
289+
290+
# Set by AwsOpenTelemetryDistro
291+
tc.assertEqual("http/protobuf", os.environ.get("OTEL_EXPORTER_OTLP_PROTOCOL"))
292+
tc.assertEqual("base2_exponential_bucket_histogram",
293+
os.environ.get("OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION"))
294+
tc.assertEqual("xray,tracecontext,b3,b3multi", os.environ.get("OTEL_PROPAGATORS"))
295+
tc.assertEqual("xray", os.environ.get("OTEL_PYTHON_ID_GENERATOR"))
296+
297+
# Not set
298+
tc.assertEqual(None, os.environ.get("OTEL_TRACES_SAMPLER"))
299+
tc.assertEqual(None, os.environ.get("OTEL_TRACES_SAMPLER_ARG"))

0 commit comments

Comments
 (0)