-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
321 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Copyright 2020, Google LLC All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging | ||
from contextlib import contextmanager | ||
|
||
from google.api_core.exceptions import GoogleAPICallError | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
try: | ||
from opentelemetry import trace | ||
from opentelemetry import propagators | ||
from opentelemetry.trace import SpanContext | ||
from opentelemetry.trace import get_current_span | ||
from opentelemetry.trace import set_span_in_context | ||
from opentelemetry.trace.status import Status | ||
from opentelemetry.instrumentation.utils import http_status_to_canonical_code | ||
|
||
USE_OPENTELEMETRY = True | ||
except ImportError: | ||
_LOGGER.info( | ||
"This service supports OpenTelemetry, but OpenTelemetry could" | ||
"not be imported. To use OpenTelemetry, please install the" | ||
"opentelemetry-api, opentelemetry-sdk, and opentelemetry-instrumentation" | ||
"pip modules. See also" | ||
"https://opentelemetry-python.readthedocs.io/en/stable/getting-started.html" | ||
) | ||
USE_OPENTELEMETRY = False | ||
pass | ||
|
||
|
||
@contextmanager | ||
def create_span(span_name, attributes=None, parent=None): | ||
""" Creates a new span | ||
Args: | ||
span_name (str): the name of the new span | ||
attributes ([dict], optional): A dictionary | ||
containing all attributes to add to a span. Defaults to None. | ||
parent ([dict], optional): A dictionary | ||
containing the attributes of a . Defaults to None. | ||
Yields: | ||
[opentelemetry.trace.Span]: The newly created span, or None if | ||
OpenTelemetry could not be imported | ||
""" | ||
|
||
# OpenTelemetry could not be imported. | ||
if not USE_OPENTELEMETRY: | ||
yield None | ||
return | ||
|
||
tracer = trace.get_tracer(__name__) | ||
|
||
if parent is not None: | ||
# Form the parent's context from the parent dict provided | ||
try: | ||
parent_span_context = SpanContext( | ||
trace_id=parent["trace_id"], | ||
span_id=parent["span_id"], | ||
is_remote=parent["is_remote"], | ||
trace_flags=parent["trace_flags"], | ||
trace_state=parent["trace_state"], | ||
) | ||
except: | ||
parent_span_context = None | ||
else: | ||
parent_span_context = None | ||
|
||
# Create a new span and yield it | ||
with tracer.start_as_current_span( | ||
span_name, attributes=attributes, parent=parent_span_context | ||
) as span: | ||
try: | ||
yield span | ||
except GoogleAPICallError as error: | ||
if error.code is not None: | ||
span.set_status(Status(http_status_to_canonical_code(error.code))) | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.