Skip to content

Commit

Permalink
chore: adding type Literal to constants for db monitoring and http (#…
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
rachelyangdog authored Jul 22, 2024
1 parent ae1ab85 commit 9f30547
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
27 changes: 17 additions & 10 deletions ddtrace/propagation/_database_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
26 changes: 13 additions & 13 deletions ddtrace/propagation/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 9f30547

Please sign in to comment.