-
Notifications
You must be signed in to change notification settings - Fork 829
Add Env variables in OTLP exporter #1101
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
Changes from all commits
416d55d
a24a88d
ced438a
da5a7a2
045ad91
710a60b
d6995de
47a5dcb
c704a55
168771f
8e66bd1
fc4429e
269b8cb
d4c6192
e4c2c09
0b3e5fc
30ac5e7
8ed3903
dcd4f69
acd3024
b9527ef
af89d7d
f0d415d
f9ae58d
4e70e10
eff69d4
82e78de
49b2ed6
bdaebb1
a9f5bae
500f8c7
5a5708c
593f459
6f37169
9483813
2ff2492
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| """OTLP Exporter""" | ||
|
|
||
| import logging | ||
| import os | ||
| from abc import ABC, abstractmethod | ||
| from collections.abc import Mapping, Sequence | ||
| from time import sleep | ||
|
|
@@ -30,8 +31,10 @@ | |
| StatusCode, | ||
| insecure_channel, | ||
| secure_channel, | ||
| ssl_channel_credentials, | ||
| ) | ||
|
|
||
| from opentelemetry.configuration import Configuration | ||
| from opentelemetry.proto.common.v1.common_pb2 import AnyValue, KeyValue | ||
| from opentelemetry.proto.resource.v1.resource_pb2 import Resource | ||
| from opentelemetry.sdk.resources import Resource as SDKResource | ||
|
|
@@ -113,6 +116,16 @@ def _get_resource_data( | |
| return resource_data | ||
|
|
||
|
|
||
| def _load_credential_from_file(filepath) -> ChannelCredentials: | ||
| try: | ||
| with open(filepath, "rb") as f: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tried to use the OTLP Exporter today and I think I ran into an issue. If you don't have the will return
which is not caught by
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest opening a new issue to track these problems instead of continuing an already closed PR thread. |
||
| credential = f.read() | ||
| return ssl_channel_credentials(credential) | ||
| except FileNotFoundError: | ||
| logger.exception("Failed to read credential file") | ||
| return None | ||
|
|
||
|
|
||
| # pylint: disable=no-member | ||
| class OTLPExporterMixin( | ||
| ABC, Generic[SDKDataT, ExportServiceRequestT, ExportResultT] | ||
|
|
@@ -121,24 +134,47 @@ class OTLPExporterMixin( | |
|
|
||
| Args: | ||
| endpoint: OpenTelemetry Collector receiver endpoint | ||
| insecure: Connection type | ||
| credentials: ChannelCredentials object for server authentication | ||
| metadata: Metadata to send when exporting | ||
| timeout: Backend request timeout in seconds | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| endpoint: str = "localhost:55680", | ||
| credentials: ChannelCredentials = None, | ||
| metadata: Optional[Tuple[Any]] = None, | ||
| endpoint: Optional[str] = None, | ||
| insecure: Optional[bool] = None, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could add env vars for
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compression is implemented in a separate PR https://github.com/open-telemetry/opentelemetry-python/pull/1141/files#diff-77db88bbe988c1d7f7d9f92f9e05400259a026e7203f7e9ee12aef76b3b0fdb3R142 |
||
| credentials: Optional[ChannelCredentials] = None, | ||
| headers: Optional[str] = None, | ||
| timeout: Optional[int] = None, | ||
| ): | ||
| super().__init__() | ||
|
|
||
| self._metadata = metadata | ||
| endpoint = ( | ||
| endpoint | ||
| or Configuration().EXPORTER_OTLP_ENDPOINT | ||
| or "localhost:55680" | ||
| ) | ||
|
|
||
| if insecure is None: | ||
lzchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| insecure = Configuration().EXPORTER_OTLP_INSECURE | ||
| if insecure is None: | ||
| insecure = False | ||
|
|
||
| self._headers = headers or Configuration().EXPORTER_OTLP_HEADERS | ||
| self._timeout = ( | ||
| timeout | ||
| or Configuration().EXPORTER_OTLP_TIMEOUT | ||
| or 10 # default: 10 seconds | ||
| ) | ||
| self._collector_span_kwargs = None | ||
|
|
||
| if credentials is None: | ||
| if insecure: | ||
| self._client = self._stub(insecure_channel(endpoint)) | ||
| else: | ||
| credentials = credentials or _load_credential_from_file( | ||
| Configuration().EXPORTER_OTLP_CERTIFICATE | ||
| ) | ||
| self._client = self._stub(secure_channel(endpoint, credentials)) | ||
|
|
||
| @abstractmethod | ||
|
|
@@ -164,7 +200,8 @@ def _export(self, data: TypingSequence[SDKDataT]) -> ExportResultT: | |
| try: | ||
| self._client.Export( | ||
| request=self._translate_data(data), | ||
| metadata=self._metadata, | ||
| metadata=self._headers, | ||
| timeout=self._timeout, | ||
| ) | ||
|
|
||
| return self._result.SUCCESS | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,11 +14,16 @@ | |
| """OTLP Span Exporter""" | ||
|
|
||
| import logging | ||
| from typing import Sequence | ||
| import os | ||
| from typing import Optional, Sequence | ||
|
|
||
| from grpc import ChannelCredentials | ||
|
|
||
| from opentelemetry.configuration import Configuration | ||
| from opentelemetry.exporter.otlp.exporter import ( | ||
| OTLPExporterMixin, | ||
| _get_resource_data, | ||
| _load_credential_from_file, | ||
| _translate_key_values, | ||
| ) | ||
| from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import ( | ||
|
|
@@ -50,13 +55,47 @@ class OTLPSpanExporter( | |
|
|
||
| Args: | ||
| endpoint: OpenTelemetry Collector receiver endpoint | ||
| insecure: Connection type | ||
| credentials: Credentials object for server authentication | ||
| metadata: Metadata to send when exporting | ||
| timeout: Backend request timeout in seconds | ||
| """ | ||
|
|
||
| _result = SpanExportResult | ||
| _stub = TraceServiceStub | ||
|
|
||
| def __init__( | ||
| self, | ||
| endpoint: Optional[str] = None, | ||
| insecure: Optional[bool] = None, | ||
| credentials: Optional[ChannelCredentials] = None, | ||
| headers: Optional[str] = None, | ||
| timeout: Optional[int] = None, | ||
| ): | ||
| if insecure is None: | ||
| insecure = Configuration().EXPORTER_OTLP_SPAN_INSECURE | ||
|
|
||
| if ( | ||
| not insecure | ||
| and Configuration().EXPORTER_OTLP_SPAN_CERTIFICATE is not None | ||
| ): | ||
| credentials = credentials or _load_credential_from_file( | ||
| Configuration().EXPORTER_OTLP_SPAN_CERTIFICATE | ||
| ) | ||
|
|
||
| super().__init__( | ||
| **{ | ||
| "endpoint": endpoint | ||
| or Configuration().EXPORTER_OTLP_SPAN_ENDPOINT, | ||
| "insecure": insecure, | ||
| "credentials": credentials, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I set which makes sense because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @NathanielRN Thanks for reporting this. I also ran into this issue. Causes a failure in the example code. |
||
| "headers": headers | ||
| or Configuration().EXPORTER_OTLP_SPAN_HEADERS, | ||
| "timeout": timeout | ||
| or Configuration().EXPORTER_OTLP_SPAN_TIMEOUT, | ||
| } | ||
| ) | ||
|
|
||
| def _translate_name(self, sdk_span: SDKSpan) -> None: | ||
| self._collector_span_kwargs["name"] = sdk_span.name | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.