Skip to content

Commit

Permalink
Merge branch 'main' into brettlangdon/simplify.flask_simple
Browse files Browse the repository at this point in the history
  • Loading branch information
brettlangdon authored May 1, 2024
2 parents 3a91539 + 2107e4c commit bae7000
Show file tree
Hide file tree
Showing 25 changed files with 1,124 additions and 257 deletions.
2 changes: 2 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ stages:
- deploy
- benchmarks
- benchmarks-pr-comment
- macrobenchmarks

include:
- remote: https://gitlab-templates.ddbuild.io/apm/packaging.yml
- local: ".gitlab/benchmarks.yml"
- local: ".gitlab/macrobenchmarks.yml"

variables:
DOWNSTREAM_BRANCH:
Expand Down
86 changes: 86 additions & 0 deletions .gitlab/macrobenchmarks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
variables:
BASE_CI_IMAGE: 486234852809.dkr.ecr.us-east-1.amazonaws.com/ci/benchmarking-platform:dd-trace-py-macrobenchmarks

.macrobenchmarks:
stage: macrobenchmarks
needs: []
tags: ["runner:apm-k8s-same-cpu"]
timeout: 1h
rules:
- if: $CI_PIPELINE_SOURCE == "schedule"
when: always
- when: manual
## Next step, enable:
# - if: $CI_COMMIT_REF_NAME == "main"
# when: always
# If you have a problem with Gitlab cache, see Troubleshooting section in Benchmarking Platform docs
image: $BENCHMARKS_CI_IMAGE
script: |
git clone --branch python/macrobenchmarks https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.ddbuild.io/DataDog/benchmarking-platform platform && cd platform
if [ "$BP_PYTHON_SCENARIO_DIR" == "flask-realworld" ]; then
bp-runner bp-runner.flask-realworld.yml --debug
else
bp-runner bp-runner.simple.yml --debug
fi
artifacts:
name: "artifacts"
when: always
paths:
- platform/artifacts/
expire_in: 3 months
variables:
# Benchmark's env variables. Modify to tweak benchmark parameters.
DD_TRACE_DEBUG: "false"
DD_RUNTIME_METRICS_ENABLED: "true"
DD_REMOTE_CONFIGURATION_ENABLED: "false"
DD_INSTRUMENTATION_TELEMETRY_ENABLED: "false"

K6_OPTIONS_NORMAL_OPERATION_RATE: 40
K6_OPTIONS_NORMAL_OPERATION_DURATION: 5m
K6_OPTIONS_NORMAL_OPERATION_GRACEFUL_STOP: 1m
K6_OPTIONS_NORMAL_OPERATION_PRE_ALLOCATED_VUS: 4
K6_OPTIONS_NORMAL_OPERATION_MAX_VUS: 4

K6_OPTIONS_HIGH_LOAD_RATE: 500
K6_OPTIONS_HIGH_LOAD_DURATION: 1m
K6_OPTIONS_HIGH_LOAD_GRACEFUL_STOP: 30s
K6_OPTIONS_HIGH_LOAD_PRE_ALLOCATED_VUS: 4
K6_OPTIONS_HIGH_LOAD_MAX_VUS: 4

# Gitlab and BP specific env vars. Do not modify.
FF_USE_LEGACY_KUBERNETES_EXECUTION_STRATEGY: "true"

# Workaround: Currently we're not running the benchmarks on every PR, but GitHub still shows them as pending.
# By marking the benchmarks as allow_failure, this should go away. (This workaround should be removed once the
# benchmarks get changed to run on every PR)
allow_failure: true

macrobenchmarks:
extends: .macrobenchmarks
parallel:
matrix:
- DD_BENCHMARKS_CONFIGURATION: baseline
BP_PYTHON_SCENARIO_DIR: flask-realworld
DDTRACE_INSTALL_VERSION: "git+https://github.com/Datadog/dd-trace-py@${CI_COMMIT_SHA}"

- DD_BENCHMARKS_CONFIGURATION: only-tracing
BP_PYTHON_SCENARIO_DIR: flask-realworld
DDTRACE_INSTALL_VERSION: "git+https://github.com/Datadog/dd-trace-py@${CI_COMMIT_SHA}"

- DD_BENCHMARKS_CONFIGURATION: only-tracing
BP_PYTHON_SCENARIO_DIR: flask-realworld
DDTRACE_INSTALL_VERSION: "git+https://github.com/Datadog/dd-trace-py@${CI_COMMIT_SHA}"
DD_REMOTE_CONFIGURATION_ENABLED: "false"
DD_INSTRUMENTATION_TELEMETRY_ENABLED: "true"

- DD_BENCHMARKS_CONFIGURATION: only-tracing
BP_PYTHON_SCENARIO_DIR: flask-realworld
DDTRACE_INSTALL_VERSION: "git+https://github.com/Datadog/dd-trace-py@${CI_COMMIT_SHA}"
DD_REMOTE_CONFIGURATION_ENABLED: "false"
DD_INSTRUMENTATION_TELEMETRY_ENABLED: "false"

