|
| 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 | +from logging import getLogger |
| 16 | +from typing import Sequence, Tuple |
| 17 | + |
| 18 | +from pkg_resources import iter_entry_points |
| 19 | + |
| 20 | +from opentelemetry import trace |
| 21 | +from opentelemetry.configuration import Configuration |
| 22 | +from opentelemetry.sdk.metrics.export import MetricsExporter |
| 23 | +from opentelemetry.sdk.resources import Resource |
| 24 | +from opentelemetry.sdk.trace import TracerProvider |
| 25 | +from opentelemetry.sdk.trace.export import ( |
| 26 | + BatchExportSpanProcessor, |
| 27 | + SpanExporter, |
| 28 | +) |
| 29 | + |
| 30 | +logger = getLogger(__file__) |
| 31 | + |
| 32 | +EXPORTER_OTLP = "otlp" |
| 33 | +EXPORTER_OTLP_SPAN = "otlp_span" |
| 34 | +EXPORTER_OTLP_METRIC = "otlp_metric" |
| 35 | +_DEFAULT_EXPORTER = EXPORTER_OTLP |
| 36 | + |
| 37 | + |
| 38 | +def get_service_name() -> str: |
| 39 | + return Configuration().SERVICE_NAME or "" |
| 40 | + |
| 41 | + |
| 42 | +def get_exporter_names() -> Sequence[str]: |
| 43 | + exporter = Configuration().EXPORTER or _DEFAULT_EXPORTER |
| 44 | + if exporter.lower().strip() == "none": |
| 45 | + return [] |
| 46 | + |
| 47 | + names = [] |
| 48 | + for exp in exporter.split(","): |
| 49 | + name = exp.strip() |
| 50 | + if name == EXPORTER_OTLP: |
| 51 | + names.append(EXPORTER_OTLP_SPAN) |
| 52 | + names.append(EXPORTER_OTLP_METRIC) |
| 53 | + else: |
| 54 | + names.append(name) |
| 55 | + return names |
| 56 | + |
| 57 | + |
| 58 | +def init_tracing(exporters: Sequence[SpanExporter]): |
| 59 | + service_name = get_service_name() |
| 60 | + provider = TracerProvider( |
| 61 | + resource=Resource.create({"service.name": service_name}), |
| 62 | + ) |
| 63 | + trace.set_tracer_provider(provider) |
| 64 | + |
| 65 | + for exporter_name, exporter_class in exporters.items(): |
| 66 | + exporter_args = {} |
| 67 | + if exporter_name not in [ |
| 68 | + EXPORTER_OTLP, |
| 69 | + EXPORTER_OTLP_SPAN, |
| 70 | + ]: |
| 71 | + exporter_args["service_name"] = service_name |
| 72 | + |
| 73 | + provider.add_span_processor( |
| 74 | + BatchExportSpanProcessor(exporter_class(**exporter_args)) |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +def init_metrics(exporters: Sequence[MetricsExporter]): |
| 79 | + if exporters: |
| 80 | + logger.warning("automatic metric initialization is not supported yet.") |
| 81 | + |
| 82 | + |
| 83 | +def import_exporters( |
| 84 | + exporter_names: Sequence[str], |
| 85 | +) -> Tuple[Sequence[SpanExporter], Sequence[MetricsExporter]]: |
| 86 | + trace_exporters, metric_exporters = {}, {} |
| 87 | + |
| 88 | + exporters = { |
| 89 | + ep.name: ep for ep in iter_entry_points("opentelemetry_exporter") |
| 90 | + } |
| 91 | + |
| 92 | + for exporter_name in exporter_names: |
| 93 | + entry_point = exporters.get(exporter_name, None) |
| 94 | + if not entry_point: |
| 95 | + raise RuntimeError( |
| 96 | + "Requested exporter not found: {0}".format(exporter_name) |
| 97 | + ) |
| 98 | + |
| 99 | + exporter_impl = entry_point.load() |
| 100 | + if issubclass(exporter_impl, SpanExporter): |
| 101 | + trace_exporters[exporter_name] = exporter_impl |
| 102 | + elif issubclass(exporter_impl, MetricsExporter): |
| 103 | + metric_exporters[exporter_name] = exporter_impl |
| 104 | + else: |
| 105 | + raise RuntimeError( |
| 106 | + "{0} is neither a trace exporter nor a metric exporter".format( |
| 107 | + exporter_name |
| 108 | + ) |
| 109 | + ) |
| 110 | + return trace_exporters, metric_exporters |
| 111 | + |
| 112 | + |
| 113 | +def initialize_components(): |
| 114 | + exporter_names = get_exporter_names() |
| 115 | + trace_exporters, metric_exporters = import_exporters(exporter_names) |
| 116 | + init_tracing(trace_exporters) |
| 117 | + |
| 118 | + # We don't support automatic initialization for metric yet but have added |
| 119 | + # some boilerplate in order to make sure current implementation does not |
| 120 | + # lock us out of supporting metrics later without major surgery. |
| 121 | + init_metrics(metric_exporters) |
0 commit comments