4444:class:`opentelemetry.sdk.trace.export.BatchSpanProcessor` with the
4545default parameters for performance reasons.
4646
47+ Auto-instrumentation
48+ --------------------
49+
50+ This exporter can also be used with `OpenTelemetry auto-instrumentation
51+ <https://opentelemetry.io/docs/instrumentation/python/automatic/>`_:
52+
53+ .. code-block:: sh
54+
55+ opentelemetry-instrument --traces_exporter gcp_trace <command> <args>
56+
57+ Configuration is supported through environment variables
58+ (:mod:`opentelemetry.exporter.cloud_trace.environment_variables`) or the corresponding command
59+ line arguments to ``opentelemetry-instrument``:
60+
61+ .. code-block:: sh
62+
63+ opentelemetry-instrument --traces_exporter gcp_trace \\
64+ --exporter_gcp_trace_project_id my-project \\
65+ <command> <args>
66+
67+ See ``opentelemetry-instrument --help`` for all configuration options.
68+
4769API
4870---
4971"""
5072
5173import logging
5274import re
5375from collections .abc import Sequence as SequenceABC
76+ from os import environ
5477from typing import (
5578 Any ,
5679 Dict ,
6790import pkg_resources
6891from google .cloud .trace_v2 import BatchWriteSpansRequest , TraceServiceClient
6992from google .cloud .trace_v2 import types as trace_types
70- from google .protobuf .timestamp_pb2 import Timestamp
93+ from google .protobuf .timestamp_pb2 import ( # pylint: disable=no-name-in-module
94+ Timestamp ,
95+ )
7196from google .rpc import code_pb2 , status_pb2
97+ from opentelemetry .exporter .cloud_trace .environment_variables import (
98+ OTEL_EXPORTER_GCP_TRACE_PROJECT_ID ,
99+ OTEL_EXPORTER_GCP_TRACE_RESOURCE_REGEX ,
100+ )
72101from opentelemetry .exporter .cloud_trace .version import __version__
73102from opentelemetry .sdk .resources import Resource
74103from opentelemetry .sdk .trace import Event
@@ -97,11 +126,13 @@ class CloudTraceSpanExporter(SpanExporter):
97126 """Cloud Trace span exporter for OpenTelemetry.
98127
99128 Args:
100- project_id: ID of the cloud project that will receive the traces.
129+ project_id: GCP project ID for the project to send spans to. Alternatively, can be
130+ configured with :envvar:`OTEL_EXPORTER_GCP_TRACE_PROJECT_ID`.
101131 client: Cloud Trace client. If not given, will be taken from gcloud
102132 default credentials
103- resource_regex: Resource attributes with keys matching this regex will be
104- added to exported spans as labels (default: None).
133+ resource_regex: Resource attributes with keys matching this regex will be added to
134+ exported spans as labels (default: None). Alternatively, can be configured with
135+ :envvar:`OTEL_EXPORTER_GCP_TRACE_RESOURCE_REGEX`.
105136 """
106137
107138 def __init__ (
@@ -112,9 +143,15 @@ def __init__(
112143 ):
113144 self .client : TraceServiceClient = client or TraceServiceClient ()
114145 if not project_id :
115- _ , self .project_id = google .auth .default ()
116- else :
117- self .project_id = project_id
146+ project_id = environ .get (OTEL_EXPORTER_GCP_TRACE_PROJECT_ID )
147+ if not project_id :
148+ _ , project_id = google .auth .default ()
149+ self .project_id = project_id
150+
151+ if not resource_regex :
152+ resource_regex = environ .get (
153+ OTEL_EXPORTER_GCP_TRACE_RESOURCE_REGEX
154+ )
118155 self .resource_regex = (
119156 re .compile (resource_regex ) if resource_regex else None
120157 )
0 commit comments