From 9f30547ca1410b3924e4e0f1b65beff5d1bcb338 Mon Sep 17 00:00:00 2001 From: Rachel Yang Date: Mon, 22 Jul 2024 14:38:48 -0400 Subject: [PATCH] chore: adding type Literal to constants for db monitoring and http (#9883) Any constant can be typed as being a literal instead of a type. This change will ensure that anywhere we use a constant for typing it means literally the value instead of a type of str. In order for this to work for Python < 3.8 we will need to add typing_extensions as a dependency. ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) --- ddtrace/propagation/_database_monitoring.py | 27 +++++++++++++-------- ddtrace/propagation/http.py | 26 ++++++++++---------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/ddtrace/propagation/_database_monitoring.py b/ddtrace/propagation/_database_monitoring.py index 9faf4f5fb2a..5b585b13210 100644 --- a/ddtrace/propagation/_database_monitoring.py +++ b/ddtrace/propagation/_database_monitoring.py @@ -19,16 +19,23 @@ from ddtrace import Span # noqa:F401 -DBM_PARENT_SERVICE_NAME_KEY = "ddps" -DBM_DATABASE_SERVICE_NAME_KEY = "dddbs" -DBM_PEER_HOSTNAME_KEY = "ddh" -DBM_PEER_DB_NAME_KEY = "dddb" -DBM_PEER_SERVICE_KEY = "ddprs" -DBM_ENVIRONMENT_KEY = "dde" -DBM_VERSION_KEY = "ddpv" -DBM_TRACE_PARENT_KEY = "traceparent" -DBM_TRACE_INJECTED_TAG = "_dd.dbm_trace_injected" - +import sys + + +if sys.version_info >= (3, 8): + from typing import Literal # noqa:F401 +else: + from typing_extensions import Literal # noqa:F401 + +DBM_PARENT_SERVICE_NAME_KEY: Literal["ddps"] = "ddps" +DBM_DATABASE_SERVICE_NAME_KEY: Literal["dddbs"] = "dddbs" +DBM_PEER_HOSTNAME_KEY: Literal["ddh"] = "ddh" +DBM_PEER_DB_NAME_KEY: Literal["dddb"] = "dddb" +DBM_PEER_SERVICE_KEY: Literal["ddprs"] = "ddprs" +DBM_ENVIRONMENT_KEY: Literal["dde"] = "dde" +DBM_VERSION_KEY: Literal["ddpv"] = "ddpv" +DBM_TRACE_PARENT_KEY: Literal["traceparent"] = "traceparent" +DBM_TRACE_INJECTED_TAG: Literal["_dd.dbm_trace_injected"] = "_dd.dbm_trace_injected" log = get_logger(__name__) diff --git a/ddtrace/propagation/http.py b/ddtrace/propagation/http.py index ddbd2f14ef1..f5d21327d59 100644 --- a/ddtrace/propagation/http.py +++ b/ddtrace/propagation/http.py @@ -62,19 +62,19 @@ # HTTP headers one should set for distributed tracing. # These are cross-language (eg: Python, Go and other implementations should honor these) -_HTTP_BAGGAGE_PREFIX = "ot-baggage-" -HTTP_HEADER_TRACE_ID = "x-datadog-trace-id" -HTTP_HEADER_PARENT_ID = "x-datadog-parent-id" -HTTP_HEADER_SAMPLING_PRIORITY = "x-datadog-sampling-priority" -HTTP_HEADER_ORIGIN = "x-datadog-origin" -_HTTP_HEADER_B3_SINGLE = "b3" -_HTTP_HEADER_B3_TRACE_ID = "x-b3-traceid" -_HTTP_HEADER_B3_SPAN_ID = "x-b3-spanid" -_HTTP_HEADER_B3_SAMPLED = "x-b3-sampled" -_HTTP_HEADER_B3_FLAGS = "x-b3-flags" -_HTTP_HEADER_TAGS = "x-datadog-tags" -_HTTP_HEADER_TRACEPARENT = "traceparent" -_HTTP_HEADER_TRACESTATE = "tracestate" +_HTTP_BAGGAGE_PREFIX: Literal["ot-baggage-"] = "ot-baggage-" +HTTP_HEADER_TRACE_ID: Literal["x-datadog-trace-id"] = "x-datadog-trace-id" +HTTP_HEADER_PARENT_ID: Literal["x-datadog-parent-id"] = "x-datadog-parent-id" +HTTP_HEADER_SAMPLING_PRIORITY: Literal["x-datadog-sampling-priority"] = "x-datadog-sampling-priority" +HTTP_HEADER_ORIGIN: Literal["x-datadog-origin"] = "x-datadog-origin" +_HTTP_HEADER_B3_SINGLE: Literal["b3"] = "b3" +_HTTP_HEADER_B3_TRACE_ID: Literal["x-b3-traceid"] = "x-b3-traceid" +_HTTP_HEADER_B3_SPAN_ID: Literal["x-b3-spanid"] = "x-b3-spanid" +_HTTP_HEADER_B3_SAMPLED: Literal["x-b3-sampled"] = "x-b3-sampled" +_HTTP_HEADER_B3_FLAGS: Literal["x-b3-flags"] = "x-b3-flags" +_HTTP_HEADER_TAGS: Literal["x-datadog-tags"] = "x-datadog-tags" +_HTTP_HEADER_TRACEPARENT: Literal["traceparent"] = "traceparent" +_HTTP_HEADER_TRACESTATE: Literal["tracestate"] = "tracestate" def _possible_header(header):