Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Otel semantic convention stability opt-in #1987

Merged
merged 12 commits into from
Nov 10, 2023
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased


### Added

- `opentelemetry-instrumentation-system-metrics` Add support for collecting process metrics
([#1948](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1948))
- `opentelemetry-instrumentation` Added Otel semantic convention opt-in mechanism
([#1987](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1987))

### Fixed

Expand All @@ -19,7 +21,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-resource-detector-azure` Using new Cloud Resource ID attribute.
([#1976](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1976))


## Version 1.20.0/0.41b0 (2023-09-01)

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
DependencyConflict,
get_dependency_conflicts,
)
from opentelemetry.instrumentation.utils import (
_OpenTelemetrySemanticConventionStability,
)

_LOG = getLogger(__name__)

Expand Down Expand Up @@ -105,6 +108,9 @@ def instrument(self, **kwargs):
_LOG.error(conflict)
return None

# initialize semantic conventions opt-in if needed
_OpenTelemetrySemanticConventionStability._initialize()

result = self._instrument( # pylint: disable=assignment-from-no-return
**kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import threading
import urllib.parse
from re import escape, sub
from typing import Dict, Sequence
Expand Down Expand Up @@ -152,3 +154,57 @@ def _python_path_without_directory(python_path, directory, path_separator):
"",
python_path,
)


_OTEL_SEMCONV_STABILITY_OPT_IN_KEY = "OTEL_SEMCONV_STABILITY_OPT_IN"
lzchen marked this conversation as resolved.
Show resolved Hide resolved
jeremydvoss marked this conversation as resolved.
Show resolved Hide resolved


class _OpenTelemetryStabilitySignalType:
HTTP = "http"


class _OpenTelemetryStabilityMode:
jeremydvoss marked this conversation as resolved.
Show resolved Hide resolved
# http - emit the new, stable HTTP and networking conventions ONLY
jeremydvoss marked this conversation as resolved.
Show resolved Hide resolved
HTTP = "http"
# http/dup - emit both the old and the stable HTTP and networking conventions
HTTP_DUP = "http/dup"
# default - continue emitting old experimental HTTP and networking conventions
DEFAULT = "default"


class _OpenTelemetrySemanticConventionStability:
lzchen marked this conversation as resolved.
Show resolved Hide resolved
_initialized = False
_lock = threading.Lock()
_OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING = {}

@classmethod
def _initialize(cls):
with _OpenTelemetrySemanticConventionStability._lock:
if not _OpenTelemetrySemanticConventionStability._initialized:
# Users can pass in comma delimited string for opt-in options
# Only values for http stability are supported for now
opt_in = os.environ.get(_OTEL_SEMCONV_STABILITY_OPT_IN_KEY, "")
opt_in_list = []
if opt_in:
jeremydvoss marked this conversation as resolved.
Show resolved Hide resolved
opt_in_list = [s.strip() for s in opt_in.split(",")]
http_opt_in = _OpenTelemetryStabilityMode.DEFAULT
if opt_in_list:
# Process http opt-in
# http/dup takes priority over http
if _OpenTelemetryStabilityMode.HTTP_DUP in opt_in_list:
http_opt_in = _OpenTelemetryStabilityMode.HTTP_DUP
elif _OpenTelemetryStabilityMode.HTTP in opt_in_list:
http_opt_in = _OpenTelemetryStabilityMode.HTTP
_OpenTelemetrySemanticConventionStability._OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING[
_OpenTelemetryStabilitySignalType.HTTP
] = http_opt_in
_OpenTelemetrySemanticConventionStability._initialized = True

@classmethod
def _get_opentelemetry_stability_opt_in(
type: _OpenTelemetryStabilitySignalType,
) -> _OpenTelemetryStabilityMode:
with _OpenTelemetrySemanticConventionStability._lock:
return _OpenTelemetrySemanticConventionStability._OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING.get(
type, _OpenTelemetryStabilityMode.DEFAULT
)
Loading