def test_otlp_headers_from_env(self, mock_ssl_channel, mock_secure): exporter = OTLPSpanExporter() # pylint: disable=protected-access self.assertEqual(exporter._headers, (("key1", "value1"), ("key2", "value2"))) exporter = OTLPSpanExporter(headers=(("key3", "value3"), ("key4", "value4"))) # pylint: disable=protected-access self.assertEqual(exporter._headers, (("key3", "value3"), ("key4", "value4")))
def test_constructors(self): try: OTLPSpanExporter() except Exception: # pylint: disable=broad-except self.fail( "Unexpected exception raised when instantiating OTLPSpanExporter" )
def init_tracing(): """ Add tracing to a Flask app. This is a bit different to the OpenTelemetry Python examples because Vercel does some forking and it needs to be handled without knowing which app runner is being used. To be used with Flask's before_first_request. """ if not tracing_inited_holder.inited: otlp_exporter = OTLPSpanExporter( endpoint="https://api.honeycomb.io:443", insecure=False, credentials=ssl_channel_credentials(), headers=( ("x-honeycomb-team", current_app.config["HONEYCOMB_WRITE_KEY"]), ("x-honeycomb-dataset", "Walter Ego"), ), ) trace.set_tracer_provider(TracerProvider()) tracing_inited_holder.processor = BatchSpanProcessor(otlp_exporter) trace.get_tracer_provider().add_span_processor( tracing_inited_holder.processor) tracing_inited_holder.inited = True
def get_tracer_with_processor(span_processor_class): span_processor = span_processor_class(OTLPSpanExporter()) tracer = TracerProvider( active_span_processor=span_processor, sampler=sampling.DEFAULT_ON, ).get_tracer("pipeline_benchmark_tracer") return tracer
def setUp(self): tracer_provider = TracerProvider() self.exporter = OTLPSpanExporter(insecure=True) tracer_provider.add_span_processor(SimpleSpanProcessor(self.exporter)) self.tracer = tracer_provider.get_tracer(__name__) self.server = server(ThreadPoolExecutor(max_workers=10)) self.server.add_insecure_port("[::]:4317") self.server.start() event_mock = Mock( **{ "timestamp": 1591240820506462784, "attributes": OrderedDict([("a", 1), ("b", False)]), }) type(event_mock).name = PropertyMock(return_value="a") self.span = _Span( "a", context=Mock( **{ "trace_state": OrderedDict([("a", "b"), ("c", "d")]), "span_id": 10217189687419569865, "trace_id": 67545097771067222548457157018666467027, }), resource=SDKResource(OrderedDict([("a", 1), ("b", False)])), parent=Mock(**{"span_id": 12345}), attributes=OrderedDict([("a", 1), ("b", True)]), events=[event_mock], links=[ Mock( **{ "context.trace_id": 1, "context.span_id": 2, "attributes": OrderedDict([("a", 1), ("b", False)]), "kind": OTLPSpan.SpanKind.SPAN_KIND_INTERNAL, # pylint: disable=no-member }) ], instrumentation_info=InstrumentationInfo(name="name", version="version"), ) self.span.start() self.span.end()
def test_otlp_exporter_otlp_compression_unspecified( self, mock_insecure_channel ): """No env or kwarg should be NoCompression""" OTLPSpanExporter(insecure=True) mock_insecure_channel.assert_called_once_with( "localhost:4317", compression=Compression.NoCompression )
def test_otlp_exporter_otlp_compression_precendence( self, mock_insecure_channel): """OTEL_EXPORTER_OTLP_TRACES_COMPRESSION as higher priority than OTEL_EXPORTER_OTLP_COMPRESSION """ OTLPSpanExporter(insecure=True) mock_insecure_channel.assert_called_once_with( "localhost:4317", compression=Compression.Gzip)
def test_otlp_exporter_otlp_compression_envvar( self, mock_insecure_channel ): """Just OTEL_EXPORTER_OTLP_COMPRESSION should work""" OTLPSpanExporter(insecure=True) mock_insecure_channel.assert_called_once_with( "localhost:4317", compression=Compression.Gzip )
def setup_tracing() -> None: # Sets the tracer_provider. This is only allowed once per execution # context and will log a warning if attempted multiple times. trace.set_tracer_provider(TracerProvider()) trace.get_tracer_provider().add_span_processor( SimpleSpanProcessor( OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True))) trace.get_tracer_provider().add_span_processor( SimpleSpanProcessor(ConsoleSpanExporter()))
def init_tracing(): resource = Resource.create(attributes={"service.name": "api-service"}) trace.set_tracer_provider(TracerProvider(resource=resource)) # This uses insecure connection for the purpose of example. Please see the # OTLP Exporter documentation for other options. span_processor = BatchSpanProcessor( OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True)) trace.get_tracer_provider().add_span_processor(span_processor)
def test_otlp_insecure_from_env(self, mock_insecure): OTLPSpanExporter() # pylint: disable=protected-access self.assertTrue(mock_insecure.called) self.assertEqual( 1, mock_insecure.call_count, f"expected {mock_insecure} to be called", )
def test_exporters_from_kwargs_classes(self): options = _Options(span_exporter_factories=[ lambda opts: OTLPSpanExporter(), lambda opts: ConsoleSpanExporter(), ]) self.assertEqual(len(options.span_exporter_factories), 2) self.assertIsInstance(options.span_exporter_factories[0](options), OTLPSpanExporter) self.assertIsInstance(options.span_exporter_factories[1](options), ConsoleSpanExporter)
def setUp(self): super().setUp() trace.set_tracer_provider(TracerProvider()) self.tracer = trace.get_tracer(__name__) self.span_processor = ExportStatusSpanProcessor( OTLPSpanExporter(insecure=True, timeout=1) ) trace.get_tracer_provider().add_span_processor(self.span_processor)
def post_fork(server, worker): server.log.info("Worker spawned (pid: %s)", worker.pid) resource = Resource.create(attributes={"service.name": "api-service"}) trace.set_tracer_provider(TracerProvider(resource=resource)) # This uses insecure connection for the purpose of example. Please see the # OTLP Exporter documentation for other options. span_processor = BatchSpanProcessor( OTLPSpanExporter(endpoint="localhost:4317", insecure=True)) trace.get_tracer_provider().add_span_processor(span_processor)
def post_fork(server, worker): server.log.info("Worker spawned (pid: %s)", worker.pid) resource = Resource.create(attributes={"service.name": "job-server"}) trace.set_tracer_provider(TracerProvider(resource=resource)) span_processor = BatchSpanProcessor( OTLPSpanExporter(endpoint="https://api.honeycomb.io")) trace.get_tracer_provider().add_span_processor(span_processor) from opentelemetry.instrumentation.auto_instrumentation import ( # noqa: F401 sitecustomize, )
def test_env_variables(self, mock_exporter_mixin): OTLPSpanExporter() self.assertTrue(len(mock_exporter_mixin.call_args_list) == 1) _, kwargs = mock_exporter_mixin.call_args_list[0] self.assertEqual(kwargs["endpoint"], "collector:4317") self.assertEqual(kwargs["headers"], " key1=value1,KEY2 = value=2") self.assertEqual(kwargs["timeout"], 10) self.assertEqual(kwargs["compression"], Compression.Gzip) self.assertIsNotNone(kwargs["credentials"]) self.assertIsInstance(kwargs["credentials"], ChannelCredentials)
def init_otel(): global tracer, session_name, service_name, insecure LOGGER.debug('Init Otel : {}'.format(service_name)) trace.set_tracer_provider( TracerProvider(resource=Resource.create({SERVICE_NAME: service_name}), )) otel_exporter = OTLPSpanExporter(insecure=insecure) trace.get_tracer_provider().add_span_processor( BatchSpanProcessor(otel_exporter)) tracer = trace.get_tracer(session_name)
def collect(conf, conn, date_from=None, date_to=None, query=None): trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer_provider().get_tracer(__name__) trace.get_tracer_provider().add_span_processor( BatchSpanProcessor(OTLPSpanExporter())) process = CrawlerProcess(conf['SCRAPY_SETTINGS']) process.crawl(Spider, conf=conf, conn=conn, date_from=date_from, date_to=date_to, query=query) process.start()
def test_otlp_exporter_endpoint(self, mock_secure, mock_insecure): """Just OTEL_EXPORTER_OTLP_COMPRESSION should work""" expected_endpoint = "localhost:4317" endpoints = [ ( "http://localhost:4317", None, mock_insecure, ), ( "localhost:4317", None, mock_insecure, ), ( "localhost:4317", False, mock_secure, ), ( "https://localhost:4317", None, mock_secure, ), ( "https://localhost:4317", True, mock_insecure, ), ] for endpoint, insecure, mock_method in endpoints: OTLPSpanExporter(endpoint=endpoint, insecure=insecure) self.assertEqual( 1, mock_method.call_count, "expected {} to be called for {} {}".format( mock_method, endpoint, insecure), ) self.assertEqual( expected_endpoint, mock_method.call_args[0][0], "expected {} got {} {}".format(expected_endpoint, mock_method.call_args[0][0], endpoint), ) mock_method.reset_mock()
def init_otel(): """Init the OpenTelemetry settings""" global tracer, session_name, service_name, insecure, otel_exporter LOGGER.debug("Init Otel : {}".format(service_name)) trace.set_tracer_provider( TracerProvider(resource=Resource.create({SERVICE_NAME: service_name}), )) if in_memory_span_exporter: otel_exporter = InMemorySpanExporter() trace.get_tracer_provider().add_span_processor( SimpleSpanProcessor(otel_exporter)) otel_exporter.clear() else: otel_exporter = OTLPSpanExporter() trace.get_tracer_provider().add_span_processor( BatchSpanProcessor(otel_exporter)) tracer = trace.get_tracer(session_name)
def init_otel(): """Init the OpenTelemetry settings""" global tracer, session_name, service_name, insecure, otel_exporter, errors_counter, failed_counter, skipped_counter, total_counter, controller # noqa: E501 LOGGER.debug("Init Otel : {}".format(service_name)) trace.set_tracer_provider( TracerProvider(resource=Resource.create({SERVICE_NAME: service_name}), )) if in_memory_span_exporter: otel_exporter = InMemorySpanExporter() trace.get_tracer_provider().add_span_processor( SimpleSpanProcessor(otel_exporter)) # metrics_exporter = ConsoleMetricsExporter() else: otel_exporter = OTLPSpanExporter() trace.get_tracer_provider().add_span_processor( BatchSpanProcessor(otel_exporter)) # metrics_exporter = CollectorMetricsExporter() tracer = trace.get_tracer(session_name)
def generate_distributed_traces(self, otel_service_name, ansible_playbook, tasks_data, status, traceparent): """ generate distributed traces from the collected TaskData and HostData """ tasks = [] parent_start_time = None for task_uuid, task in tasks_data.items(): if parent_start_time is None: parent_start_time = task.start tasks.append(task) trace.set_tracer_provider( TracerProvider( resource=Resource.create({SERVICE_NAME: otel_service_name}))) processor = BatchSpanProcessor(OTLPSpanExporter()) trace.get_tracer_provider().add_span_processor(processor) tracer = trace.get_tracer(__name__) with tracer.start_as_current_span( ansible_playbook, context=self.traceparent_context(traceparent), start_time=parent_start_time, kind=SpanKind.SERVER) as parent: parent.set_status(status) # Populate trace metadata attributes if self.ansible_version is not None: parent.set_attribute("ansible.version", self.ansible_version) parent.set_attribute("ansible.session", self.session) parent.set_attribute("ansible.host.name", self.host) if self.ip_address is not None: parent.set_attribute("ansible.host.ip", self.ip_address) parent.set_attribute("ansible.host.user", self.user) for task in tasks: for host_uuid, host_data in task.host_data.items(): with tracer.start_as_current_span( task.name, start_time=task.start, end_on_exit=False) as span: self.update_span_data(task, host_data, span)
from opentelemetry.sdk.trace import TracerProvider, sampling from opentelemetry.sdk.trace.export import BatchSpanProcessor TEST_DURATION_SECONDS = 15 SPANS_PER_SECOND = 10_000 class MockTraceServiceStub(object): def __init__(self, channel): self.Export = lambda *args, **kwargs: None old_stub = OTLPSpanExporter._stub OTLPSpanExporter._stub = MockTraceServiceStub simple_span_processor = BatchSpanProcessor(OTLPSpanExporter()) tracer = TracerProvider( active_span_processor=simple_span_processor, sampler=sampling.DEFAULT_ON, ).get_tracer("resource_usage_tracer") starttime = time.time() for _ in range(TEST_DURATION_SECONDS): for _ in range(SPANS_PER_SECOND): span = tracer.start_span("benchmarkedSpan") span.end() time_to_finish_spans = time.time() - starttime time.sleep(1.0 - time_to_finish_spans if time_to_finish_spans < 1.0 else 0) OTLPSpanExporter._stub = old_stub
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # otcollector.py import time from opentelemetry import trace from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( OTLPSpanExporter, ) from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor span_exporter = OTLPSpanExporter( # optional # endpoint:="myCollectorURL:55678", # credentials=ChannelCredentials(credentials), # headers=(("metadata", "metadata")), ) tracer_provider = TracerProvider() trace.set_tracer_provider(tracer_provider) span_processor = BatchSpanProcessor(span_exporter) tracer_provider.add_span_processor(span_processor) # Configure the tracer to use the collector exporter tracer = trace.get_tracer_provider().get_tracer(__name__) with tracer.start_as_current_span("foo"): print("Hello world!")
# Licensed under the MIT License. """ An example to show an application using Opentelemetry tracing api and sdk with the OpenTelemetry Collector and the Azure monitor exporter. Telemetry is exported to application insights with the AzureMonitorTraceExporter and Zipkin with the OTLP Span exporter. """ import os from opentelemetry import trace from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter from opentelemetry.sdk.resources import SERVICE_NAME, Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter trace.set_tracer_provider( TracerProvider( resource=Resource.create({SERVICE_NAME: "my-zipkin-service"}))) tracer = trace.get_tracer(__name__) exporter = AzureMonitorTraceExporter.from_connection_string( os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]) otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317") span_processor = BatchSpanProcessor(otlp_exporter) trace.get_tracer_provider().add_span_processor(span_processor) with tracer.start_as_current_span("test"): print("Hello world!") input(...)
def test_no_credentials_error( self, mock_ssl_channel, mock_secure, mock_stub ): OTLPSpanExporter(insecure=False) self.assertTrue(mock_ssl_channel.called)
"service.instance.id": str(id(app)), "telemetry.sdk.name": "opentelemetry", "telemetry.sdk.language": "python", "telemetry.sdk.version": pkg_resources.get_distribution("opentelemetry-sdk").version, "host.hostname": socket.gethostname(), } ) ) ) tracerProvider = trace.get_tracer_provider() tracer = tracerProvider.get_tracer(__name__) tracerProvider.add_span_processor( SimpleSpanProcessor(ConsoleSpanExporter()) ) otlp_exporter = OTLPSpanExporter(endpoint="{}:55680".format(OTLP), insecure=True) tracerProvider.add_span_processor( SimpleSpanProcessor(otlp_exporter) ) DB_NAME = 'APM' HOST = os.getenv("MYSQL_HOST") if os.getenv("MYSQL_HOST") is not None else "localhost" PORT = int(os.getenv("MYSQL_PORT")) if os.getenv("MYSQL_PORT") is not None else 3306 TABLES = {} TABLES['Inventory_Items'] = ( "CREATE TABLE `Inventory_Items` (" " `ItemId` varchar(16) NOT NULL," " `TotalQty` int(11) NOT NULL," " PRIMARY KEY (`ItemId`)" ") ENGINE=InnoDB")
def test_otlp_headers(self, mock_ssl_channel, mock_secure): exporter = OTLPSpanExporter() # pylint: disable=protected-access self.assertIsNone(exporter._headers, None)
from opentelemetry import trace from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor from opentelemetry.sdk.resources import Resource from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter # Pass in the service we're creating into the tracer provider trace.set_tracer_provider( TracerProvider(resource=Resource.create( {"service.name": "python-app"}) ) ) # Create a BatchSpanProcessor and add the exporter to it # Add to the tracer trace.get_tracer_provider().add_span_processor( BatchSpanProcessor(OTLPSpanExporter( endpoint="https://otlp.nr-data.net:4317") ) ) tracer = trace.get_tracer(__name__) with tracer.start_as_current_span("foo"): with tracer.start_as_current_span("bar"): with tracer.start_as_current_span("baz"): print("Hello world from OpenTelemetry Python!")
def test_otlp_exporter_otlp_compression_kwarg(self, mock_insecure_channel): """Specifying kwarg should take precedence over env""" OTLPSpanExporter(insecure=True, compression=Compression.NoCompression) mock_insecure_channel.assert_called_once_with( "localhost:4317", compression=Compression.NoCompression )