Skip to content

Commit 322d1c4

Browse files
authored
Add entry point for Cloud Trace exporter to work with auto instrumentation (#179)
1 parent 02b720c commit 322d1c4

File tree

5 files changed

+117
-7
lines changed

5 files changed

+117
-7
lines changed

opentelemetry-exporter-gcp-trace/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- Add entry point for Cloud Trace exporter to work with auto instrumentation
6+
([#179](https://github.com/GoogleCloudPlatform/opentelemetry-operations-python/pull/179))
7+
58
## Version 1.1.0
69

710
Released 2022-01-13

opentelemetry-exporter-gcp-trace/setup.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@ where = src
3434

3535
[options.extras_require]
3636
test =
37+
38+
[options.entry_points]
39+
opentelemetry_traces_exporter =
40+
gcp_trace = opentelemetry.exporter.cloud_trace:CloudTraceSpanExporter
41+
opentelemetry_environment_variables =
42+
gcp_trace = opentelemetry.exporter.cloud_trace.environment_variables

opentelemetry-exporter-gcp-trace/src/opentelemetry/exporter/cloud_trace/__init__.py

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,36 @@
4444
:class:`opentelemetry.sdk.trace.export.BatchSpanProcessor` with the
4545
default 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+
4769
API
4870
---
4971
"""
5072

5173
import logging
5274
import re
5375
from collections.abc import Sequence as SequenceABC
76+
from os import environ
5477
from typing import (
5578
Any,
5679
Dict,
@@ -67,8 +90,14 @@
6790
import pkg_resources
6891
from google.cloud.trace_v2 import BatchWriteSpansRequest, TraceServiceClient
6992
from 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+
)
7196
from 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+
)
72101
from opentelemetry.exporter.cloud_trace.version import __version__
73102
from opentelemetry.sdk.resources import Resource
74103
from 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
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
OTEL_EXPORTER_GCP_TRACE_PROJECT_ID = "OTEL_EXPORTER_GCP_TRACE_PROJECT_ID"
16+
"""
17+
.. envvar:: OTEL_EXPORTER_GCP_TRACE_PROJECT_ID
18+
19+
GCP project ID for the project to send spans to. Equivalent to constructor parameter to
20+
:class:`opentelemetry.exporter.cloud_trace.CloudTraceSpanExporter`.
21+
"""
22+
23+
OTEL_EXPORTER_GCP_TRACE_RESOURCE_REGEX = (
24+
"OTEL_EXPORTER_GCP_TRACE_RESOURCE_REGEX"
25+
)
26+
"""
27+
.. envvar:: OTEL_EXPORTER_GCP_TRACE_RESOURCE_REGEX
28+
29+
Resource attributes with keys matching this regex will be added to exported spans as labels
30+
:class:`opentelemetry.exporter.cloud_trace.CloudTraceSpanExporter`. Equivalent to constructor parameter to
31+
:class:`opentelemetry.exporter.cloud_trace.CloudTraceSpanExporter`.
32+
"""
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from unittest import TestCase
16+
17+
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
18+
19+
# private import for testing only
20+
from opentelemetry.sdk._configuration import _import_exporters
21+
22+
23+
class TestCloudTraceAutoInstrument(TestCase):
24+
def test_loads_cloud_trace_exporter(self):
25+
"""Test that OTel configuration internals can load the trace exporter from entrypoint
26+
by name"""
27+
trace_exporters, _ = _import_exporters(
28+
trace_exporter_names=["gcp_trace"], log_exporter_names=[]
29+
)
30+
self.assertEqual(
31+
trace_exporters, {"gcp_trace": CloudTraceSpanExporter}
32+
)

0 commit comments

Comments
 (0)