Skip to content

Commit b6d77f1

Browse files
authored
Implement Otel semantic convention stability opt-in (#1987)
1 parent 7166de6 commit b6d77f1

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

Diff for: CHANGELOG.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10-
## Version 1.21.0/0.42b0 ()
10+
### Added
1111

12-
- `opentelemetry-instrumentation-aiohttp-server` Add instrumentor and auto instrumentation support for aiohttp-server
13-
([#1800](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1800))
12+
- `opentelemetry-instrumentation` Added Otel semantic convention opt-in mechanism
13+
([#1987](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1987))
14+
15+
## Version 1.21.0/0.42b0 (2023-11-01)
1416

1517
### Added
1618

19+
- `opentelemetry-instrumentation-aiohttp-server` Add instrumentor and auto instrumentation support for aiohttp-server
20+
([#1800](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1800))
1721
- `opentelemetry-instrumentation-botocore` Include SNS topic ARN as a span attribute with name `messaging.destination.name` to uniquely identify the SNS topic
1822
([#1995](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1995))
1923
- `opentelemetry-instrumentation-system-metrics` Add support for collecting process metrics

Diff for: opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
DependencyConflict,
2626
get_dependency_conflicts,
2727
)
28+
from opentelemetry.instrumentation.utils import (
29+
_OpenTelemetrySemanticConventionStability,
30+
)
2831

2932
_LOG = getLogger(__name__)
3033

@@ -105,6 +108,9 @@ def instrument(self, **kwargs):
105108
_LOG.error(conflict)
106109
return None
107110

111+
# initialize semantic conventions opt-in if needed
112+
_OpenTelemetrySemanticConventionStability._initialize()
113+
108114
result = self._instrument( # pylint: disable=assignment-from-no-return
109115
**kwargs
110116
)

Diff for: opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py

+60
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
16+
import threading
1517
import urllib.parse
18+
from enum import Enum
1619
from re import escape, sub
1720
from typing import Dict, Sequence
1821

@@ -152,3 +155,60 @@ def _python_path_without_directory(python_path, directory, path_separator):
152155
"",
153156
python_path,
154157
)
158+
159+
160+
_OTEL_SEMCONV_STABILITY_OPT_IN_KEY = "OTEL_SEMCONV_STABILITY_OPT_IN"
161+
162+
163+
class _OpenTelemetryStabilitySignalType:
164+
HTTP = "http"
165+
166+
167+
class _OpenTelemetryStabilityMode(Enum):
168+
# http - emit the new, stable HTTP and networking conventions ONLY
169+
HTTP = "http"
170+
# http/dup - emit both the old and the stable HTTP and networking conventions
171+
HTTP_DUP = "http/dup"
172+
# default - continue emitting old experimental HTTP and networking conventions
173+
DEFAULT = "default"
174+
175+
176+
class _OpenTelemetrySemanticConventionStability:
177+
_initialized = False
178+
_lock = threading.Lock()
179+
_OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING = {}
180+
181+
@classmethod
182+
def _initialize(cls):
183+
with _OpenTelemetrySemanticConventionStability._lock:
184+
if not _OpenTelemetrySemanticConventionStability._initialized:
185+
# Users can pass in comma delimited string for opt-in options
186+
# Only values for http stability are supported for now
187+
opt_in = os.environ.get(_OTEL_SEMCONV_STABILITY_OPT_IN_KEY, "")
188+
opt_in_list = []
189+
if opt_in:
190+
opt_in_list = [s.strip() for s in opt_in.split(",")]
191+
http_opt_in = _OpenTelemetryStabilityMode.DEFAULT
192+
if opt_in_list:
193+
# Process http opt-in
194+
# http/dup takes priority over http
195+
if (
196+
_OpenTelemetryStabilityMode.HTTP_DUP.value
197+
in opt_in_list
198+
):
199+
http_opt_in = _OpenTelemetryStabilityMode.HTTP_DUP
200+
elif _OpenTelemetryStabilityMode.HTTP.value in opt_in_list:
201+
http_opt_in = _OpenTelemetryStabilityMode.HTTP
202+
_OpenTelemetrySemanticConventionStability._OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING[
203+
_OpenTelemetryStabilitySignalType.HTTP
204+
] = http_opt_in
205+
_OpenTelemetrySemanticConventionStability._initialized = True
206+
207+
@classmethod
208+
def _get_opentelemetry_stability_opt_in(
209+
type: _OpenTelemetryStabilitySignalType,
210+
) -> _OpenTelemetryStabilityMode:
211+
with _OpenTelemetrySemanticConventionStability._lock:
212+
return _OpenTelemetrySemanticConventionStability._OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING.get(
213+
type, _OpenTelemetryStabilityMode.DEFAULT
214+
)

0 commit comments

Comments
 (0)