Skip to content

Commit

Permalink
Use configuration library
Browse files Browse the repository at this point in the history
  • Loading branch information
jan25 committed Sep 20, 2020
1 parent f37e054 commit 0ac9bbc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
secure_channel,
)

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
Expand Down Expand Up @@ -116,9 +117,13 @@ def _get_resource_data(

def _load_credential_from_file(filepath) -> ChannelCredentials:
real_path = os.path.join(os.path.dirname(__file__), filepath)
with open(real_path, 'rb') as f:
credential = f.read()
return ssl_channel_credentials(credential)
try:
with open(real_path, 'rb') as f:
credential = f.read()
return ssl_channel_credentials(credential)
except FileNotFoundError as error:
logger.exception(error)
return None

# pylint: disable=no-member
class OTLPExporterMixin(
Expand All @@ -141,15 +146,15 @@ def __init__(
):
super().__init__()

endpoint = endpoint or os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT") or "localhost:55680"
insecure = insecure or os.environ.get("OTEL_EXPORTER_OTLP_INSECURE") or True # TODO(jan25): verify spec for default value. This affects how credentials are passed
endpoint = endpoint or Configuration().EXPORTER_OTLP_ENDPOINT or "localhost:55680"
insecure = insecure or Configuration().EXPORTER_OTLP_INSECURE or True
self._metadata = metadata
self._collector_span_kwargs = None

if insecure:
self._client = self._stub(insecure_channel(endpoint))
else:
credentials = credentials or _load_credential_from_file(os.environ.get("OTEL_EXPORTER_OTLP_CERTIFICATE"))
credentials = credentials or _load_credential_from_file(Configuration().EXPORTER_OTLP_CERTIFICATE)
self._client = self._stub(secure_channel(endpoint, credentials))

@abstractmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
import logging
from typing import List, Sequence, Type, TypeVar, Union

from grpc import (
ChannelCredentials
)

from opentelemetry.configuration import Configuration
# pylint: disable=duplicate-code
from opentelemetry.exporter.otlp.exporter import (
OTLPExporterMixin,
Expand Down Expand Up @@ -159,16 +164,16 @@ def __init__(
credentials: ChannelCredentials = None,
metadata: tuple = None,
):
insecure = insecure or os.environ.get("OTEL_EXPORTER_OTLP_METRIC_INSECURE")
insecure = insecure or Configuration().EXPORTER_OTLP_METRIC_INSECURE
if not insecure:
credentials = credentials or _load_credential_from_file(os.environ.get("OTEL_EXPORTER_OTLP_METRIC_CERTIFICATE"))
credentials = credentials or _load_credential_from_file(Configuration().EXPORTER_OTLP_METRIC_CERTIFICATE)

super().__init__(
**{
endpoint: endpoint or os.environ.get("OTEL_EXPORTER_OTLP_METRIC_ENDPOINT"),
endpoint: endpoint or Configuration().EXPORTER_OTLP_METRIC_ENDPOINT,
insecure: insecure,
credentials: credentials,
metadata: metadata or os.environ.get("OTEL_EXPORTER_OTLP_METRIC_HEADERS")
metadata: metadata or Configuration().EXPORTER_OTLP_METRIC_HEADERS
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
import logging
from typing import Sequence

from grpc import (
ChannelCredentials
)

from opentelemetry.configuration import Configuration
from opentelemetry.exporter.otlp.exporter import (
OTLPExporterMixin,
_get_resource_data,
Expand Down Expand Up @@ -66,16 +71,16 @@ def __init__(
credentials: ChannelCredentials = None,
metadata: tuple = None,
):
insecure = insecure or os.environ.get("OTEL_EXPORTER_OTLP_SPAN_INSECURE")
insecure = insecure or Configuration().EXPORTER_OTLP_SPAN_INSECURE
if not insecure:
credentials = credentials or _load_credential_from_file(os.environ.get("OTEL_EXPORTER_OTLP_SPAN_CERTIFICATE"))
credentials = credentials or _load_credential_from_file(Configuration().EXPORTER_OTLP_SPAN_CERTIFICATE)

super().__init__(
**{
endpoint: endpoint or os.environ.get("OTEL_EXPORTER_OTLP_SPAN_ENDPOINT"),
endpoint: endpoint or Configuration().EXPORTER_OTLP_SPAN_ENDPOINT,
insecure: insecure,
credentials: credentials,
metadata: metadata or os.environ.get("OTEL_EXPORTER_OTLP_SPAN_HEADERS")
metadata: metadata or Configuration().EXPORTER_OTLP_SPAN_HEADERS
}
)

Expand Down

0 comments on commit 0ac9bbc

Please sign in to comment.