- DD_BENCHMARKS_CONFIGURATION: only-tracing
BP_PYTHON_SCENARIO_DIR: flask-realworld
DDTRACE_INSTALL_VERSION: "git+https://github.com/Datadog/dd-trace-py@${CI_COMMIT_SHA}"
DD_REMOTE_CONFIGURATION_ENABLED: "true"
DD_INSTRUMENTATION_TELEMETRY_ENABLED: "true"
59 changes: 45 additions & 14 deletions ddtrace/_trace/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ def __init__(

self.enabled = config._tracing_enabled
self.context_provider = context_provider or DefaultContextProvider()
self._user_sampler: Optional[BaseSampler] = None
# _user_sampler is the backup in case we need to revert from remote config to local
self._user_sampler: Optional[BaseSampler] = DatadogSampler()
self._sampler: BaseSampler = DatadogSampler()
self._dogstatsd_url = agent.get_stats_url() if dogstatsd_url is None else dogstatsd_url
self._compute_stats = config._trace_compute_stats
Expand Down Expand Up @@ -286,7 +287,7 @@ def __init__(
self._shutdown_lock = RLock()

self._new_process = False
config._subscribe(["_trace_sample_rate"], self._on_global_config_update)
config._subscribe(["_trace_sample_rate", "_trace_sampling_rules"], self._on_global_config_update)
config._subscribe(["logs_injection"], self._on_global_config_update)
config._subscribe(["tags"], self._on_global_config_update)
config._subscribe(["_tracing_enabled"], self._on_global_config_update)
Expand Down Expand Up @@ -1125,19 +1126,10 @@ def _is_span_internal(span):

def _on_global_config_update(self, cfg, items):
# type: (Config, List) -> None
if "_trace_sample_rate" in items:
# Reset the user sampler if one exists
if cfg._get_source("_trace_sample_rate") != "remote_config" and self._user_sampler:
self._sampler = self._user_sampler
return

if cfg._get_source("_trace_sample_rate") != "default":
sample_rate = cfg._trace_sample_rate
else:
sample_rate = None

sampler = DatadogSampler(default_sample_rate=sample_rate)
self._sampler = sampler
# sampling configs always come as a pair
if "_trace_sample_rate" in items and "_trace_sampling_rules" in items:
self._handle_sampler_update(cfg)

if "tags" in items:
self._tags = cfg.tags.copy()
Expand All @@ -1160,3 +1152,42 @@ def _on_global_config_update(self, cfg, items):
from ddtrace.contrib.logging import unpatch

unpatch()

def _handle_sampler_update(self, cfg):
# type: (Config) -> None
if (
cfg._get_source("_trace_sample_rate") != "remote_config"
and cfg._get_source("_trace_sampling_rules") != "remote_config"
and self._user_sampler
):
# if we get empty configs from rc for both sample rate and rules, we should revert to the user sampler
self.sampler = self._user_sampler
return

if cfg._get_source("_trace_sample_rate") != "remote_config" and self._user_sampler:
try:
sample_rate = self._user_sampler.default_sample_rate # type: ignore[attr-defined]
except AttributeError:
log.debug("Custom non-DatadogSampler is being used, cannot pull default sample rate")
sample_rate = None
elif cfg._get_source("_trace_sample_rate") != "default":
sample_rate = cfg._trace_sample_rate
else:
sample_rate = None

if cfg._get_source("_trace_sampling_rules") != "remote_config" and self._user_sampler:
try:
sampling_rules = self._user_sampler.rules # type: ignore[attr-defined]
# we need to chop off the default_sample_rate rule so the new sample_rate can be applied
sampling_rules = sampling_rules[:-1]
except AttributeError:
log.debug("Custom non-DatadogSampler is being used, cannot pull sampling rules")
sampling_rules = None
elif cfg._get_source("_trace_sampling_rules") != "default":
sampling_rules = DatadogSampler._parse_rules_from_str(cfg._trace_sampling_rules)
else:
sampling_rules = None

sampler = DatadogSampler(rules=sampling_rules, default_sample_rate=sample_rate)

self._sampler = sampler
8 changes: 6 additions & 2 deletions ddtrace/internal/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@

class _PRIORITY_CATEGORY:
USER = "user"
RULE = "rule"
RULE_DEF = "rule_default"
RULE_CUSTOMER = "rule_customer"
RULE_DYNAMIC = "rule_dynamic"
AUTO = "auto"
DEFAULT = "default"

Expand All @@ -99,7 +101,9 @@ class _PRIORITY_CATEGORY:
# used to simplify code that selects sampling priority based on many factors
_CATEGORY_TO_PRIORITIES = {
_PRIORITY_CATEGORY.USER: (USER_KEEP, USER_REJECT),
_PRIORITY_CATEGORY.RULE: (USER_KEEP, USER_REJECT),
_PRIORITY_CATEGORY.RULE_DEF: (USER_KEEP, USER_REJECT),
_PRIORITY_CATEGORY.RULE_CUSTOMER: (USER_KEEP, USER_REJECT),
_PRIORITY_CATEGORY.RULE_DYNAMIC: (USER_KEEP, USER_REJECT),
_PRIORITY_CATEGORY.AUTO: (AUTO_KEEP, AUTO_REJECT),
_PRIORITY_CATEGORY.DEFAULT: (AUTO_KEEP, AUTO_REJECT),
}
Expand Down
Loading

0 comments on commit bae7000

Please sign in to comment.