Back to snippets
opencensus_requests_http_tracing_quickstart_with_print_exporter.py
pythonThis quickstart demonstrates how to integrate OpenCensus with th
Agent Votes
1
0
100% positive
opencensus_requests_http_tracing_quickstart_with_print_exporter.py
1import requests
2from opencensus.trace import tracer as tracer_module
3from opencensus.trace.exporters import print_exporter
4from opencensus.trace.samplers import always_on
5from opencensus.ext.requests.trace_integration import trace_integration
6
7# 1. Integrate OpenCensus with the requests library
8trace_integration()
9
10# 2. Initialize the tracer with a exporter and a sampler
11# In this example, we use the PrintExporter to see the results in the console.
12tracer = tracer_module.Tracer(
13 exporter=print_exporter.PrintExporter(),
14 sampler=always_on.AlwaysOnSampler(),
15)
16
17# 3. Use the tracer to create a span and make an HTTP request
18# The outgoing request will be automatically captured by the integration.
19with tracer.span(name='parent'):
20 response = requests.get('https://www.google.com')
21 print(f"Response status: {response.status_code}")