Skip to content

Commit

Permalink
[opentelemetry][callback] Add support for http exporter
Browse files Browse the repository at this point in the history
The previous version of the callback was supporting only the grpc
exporter. This was counter intuitive as the documentation was
mentioning `<your endpoint (OTLP/HTTP)>`. Users were left with a error
similar to
`Transient error StatusCode.UNAVAILABLE encountered while exporting traces to <endpoint>, retrying in 1s.`

The following commit fix this situation by support both HTTP and GRPC
via the standard environment variables and ansible.cfg

See as well #7888

Signed-off-by: Wilfried Roset <wilfriedroset@users.noreply.github.com>
  • Loading branch information
wilfriedroset committed May 6, 2024
1 parent bc609d7 commit 9f18813
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8321-fix-opentelemetry-callback.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- opentelemetry - add support for HTTP trace_exporter and configures the behavior via ``OTEL_EXPORTER_OTLP_TRACES_PROTOCOL``
46 changes: 42 additions & 4 deletions plugins/callback/opentelemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@
- section: callback_opentelemetry
key: disable_attributes_in_logs
version_added: 7.1.0
otel_exporter_otlp_traces_protocol:
default: grpc
type: str
description:
- OTEL_EXPORTER_OTLP_TRACES_PROTOCOL represents the the transport protocol for spans.
- See
U(https://opentelemetry-python.readthedocs.io/en/latest/sdk/environment_variables.html#envvar-OTEL_EXPORTER_OTLP_TRACES_PROTOCOL).
env:
- name: OTEL_EXPORTER_OTLP_TRACES_PROTOCOL
ini:
- section: callback_opentelemetry
key: otel_exporter_otlp_traces_protocol
version_added: 8.7.0
requirements:
- opentelemetry-api (Python library)
- opentelemetry-exporter-otlp (Python library)
Expand Down Expand Up @@ -124,7 +137,8 @@
try:
from opentelemetry import trace
from opentelemetry.trace import SpanKind
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter as grpcOTLPSpanExporter
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter as httpOTLPSpanExporter
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.trace.status import Status, StatusCode
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
Expand Down Expand Up @@ -255,7 +269,15 @@ def finish_task(self, tasks_data, status, result, dump):
task.dump = dump
task.add_host(HostData(host_uuid, host_name, status, result))

def generate_distributed_traces(self, otel_service_name, ansible_playbook, tasks_data, status, traceparent, disable_logs, disable_attributes_in_logs):
def generate_distributed_traces(self,
otel_service_name,
ansible_playbook,
tasks_data,
status,
traceparent,
disable_logs,
disable_attributes_in_logs,
otel_exporter_otlp_traces_protocol):
""" generate distributed traces from the collected TaskData and HostData """

tasks = []
Expand All @@ -271,7 +293,11 @@ def generate_distributed_traces(self, otel_service_name, ansible_playbook, tasks
)
)

processor = BatchSpanProcessor(OTLPSpanExporter())
processor = None
if otel_exporter_otlp_traces_protocol == 'grpc':
processor = BatchSpanProcessor(grpcOTLPSpanExporter())
else:
processor = BatchSpanProcessor(httpOTLPSpanExporter())

trace.get_tracer_provider().add_span_processor(processor)

Expand Down Expand Up @@ -462,6 +488,7 @@ def __init__(self, display=None):
self.errors = 0
self.disabled = False
self.traceparent = False
self.otel_exporter_otlp_traces_protocol = None

if OTEL_LIBRARY_IMPORT_ERROR:
raise_from(
Expand Down Expand Up @@ -497,6 +524,16 @@ def set_options(self, task_keys=None, var_options=None, direct=None):
# See https://github.com/open-telemetry/opentelemetry-specification/issues/740
self.traceparent = self.get_option('traceparent')

self.otel_exporter_otlp_traces_protocol = self.get_option('otel_exporter_otlp_traces_protocol')
if not self.otel_exporter_otlp_traces_protocol:
self.otel_exporter_otlp_traces_protocol = 'grpc'
elif self.otel_exporter_otlp_traces_protocol not in ['grpc', 'http/protobuf']:
self.disabled = True
self._display.warning(
"The `otel_exporter_otlp_traces_protocol` option has been set with an unsupported value `{0}`. "
"Disabling the `opentelemetry` callback plugin.".format(self.otel_exporter_otlp_traces_protocol)
)

def v2_playbook_on_start(self, playbook):
self.ansible_playbook = basename(playbook._file_name)

Expand Down Expand Up @@ -585,7 +622,8 @@ def v2_playbook_on_stats(self, stats):
status,
self.traceparent,
self.disable_logs,
self.disable_attributes_in_logs
self.disable_attributes_in_logs,
self.otel_exporter_otlp_traces_protocol
)

def v2_runner_on_async_failed(self, result, **kwargs):
Expand Down

0 comments on commit 9f18813

Please sign in to comment.