OpenTelemetry Cloud Trace Exporter¶
Cloud Trace Span Exporter for OpenTelemetry. Uses Cloud Trace Client’s REST API to export traces and spans for viewing in Cloud Trace.
Usage¶
from opentelemetry import trace
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
from opentelemetry.sdk.trace import TracerProvider
# For debugging
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
# Otherwise
from opentelemetry.sdk.trace.export import BatchSpanProcessor
trace.set_tracer_provider(TracerProvider())
cloud_trace_exporter = CloudTraceSpanExporter()
trace.get_tracer_provider().add_span_processor(
BatchSpanProcessor(cloud_trace_exporter)
)
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("foo"):
print("Hello world!")
When not debugging, make sure to use
opentelemetry.sdk.trace.export.BatchSpanProcessor
with the
default parameters for performance reasons.
Auto-instrumentation¶
This exporter can also be used with OpenTelemetry auto-instrumentation:
opentelemetry-instrument --traces_exporter gcp_trace <command> <args>
Configuration is supported through environment variables
(opentelemetry.exporter.cloud_trace.environment_variables
) or the corresponding command
line arguments to opentelemetry-instrument
:
opentelemetry-instrument --traces_exporter gcp_trace \
--exporter_gcp_trace_project_id my-project \
<command> <args>
See opentelemetry-instrument --help
for all configuration options.
API¶
- class opentelemetry.exporter.cloud_trace.CloudTraceSpanExporter(project_id=None, client=None, resource_regex=None)[source]
Bases:
SpanExporter
Cloud Trace span exporter for OpenTelemetry.
- Parameters:
project_id – GCP project ID for the project to send spans to. Alternatively, can be configured with
OTEL_EXPORTER_GCP_TRACE_PROJECT_ID
.client – Cloud Trace client. If not given, will be taken from gcloud default credentials
resource_regex – Resource attributes with keys matching this regex will be added to exported spans as labels (default: None). Alternatively, can be configured with
OTEL_EXPORTER_GCP_TRACE_RESOURCE_REGEX
.
- export(spans)[source]
Export the spans to Cloud Trace.
See: https://cloud.google.com/trace/docs/reference/v2/rest/v2/projects.traces/batchWrite
- Parameters:
spans (
Sequence
[ReadableSpan
]) – Sequence of spans to export- Return type:
- shutdown()[source]
Shuts down the exporter.
Called when the SDK is shut down.