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.
Add true auto-instrumentation support to opentelemetry-instrument
This commit extends the instrument command so it automatically configures tracing with a provider, span processor and exporter. Most of the component used can be customized with env vars or CLI arguments. Details can be found on opentelemetry-instrumentation's README package. Fixes open-telemetry#663
- Loading branch information
Showing
9 changed files
with
506 additions
and
31 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
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
157 changes: 157 additions & 0 deletions
157
...etry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/components.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,157 @@ | ||
# 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 logging import getLogger | ||
from typing import Sequence, Tuple | ||
|
||
from opentelemetry import trace | ||
from opentelemetry.configuration import Configuration | ||
from opentelemetry.instrumentation import symbols | ||
from opentelemetry.sdk.metrics.export import MetricsExporter | ||
from opentelemetry.sdk.resources import Resource | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import ( | ||
BatchExportSpanProcessor, | ||
SpanExporter, | ||
SpanProcessor, | ||
) | ||
|
||
logger = getLogger(__file__) | ||
|
||
_DEFAULT_EXPORTER = symbols.exporter_otlp | ||
|
||
known_exporters = { | ||
symbols.exporter_otlp: ( | ||
"opentelemetry.exporter.otlp.trace_exporter.OTLPSpanExporter", | ||
"opentelemetry.exporter.otlp.metrics_exporter.OTLPMetricsExporter", | ||
), | ||
symbols.exporter_dd: ( | ||
"opentelemetry.exporter.datadog.DatadogSpanExporter", | ||
), | ||
symbols.exporter_oc: ( | ||
"opentelemetry.exporter.opencensus.trace_exporter.OpenCensusSpanExporter", | ||
), | ||
symbols.exporter_otlp_span: ( | ||
"opentelemetry.exporter.otlp.trace_exporter.OTLPSpanExporter", | ||
), | ||
symbols.exporter_otlp_metric: ( | ||
"opentelemetry.exporter.otlp.metrics_exporter.OTLPMetricsExporter" | ||
), | ||
symbols.exporter_jaeger: ( | ||
"opentelemetry.exporter.jaeger.JaegerSpanExporter", | ||
), | ||
symbols.exporter_zipkin: ( | ||
"opentelemetry.exporter.zipkin.ZipkinSpanExporter", | ||
), | ||
symbols.exporter_prometheus: ( | ||
"opentelemetry.exporter.prometheus.PrometheusMetricsExporter", | ||
), | ||
} | ||
|
||
|
||
def _import(import_path: str) -> any: | ||
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 get_service_name() -> str: | ||
return Configuration().SERVICE_NAME or "" | ||
|
||
|
||
def get_exporter_names() -> Sequence[str]: | ||
exporter = Configuration().EXPORTER or _DEFAULT_EXPORTER | ||
if exporter.lower().strip() == "none": | ||
return [] | ||
|
||
return [e.strip() for e in exporter.split(",")] | ||
|
||
|
||
def get_tracer_provider_class() -> trace.TracerProvider: | ||
return TracerProvider | ||
|
||
|
||
def get_processor_class_for_exporter(exporter_name: str) -> SpanProcessor: | ||
if exporter_name == symbols.exporter_dd: | ||
return _import( | ||
"opentelemetry.exporter.datadog.DatadogExportSpanProcessor" | ||
) | ||
return BatchExportSpanProcessor | ||
|
||
|
||
def init_tracing(exporters: Sequence[SpanExporter]): | ||
service_name = get_service_name() | ||
provider = get_tracer_provider_class()( | ||
resource=Resource.create({"service.name": service_name}), | ||
) | ||
trace.set_tracer_provider(provider) | ||
|
||
for exporter_name, exporter_class in exporters.items(): | ||
processor_class = get_processor_class_for_exporter(exporter_name) | ||
|
||
exporter_args = {} | ||
if exporter_name == symbols.exporter_dd: | ||
exporter_args["service"] = service_name | ||
elif exporter_name not in [ | ||
symbols.exporter_otlp, | ||
symbols.exporter_otlp_span, | ||
]: | ||
exporter_args["service_name"] = service_name | ||
|
||
provider.add_span_processor( | ||
processor_class(exporter_class(**exporter_args)) | ||
) | ||
|
||
|
||
def init_metrics(exporters: Sequence[MetricsExporter]): | ||
if exporters: | ||
logger.warning("automatic metric initialization is not supported yet.") | ||
|
||
|
||
def import_exporters( | ||
exporter_names: Sequence[str], | ||
) -> Tuple[Sequence[SpanExporter], Sequence[MetricsExporter]]: | ||
trace_exporters, metric_exporters = {}, {} | ||
for exporter_name in exporter_names: | ||
for exporter_path in known_exporters.get( | ||
exporter_name, [exporter_name] | ||
): | ||
exporter_impl = _import(exporter_path) | ||
if issubclass(exporter_impl, SpanExporter): | ||
trace_exporters[exporter_name] = exporter_impl | ||
elif issubclass(exporter_impl, MetricsExporter): | ||
metric_exporters[exporter_name] = exporter_impl | ||
else: | ||
raise RuntimeError( | ||
"{0} ({1}) is neither a trace exporter nor a metric exporter".format( | ||
exporter_name, exporter_path | ||
) | ||
) | ||
return trace_exporters, metric_exporters | ||
|
||
|
||
def initialize_components(): | ||
exporter_names = get_exporter_names() | ||
trace_exporters, metric_exporters = import_exporters(exporter_names) | ||
init_tracing(trace_exporters) | ||
|
||
# We don't support automatic initialization for metric yet but have added | ||
# some boilerplate in order to make sure current implementation does not | ||
# lock us out of supporting metrics later without major surgery. | ||
init_metrics(metric_exporters) |
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
Oops, something went wrong.