Skip to content

Commit

Permalink
Add some how-to examples (#1905)
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanthccv authored Jun 11, 2021
1 parent 540b8a5 commit bab5011
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions docs/faq-and-cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,69 @@ Capturing baggage at different contexts
print(baggage.get_baggage("context", parent_ctx))
print(baggage.get_baggage("context", child_ctx))
Manually setting span context
-----------------------------

.. code-block:: python
from opentelemetry import trace
from opentelemetry.trace import NonRecordingSpan, SpanContext, TraceFlags
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
tracer = trace.get_tracer(__name__)
# Extracting from carrier header
carrier = {'traceparent': '00-a9c3b99a95cc045e573e163c3ac80a77-d99d251a8caecd06-01'}
ctx = TraceContextTextMapPropagator().extract(carrier=carrier)
with tracer.start_as_current_span('child', context=ctx) as span:
span.set_attribute('primes', [2, 3, 5, 7])
# Or if you have a SpanContext object already.
span_context = SpanContext(
trace_id=2604504634922341076776623263868986797,
span_id=5213367945872657620,
is_remote=True,
trace_flags=TraceFlags(0x01)
)
ctx = trace.set_span_in_context(NonRecordingSpan(span_context))
with tracer.start_as_current_span("child", context=ctx) as span:
span.set_attribute('evens', [2, 4, 6, 8])
Using multiple tracer providers with different Resource
-------------------------------------------------------

.. code-block:: python
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
# Global tracer provider which can be set only once
trace.set_tracer_provider(
TracerProvider(resource=Resource.create({"service.name": "service1"}))
)
trace.get_tracer_provider().add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("some-name") as span:
span.set_attribute("key", "value")
another_tracer_provider = TracerProvider(
resource=Resource.create({"service.name": "service2"})
)
another_tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
another_tracer = trace.get_tracer(__name__, tracer_provider=another_tracer_provider)
with another_tracer.start_as_current_span("name-here") as span:
span.set_attribute("another-key", "another-value")

0 comments on commit bab5011

Please sign in to comment.