Skip to content

Commit

Permalink
Remove service name (#1669)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarar authored Mar 16, 2021
1 parent 75c7371 commit c61a712
Show file tree
Hide file tree
Showing 16 changed files with 119 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
11 changes: 7 additions & 4 deletions docs/getting_started/jaeger_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
# jaeger_example.py
from opentelemetry import trace
from opentelemetry.exporter import jaeger
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 = jaeger.JaegerExporter(
service_name="my-helloworld-service",
agent_host_name="localhost",
agent_port=6831,
agent_host_name="localhost", agent_port=6831,
)

trace.get_tracer_provider().add_span_processor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

# create a JaegerExporter
jaeger_exporter = jaeger.JaegerExporter(
service_name="my-helloworld-service",
# configure agent
agent_host_name="localhost",
agent_port=6831,
Expand All @@ -29,7 +28,6 @@
# `EXPORTER_JAEGER_CERTIFICATE` with file containing creds.

# jaeger_exporter = jaeger.JaegerExporter(
# service_name="my-helloworld-service",
# collector_endpoint="localhost:14250",
# insecure=True,
# transport_format="protobuf",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
# create a JaegerExporter
jaeger_exporter = jaeger.JaegerExporter(
service_name='my-helloworld-service',
# configure agent
agent_host_name='localhost',
agent_port=6831,
Expand Down Expand Up @@ -81,6 +80,7 @@

from grpc import ChannelCredentials, insecure_channel, secure_channel

from opentelemetry import trace
from opentelemetry.exporter.jaeger import util
from opentelemetry.exporter.jaeger.gen import model_pb2
from opentelemetry.exporter.jaeger.gen.collector_pb2 import PostSpansRequest
Expand All @@ -100,6 +100,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 @@ -118,8 +119,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 @@ -137,7 +136,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 @@ -149,7 +147,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 @@ -203,6 +200,12 @@ def __init__(
if transport_format
else TRANSPORT_FORMAT_THRIFT
)
tracer_provider = trace.get_tracer_provider()
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_grpc_client(self) -> Optional[CollectorServiceStub]:
Expand Down Expand Up @@ -243,6 +246,7 @@ def _collector_http_client(self) -> Optional[Collector]:
return self._collector

def export(self, spans) -> SpanExportResult:

translator = Translate(spans)
if self.transport_format == TRANSPORT_FORMAT_PROTOBUF:
pb_translator = ProtobufTranslator(
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,20 +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, transport_format="protobuf"
)

provider = TracerProvider(resource=Resource.create({}))
trace_api.set_tracer_provider(provider)
exporter = JaegerExporter(transport_format="protobuf")
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
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 @@ -31,7 +32,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 @@ -51,14 +53,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 @@ -69,6 +78,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 @@ -81,17 +91,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 @@ -108,6 +123,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 @@ -133,10 +149,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 @@ -152,9 +174,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 @@ -426,10 +456,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 @@ -448,6 +487,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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
translate_to_collector,
)
from opentelemetry.sdk import trace
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SpanExportResult
from opentelemetry.trace import TraceFlags

Expand All @@ -38,21 +40,23 @@ def test_constructor(self):
"opentelemetry.exporter.opencensus.util.get_node",
side_effect=mock_get_node,
)
service_name = "testServiceName"
trace_api.set_tracer_provider(
TracerProvider(
resource=Resource.create({SERVICE_NAME: "testServiceName"})
)
)

host_name = "testHostName"
client = grpc.insecure_channel("")
endpoint = "testEndpoint"
with patch:
exporter = OpenCensusSpanExporter(
service_name=service_name,
host_name=host_name,
endpoint=endpoint,
client=client,
host_name=host_name, endpoint=endpoint, client=client,
)

self.assertIs(exporter.client, client)
self.assertEqual(exporter.endpoint, endpoint)
mock_get_node.assert_called_with(service_name, host_name)
mock_get_node.assert_called_with("testServiceName", host_name)

def test_get_collector_span_kind(self):
result = utils.get_collector_span_kind(trace_api.SpanKind.SERVER)
Expand Down
Loading

0 comments on commit c61a712

Please sign in to comment.