-
Notifications
You must be signed in to change notification settings - Fork 651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatic exporter/provider setup for opentelemetry-instrument command. #1036
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
121 changes: 121 additions & 0 deletions
121
...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,121 @@ | ||
# 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 pkg_resources import iter_entry_points | ||
|
||
from opentelemetry import trace | ||
from opentelemetry.configuration import Configuration | ||
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, | ||
) | ||
|
||
logger = getLogger(__file__) | ||
|
||
EXPORTER_OTLP = "otlp" | ||
EXPORTER_OTLP_SPAN = "otlp_span" | ||
EXPORTER_OTLP_METRIC = "otlp_metric" | ||
_DEFAULT_EXPORTER = EXPORTER_OTLP | ||
|
||
|
||
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 [] | ||
|
||
names = [] | ||
for exp in exporter.split(","): | ||
name = exp.strip() | ||
if name == EXPORTER_OTLP: | ||
names.append(EXPORTER_OTLP_SPAN) | ||
names.append(EXPORTER_OTLP_METRIC) | ||
else: | ||
names.append(name) | ||
return names | ||
|
||
|
||
def init_tracing(exporters: Sequence[SpanExporter]): | ||
service_name = get_service_name() | ||
provider = TracerProvider( | ||
resource=Resource.create({"service.name": service_name}), | ||
) | ||
trace.set_tracer_provider(provider) | ||
|
||
for exporter_name, exporter_class in exporters.items(): | ||
exporter_args = {} | ||
if exporter_name not in [ | ||
EXPORTER_OTLP, | ||
EXPORTER_OTLP_SPAN, | ||
]: | ||
exporter_args["service_name"] = service_name | ||
|
||
provider.add_span_processor( | ||
BatchExportSpanProcessor(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 = {}, {} | ||
|
||
exporters = { | ||
ep.name: ep for ep in iter_entry_points("opentelemetry_exporter") | ||
} | ||
|
||
for exporter_name in exporter_names: | ||
entry_point = exporters.get(exporter_name, None) | ||
if not entry_point: | ||
raise RuntimeError( | ||
"Requested exporter not found: {0}".format(exporter_name) | ||
) | ||
|
||
exporter_impl = entry_point.load() | ||
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} is neither a trace exporter nor a metric exporter".format( | ||
exporter_name | ||
) | ||
) | ||
return trace_exporters, metric_exporters | ||
|
||
|
||
def initialize_components(): | ||
ocelotl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you explain the reason behind removing the info about opentelemetry-bootstrap?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, this info is probably important to keep here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unintentional. Probably caused by a bad rebase. Fixing.