forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatic exporter/provider setup for opentelemetry-instrument command.
open-telemetry#1036 This commit adds support to the opentelemetry-instrument command to automatically configure a tracer provider and exporter. By default, it configures the OTLP exporter (like other Otel auto-instrumentations. e.g, Java: https://github.com/open-telemetry/opentelemetry-java-instrumentation#getting-started). It also allows using a different in-built or 3rd party via a CLI argument or env variable. Details can be found on opentelemetry-instrumentation's README package. Fixes open-telemetry#663
- Loading branch information
Showing
9 changed files
with
579 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
...lemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/tracing.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from collections import defaultdict | ||
from functools import partial | ||
from logging import getLogger | ||
|
||
from pkg_resources import iter_entry_points | ||
|
||
from opentelemetry import metrics, trace | ||
from opentelemetry.configuration import Configuration | ||
from opentelemetry.instrumentation import symbols | ||
from opentelemetry.sdk.resources import Resource | ||
|
||
logger = getLogger(__file__) | ||
|
||
|
||
# Defaults | ||
_DEFAULT_TRACE_EXPORTER = symbols.exporter_otlp | ||
_DEFAULT_TRACER_PROVIDER = "opentelemetry.sdk.trace.TracerProvider" | ||
_DEFAULT_SPAN_PROCESSOR = ( | ||
"opentelemetry.sdk.trace.export.BatchExportSpanProcessor" | ||
) | ||
|
||
|
||
_trace_exporter_classes = { | ||
symbols.exporter_dd: "opentelemetry.exporter.datadog.DatadogSpanExporter", | ||
symbols.exporter_oc: "opentelemetry.exporter.opencensus.trace_exporter.OpenCensusSpanExporter", | ||
symbols.exporter_otlp: "opentelemetry.exporter.otlp.trace_exporter.OTLPSpanExporter", | ||
symbols.exporter_jaeger: "opentelemetry.exporter.jaeger.JaegerSpanExporter", | ||
symbols.exporter_zipkin: "opentelemetry.exporter.zipkin.ZipkinSpanExporter", | ||
} | ||
|
||
_span_processors_by_exporter = defaultdict( | ||
lambda: _DEFAULT_SPAN_PROCESSOR, | ||
{ | ||
symbols.exporter_dd: "opentelemetry.exporter.datadog.DatadogExportSpanProcessor", | ||
}, | ||
) | ||
|
||
|
||
def get_service_name(): | ||
return Configuration().SERVICE_NAME or "" | ||
|
||
|
||
def get_tracer_provider(): | ||
return Configuration().TRACER_PROVIDER or _DEFAULT_TRACER_PROVIDER | ||
|
||
|
||
def get_exporter_name(): | ||
return Configuration().TRACE_EXPORTER or _DEFAULT_TRACE_EXPORTER | ||
|
||
|
||
def _trace_init( | ||
trace_exporter, tracer_provider, span_processor, | ||
): | ||
exporter = trace_exporter() | ||
processor = span_processor(exporter) | ||
provider = tracer_provider() | ||
trace.set_tracer_provider(provider) | ||
provider.add_span_processor(processor) | ||
|
||
|
||
def _default_trace_init(exporter, provider, processor): | ||
service_name = get_service_name() | ||
if service_name: | ||
exporter = partial(exporter, service_name=get_service_name()) | ||
_trace_init(exporter, provider, processor) | ||
|
||
|
||
def _otlp_trace_init(exporter, provider, processor): | ||
resource = Resource(labels={"service_name": get_service_name()}) | ||
provider = partial(provider, resource=resource) | ||
_trace_init(exporter, provider, processor) | ||
|
||
|
||
def _dd_trace_init(exporter, provider, processor): | ||
exporter = partial(exporter, service=get_service_name()) | ||
_trace_init(exporter, provider, processor) | ||
|
||
|
||
_initializers = defaultdict( | ||
lambda: _default_trace_init, | ||
{ | ||
symbols.exporter_dd: _dd_trace_init, | ||
symbols.exporter_otlp: _otlp_trace_init, | ||
}, | ||
) | ||
|
||
|
||
def _import(import_path): | ||
split_path = import_path.rsplit(".", 1) | ||
if len(split_path) < 2: | ||
raise ImportError( | ||
"could not import module or class: {0}".format(import_path) | ||
) | ||
module, class_name = split_path | ||
mod = __import__(module, fromlist=[class_name]) | ||
return getattr(mod, class_name) | ||
|
||
|
||
def _load_component(components, name): | ||
if name.lower() == "none": | ||
return None | ||
|
||
component = components.get(name.lower(), name) | ||
if not component: | ||
logger.info("component not found with name: {0}".format(name)) | ||
return | ||
|
||
if isinstance(component, str): | ||
try: | ||
return _import(component) | ||
except ImportError as exc: | ||
logger.error(exc.msg) | ||
return None | ||
return component | ||
|
||
|
||
def initialize_tracing(): | ||
exporter_name = get_exporter_name() | ||
print("exporter: ", get_exporter_name()) | ||
TraceExporter = _load_component(_trace_exporter_classes, exporter_name) | ||
if TraceExporter is None: | ||
logger.info("not using any trace exporter") | ||
return | ||
|
||
print("provider: ", get_tracer_provider()) | ||
TracerProvider = _load_component({}, get_tracer_provider()) | ||
SpanProcessor = _import(_span_processors_by_exporter[exporter_name]) | ||
initializer = _initializers[exporter_name] | ||
initializer(TraceExporter, TracerProvider, SpanProcessor) |
Oops, something went wrong.