def test_constructors(self): """Test ensures both exporters can co-exist""" try: grpc.JaegerExporter() thrift.JaegerExporter() except Exception as exc: # pylint: disable=broad-except self.assertIsNone(exc)
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(agent_host_name="localhost", agent_port=6318) # just agent is configured now agent_client_mock = mock.Mock(spec=jaeger_exporter.AgentClientUDP) # pylint: disable=protected-access exporter._agent_client = agent_client_mock exporter.export((self._test_span, )) self.assertEqual(agent_client_mock.emit.call_count, 1) # add also a collector and test that both are called collector_mock = mock.Mock(spec=jaeger_exporter.Collector) # pylint: disable=protected-access exporter._collector = collector_mock exporter.export((self._test_span, )) self.assertEqual(agent_client_mock.emit.call_count, 1) self.assertEqual(collector_mock.submit.call_count, 1)
def test_constructor_by_environment_variables(self): # pylint: disable=protected-access """Test the constructor using Environment Variables.""" service = "my-opentelemetry-jaeger" agent_host_name = "opentelemetry.io" agent_port = "6831" collector_endpoint = "https://opentelemetry.io:15875" username = "******" password = "******" auth = (username, password) environ_patcher = mock.patch.dict( "os.environ", { OTEL_EXPORTER_JAEGER_AGENT_HOST: agent_host_name, OTEL_EXPORTER_JAEGER_AGENT_PORT: agent_port, OTEL_EXPORTER_JAEGER_ENDPOINT: collector_endpoint, OTEL_EXPORTER_JAEGER_USER: username, OTEL_EXPORTER_JAEGER_PASSWORD: password, OTEL_EXPORTER_JAEGER_TIMEOUT: "20", }, ) 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)) self.assertEqual(exporter._timeout, 20) self.assertTrue(exporter._collector_http_client is not None) self.assertEqual(exporter.collector_endpoint, collector_endpoint) self.assertEqual(exporter._collector_http_client.auth, auth) # property should not construct new object collector = exporter._collector_http_client self.assertEqual(exporter._collector_http_client, collector) # property should construct new object exporter._collector = None exporter.username = None exporter.password = None self.assertNotEqual(exporter._collector_http_client, collector) self.assertTrue(exporter._collector_http_client.auth is None) environ_patcher.stop()
def test_export_span_service_name(self): trace_api.set_tracer_provider( TracerProvider( resource=Resource.create({SERVICE_NAME: "text_export"}))) exporter = jaeger_exporter.JaegerExporter(agent_host_name="localhost", agent_port=6318) agent_client_mock = mock.Mock(spec=jaeger_exporter.AgentClientUDP) exporter._agent_client = agent_client_mock resource = Resource.create({SERVICE_NAME: "test"}) span = trace._Span("test_span", context=self.context, resource=resource) span.start() span.end() exporter.export([span]) self.assertEqual(exporter.service_name, "test")
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 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) self.assertEqual(exporter.collector_endpoint, None) self.assertEqual(exporter.username, None) self.assertEqual(exporter.password, None) self.assertTrue(exporter._collector_http_client is None) self.assertTrue(exporter._agent_client is not None) self.assertIsNone(exporter._max_tag_value_length)
def test_constructor_explicit(self): # pylint: disable=protected-access """Test the constructor passing all the options.""" service = "my-opentelemetry-jaeger" collector_endpoint = "https://opentelemetry.io:15875" agent_port = 14268 agent_host_name = "opentelemetry.io" username = "******" password = "******" auth = (username, password) trace_api.set_tracer_provider( TracerProvider(resource=Resource.create( {SERVICE_NAME: "my-opentelemetry-jaeger"}))) exporter = jaeger_exporter.JaegerExporter( 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) self.assertTrue(exporter._collector_http_client is not None) self.assertEqual(exporter._collector_http_client.auth, auth) # property should not construct new object collector = exporter._collector_http_client self.assertEqual(exporter._collector_http_client, collector) # property should construct new object exporter._collector = None exporter.username = None exporter.password = None self.assertNotEqual(exporter._collector_http_client, collector) self.assertTrue(exporter._collector_http_client.auth is None) self.assertEqual(exporter._max_tag_value_length, 42)
from opentelemetry.instrumentation.flask import FlaskInstrumentor from opentelemetry.instrumentation.mysql import MySQLInstrumentor ############################### framework setup ################################ # The preferred tracer implementation must be set, as the opentelemetry-api # defines the interface with a no-op implementation. # It must be done before instrumenting any library. trace.set_tracer_provider( TracerProvider( resource=Resource.create({SERVICE_NAME: "my-server"}) ) ) # configure exporter to Jaeger jaeger_exporter = thrift.JaegerExporter( agent_host_name="localhost", agent_port=6831, ) trace.get_tracer_provider().add_span_processor( BatchSpanProcessor(jaeger_exporter) ) # Enable instrumentation for Flask and MySQL libraries FlaskInstrumentor().instrument() MySQLInstrumentor().instrument() tracer = trace.get_tracer(__name__) ############################# application code ################################# app = flask.Flask(__name__)
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")
import time from opentelemetry import trace from opentelemetry.exporter.jaeger import thrift from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer(__name__) # create a JaegerExporter jaeger_exporter = thrift.JaegerExporter( # configure agent agent_host_name="localhost", agent_port=6831, # optional: configure also collector # collector_endpoint="http://localhost:14268/api/traces?format=jaeger.thrift", # username=xxxx, # optional # password=xxxx, # optional ) # create a BatchSpanProcessor and add the exporter to it span_processor = BatchSpanProcessor(jaeger_exporter) # add to the tracer factory trace.get_tracer_provider().add_span_processor(span_processor) # create some spans for testing with tracer.start_as_current_span("foo") as foo: time.sleep(0.1) foo.set_attribute("my_atribbute", True)