Skip to content

Commit

Permalink
Remove service name (open-telemetry#1669)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarar authored and Alex Boten committed Mar 16, 2021
1 parent 9664f9f commit ee017a5
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 99 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1500](https://github.com/open-telemetry/opentelemetry-python/pull/1500))

### Changed
- remove `service_name` from constructor of jaeger and opencensus exporters and
use of env variable `OTEL_PYTHON_SERVICE_NAME`
([#1669])(https://github.com/open-telemetry/opentelemetry-python/pull/1669)
- Rename `IdsGenerator` to `IdGenerator`
([#1651](https://github.com/open-telemetry/opentelemetry-python/pull/1651))
- Make TracerProvider's resource attribute private
Expand Down
4 changes: 1 addition & 3 deletions docs/examples/opencensus-exporter-tracer/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

exporter = OpenCensusSpanExporter(
service_name="basic-service", endpoint="localhost:55678"
)
exporter = OpenCensusSpanExporter(endpoint="localhost:55678")

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
Expand Down
8 changes: 6 additions & 2 deletions docs/getting_started/jaeger_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
# jaeger_example.py
from opentelemetry import trace
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

trace.set_tracer_provider(TracerProvider())
trace.set_tracer_provider(
TracerProvider(
resource=Resource.create({SERVICE_NAME: "my-helloworld-service"})
)
)

jaeger_exporter = JaegerExporter(
service_name="my-helloworld-service",
agent_host_name="localhost",
agent_port=6831,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# `EXPORTER_JAEGER_CERTIFICATE` with file containing creds.

jaeger_exporter = proto.JaegerExporter(
service_name="my-helloworld-service",
collector_endpoint="localhost:14250",
insecure=True,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
from opentelemetry.sdk.environment_variables import (
OTEL_EXPORTER_JAEGER_CERTIFICATE,
OTEL_EXPORTER_JAEGER_ENDPOINT,
OTEL_RESOURCE_ATTRIBUTES,
)
from opentelemetry.sdk.trace import Resource
from opentelemetry.sdk.trace import Resource, TracerProvider
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
from opentelemetry.trace.status import Status, StatusCode

Expand All @@ -51,6 +52,7 @@ def setUp(self):
self._test_span = trace._Span("test_span", context=context)
self._test_span.start()
self._test_span.end()

# pylint: disable=protected-access

def test_constructor_by_environment_variables(self):
Expand All @@ -66,18 +68,18 @@ def test_constructor_by_environment_variables(self):
OTEL_EXPORTER_JAEGER_ENDPOINT: collector_endpoint,
OTEL_EXPORTER_JAEGER_CERTIFICATE: os.path.dirname(__file__)
+ "/certs/cred.cert",
OTEL_RESOURCE_ATTRIBUTES: "service.name=my-opentelemetry-jaeger",
},
)

env_patch.start()

exporter = JaegerExporter(service_name=service,)

provider = TracerProvider(resource=Resource.create({}))
trace_api.set_tracer_provider(provider)
exporter = JaegerExporter()
self.assertEqual(exporter.service_name, service)
self.assertIsNotNone(exporter._collector_grpc_client)
self.assertEqual(exporter.collector_endpoint, collector_endpoint)
self.assertIsNotNone(exporter.credentials)

env_patch.stop()

# pylint: disable=too-many-locals,too-many-statements
Expand Down Expand Up @@ -122,8 +124,8 @@ def test_translate_to_jaeger(self):

event_timestamp = base_time + 50 * 10 ** 6
# pylint:disable=protected-access
event_timestamp_proto = pb_translator._proto_timestamp_from_epoch_nanos(
event_timestamp
event_timestamp_proto = (
pb_translator._proto_timestamp_from_epoch_nanos(event_timestamp)
)

event = trace.Event(
Expand Down Expand Up @@ -335,7 +337,9 @@ def test_translate_to_jaeger(self):
duration=span2_duration,
flags=0,
tags=default_tags,
process=model_pb2.Process(service_name="svc",),
process=model_pb2.Process(
service_name="svc",
),
),
model_pb2.Span(
operation_name=span_names[2],
Expand Down Expand Up @@ -366,15 +370,18 @@ def test_translate_to_jaeger(self):
v_str="version",
),
],
process=model_pb2.Process(service_name="svc",),
process=model_pb2.Process(
service_name="svc",
),
),
]

# events are complicated to compare because order of fields
# (attributes) in otel is not important but in jeager it is
# pylint: disable=no-member
self.assertCountEqual(
spans[0].logs[0].fields, expected_spans[0].logs[0].fields,
spans[0].logs[0].fields,
expected_spans[0].logs[0].fields,
)

self.assertEqual(spans, expected_spans)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
# create a JaegerExporter
jaeger_exporter = JaegerExporter(
service_name='my-helloworld-service',
# configure agent
agent_host_name='localhost',
agent_port=6831,
Expand Down Expand Up @@ -80,6 +79,7 @@
from os import environ
from typing import Optional

from opentelemetry import trace
from opentelemetry.exporter.jaeger.thrift.gen.jaeger import (
Collector as jaeger_thrift,
)
Expand All @@ -96,6 +96,7 @@
OTEL_EXPORTER_JAEGER_PASSWORD,
OTEL_EXPORTER_JAEGER_USER,
)
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult

DEFAULT_AGENT_HOST_NAME = "localhost"
Expand All @@ -108,8 +109,6 @@ class JaegerExporter(SpanExporter):
"""Jaeger span exporter for OpenTelemetry.
Args:
service_name: Service that logged an annotation in a trace.Classifier
when query for spans.
agent_host_name: The host name of the Jaeger-Agent.
agent_port: The port of the Jaeger-Agent.
collector_endpoint: The endpoint of the Jaeger collector that uses
Expand All @@ -124,7 +123,6 @@ class JaegerExporter(SpanExporter):

def __init__(
self,
service_name: str,
agent_host_name: Optional[str] = None,
agent_port: Optional[int] = None,
collector_endpoint: Optional[str] = None,
Expand All @@ -133,7 +131,6 @@ def __init__(
max_tag_value_length: Optional[int] = None,
udp_split_oversized_batches: bool = None,
):
self.service_name = service_name
self._max_tag_value_length = max_tag_value_length
self.agent_host_name = _parameter_setter(
param=agent_host_name,
Expand Down Expand Up @@ -179,6 +176,11 @@ def __init__(
default=None,
)
self._collector = None
self.service_name = (
tracer_provider.resource.attributes[SERVICE_NAME]
if getattr(tracer_provider, "resource", None)
else Resource.create().attributes.get(SERVICE_NAME)
)

@property
def _collector_http_client(self) -> Optional[Collector]:
Expand All @@ -198,6 +200,7 @@ def _collector_http_client(self) -> Optional[Collector]:
return self._collector

def export(self, spans) -> SpanExportResult:

translator = Translate(spans)
thrift_translator = ThriftTranslator(self._max_tag_value_length)
jaeger_spans = translator._translate(thrift_translator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import unittest
from unittest import mock
from unittest.mock import patch

# pylint:disable=no-name-in-module
# pylint:disable=import-error
Expand All @@ -33,7 +34,8 @@
OTEL_EXPORTER_JAEGER_PASSWORD,
OTEL_EXPORTER_JAEGER_USER,
)
from opentelemetry.sdk.trace import Resource
from opentelemetry.sdk.resources import SERVICE_NAME
from opentelemetry.sdk.trace import Resource, TracerProvider
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
from opentelemetry.trace import SpanKind
from opentelemetry.trace.status import Status, StatusCode
Expand All @@ -53,14 +55,21 @@ def setUp(self):
self._test_span.end()
# pylint: disable=protected-access

@patch("opentelemetry.exporter.jaeger.trace._TRACER_PROVIDER", None)
def test_constructor_default(self):
# pylint: disable=protected-access
"""Test the default values assigned by constructor."""
service_name = "my-service-name"
agent_host_name = "localhost"
agent_port = 6831
exporter = jaeger_exporter.JaegerExporter(service_name)

trace_api.set_tracer_provider(
TracerProvider(
resource=Resource.create({SERVICE_NAME: "my-service-name"})
)
)

exporter = jaeger_exporter.JaegerExporter()
self.assertEqual(exporter.service_name, service_name)
self.assertEqual(exporter.agent_host_name, agent_host_name)
self.assertEqual(exporter.agent_port, agent_port)
Expand All @@ -71,6 +80,7 @@ def test_constructor_default(self):
self.assertTrue(exporter._agent_client is not None)
self.assertIsNone(exporter._max_tag_value_length)

@patch("opentelemetry.exporter.jaeger.trace._TRACER_PROVIDER", None)
def test_constructor_explicit(self):
# pylint: disable=protected-access
"""Test the constructor passing all the options."""
Expand All @@ -83,17 +93,22 @@ def test_constructor_explicit(self):
username = "username"
password = "password"
auth = (username, password)
trace_api.set_tracer_provider(
TracerProvider(
resource=Resource.create(
{SERVICE_NAME: "my-opentelemetry-jaeger"}
)
)
)

exporter = jaeger_exporter.JaegerExporter(
service_name=service,
agent_host_name=agent_host_name,
agent_port=agent_port,
collector_endpoint=collector_endpoint,
username=username,
password=password,
max_tag_value_length=42,
)

self.assertEqual(exporter.service_name, service)
self.assertEqual(exporter.agent_host_name, agent_host_name)
self.assertEqual(exporter.agent_port, agent_port)
Expand All @@ -110,6 +125,7 @@ def test_constructor_explicit(self):
self.assertTrue(exporter._collector_http_client.auth is None)
self.assertEqual(exporter._max_tag_value_length, 42)

@patch("opentelemetry.exporter.jaeger.trace._TRACER_PROVIDER", None)
def test_constructor_by_environment_variables(self):
# pylint: disable=protected-access
"""Test the constructor using Environment Variables."""
Expand All @@ -135,10 +151,16 @@ def test_constructor_by_environment_variables(self):
},
)

environ_patcher.start()

exporter = jaeger_exporter.JaegerExporter(service_name=service)
trace_api.set_tracer_provider(
TracerProvider(
resource=Resource.create(
{SERVICE_NAME: "my-opentelemetry-jaeger"}
)
)
)

environ_patcher.start()
exporter = jaeger_exporter.JaegerExporter()
self.assertEqual(exporter.service_name, service)
self.assertEqual(exporter.agent_host_name, agent_host_name)
self.assertEqual(exporter.agent_port, int(agent_port))
Expand All @@ -154,9 +176,17 @@ def test_constructor_by_environment_variables(self):
exporter.password = None
self.assertNotEqual(exporter._collector_http_client, collector)
self.assertTrue(exporter._collector_http_client.auth is None)

environ_patcher.stop()

@patch("opentelemetry.exporter.jaeger.trace._TRACER_PROVIDER", None)
def test_constructor_with_no_traceprovider_resource(self):

"""Test the constructor when there is no resource attached to trace_provider"""

exporter = jaeger_exporter.JaegerExporter()

self.assertEqual(exporter.service_name, "unknown_service")

def test_nsec_to_usec_round(self):
# pylint: disable=protected-access
nsec_to_usec_round = jaeger_exporter.translate._nsec_to_usec_round
Expand Down Expand Up @@ -428,10 +458,19 @@ def test_translate_to_jaeger(self):

self.assertEqual(spans, expected_spans)

@patch("opentelemetry.exporter.jaeger.trace._TRACER_PROVIDER", None)
def test_export(self):

"""Test that agent and/or collector are invoked"""

trace_api.set_tracer_provider(
TracerProvider(
resource=Resource.create({SERVICE_NAME: "text_export"})
)
)

exporter = jaeger_exporter.JaegerExporter(
"test_export", agent_host_name="localhost", agent_port=6318
agent_host_name="localhost", agent_port=6318
)

# just agent is configured now
Expand All @@ -450,6 +489,7 @@ def test_export(self):
exporter.export((self._test_span,))
self.assertEqual(agent_client_mock.emit.call_count, 1)
self.assertEqual(collector_mock.submit.call_count, 1)
# trace_api._TRACER_PROVIDER = None

def test_agent_client(self):
agent_client = jaeger_exporter.AgentClientUDP(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from opencensus.proto.trace.v1 import trace_pb2

import opentelemetry.exporter.opencensus.util as utils
from opentelemetry import trace
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import ReadableSpan
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult

Expand All @@ -39,18 +41,19 @@ class OpenCensusSpanExporter(SpanExporter):
Args:
endpoint: OpenCensus Collector receiver endpoint.
service_name: Name of Collector service.
host_name: Host name.
client: TraceService client stub.
"""

def __init__(
self,
endpoint=DEFAULT_ENDPOINT,
service_name=None,
host_name=None,
client=None,
self, endpoint=DEFAULT_ENDPOINT, host_name=None, client=None,
):
tracer_provider = trace.get_tracer_provider()
service_name = (
tracer_provider.resource.attributes[SERVICE_NAME]
if getattr(tracer_provider, "resource", None)
else Resource.create().attributes.get(SERVICE_NAME)
)
self.endpoint = endpoint
if client is None:
self.channel = grpc.insecure_channel(self.endpoint)
Expand Down
Loading

0 comments on commit ee017a5

Please sign in to comment.