forked from DataDog/dd-trace-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(peer.service): peer.service logic + flag (DataDog#6042)
== Special Notes == These changes are gated behind a feature flag and are not expected to be stable until a release note is generated. Existing customers should be unaffected. The logic relies on 'precursor' tags, which are being added in other PRs. Logic will not be fully enabled until until the precursors are completed. These changes are reliant on service naming as well, and will not be fully enabled until that initiative is complete. == Peer.Service Summary == The changes for peer.service will enable new functionality in the service map, when enabled, in conjunction with service naming. The changes in this PR are: 1. Introduce the peer.service computation, gated behind a feature flag 2. Add in the peer.service and peer.service.source computation logic When peer service is enabled, the logic is applied as a post-processor on spans, as part of the "on_finish" methods enabled. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines) are followed. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). ## Reviewer Checklist - [ ] Title is accurate. - [ ] No unnecessary changes are introduced. - [ ] Description motivates each change. - [ ] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [ ] Testing strategy adequately addresses listed risk(s). - [ ] Change is maintainable (easy to change, telemetry, documentation). - [ ] Release note makes sense to a user of the library. - [ ] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment.
- Loading branch information
Showing
18 changed files
with
219 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import os | ||
|
||
from ddtrace.ext import SpanKind | ||
from ddtrace.internal.schema import SCHEMA_VERSION | ||
from ddtrace.internal.utils.formats import asbool | ||
|
||
|
||
class PeerServiceConfig(object): | ||
source_tag_name = "_dd.peer.service.source" | ||
tag_name = "peer.service" | ||
enabled_span_kinds = {SpanKind.CLIENT, SpanKind.PRODUCER} | ||
prioritized_data_sources = ["messaging.kafka.bootstrap.servers", "db.name", "rpc.service", "out.host"] | ||
|
||
@property | ||
def enabled(self): | ||
env_enabled = asbool(os.getenv("DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED", default=False)) | ||
|
||
return SCHEMA_VERSION == "v1" or (SCHEMA_VERSION == "v0" and env_enabled) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import os | ||
|
||
import mock | ||
import pytest | ||
|
||
from ddtrace.constants import SPAN_KIND | ||
from ddtrace.ext import SpanKind | ||
from ddtrace.internal.processor.trace import PeerServiceProcessor | ||
from ddtrace.settings.peer_service import PeerServiceConfig | ||
from ddtrace.span import Span | ||
|
||
|
||
@pytest.fixture | ||
def peer_service_config(): | ||
return PeerServiceConfig() | ||
|
||
|
||
@pytest.fixture | ||
def processor(peer_service_config): | ||
return PeerServiceProcessor(peer_service_config) | ||
|
||
|
||
@pytest.fixture | ||
def test_span(): | ||
return Span( | ||
"test_messaging_span", | ||
service="test_service", | ||
resource="test_resource", | ||
span_type="test_span_type", | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("span_kind", [SpanKind.CLIENT, SpanKind.PRODUCER]) | ||
def test_processing_peer_service_exists(processor, test_span, span_kind, peer_service_config): | ||
processor.enabled = True | ||
test_span.set_tag(SPAN_KIND, span_kind) | ||
test_span.set_tag(peer_service_config.tag_name, "fake_peer_service") | ||
test_span.set_tag("out.host", "fake_falue") # Should not show up | ||
processor.on_span_finish(test_span) | ||
|
||
assert test_span.get_tag(peer_service_config.tag_name) == "fake_peer_service" | ||
assert test_span.get_tag(peer_service_config.source_tag_name) == "peer.service" | ||
|
||
|
||
@pytest.mark.parametrize("span_kind", [SpanKind.SERVER, SpanKind.CONSUMER]) | ||
def test_nothing_happens_for_server_and_consumer(processor, test_span, span_kind, peer_service_config): | ||
processor.enabled = True | ||
test_span.set_tag(SPAN_KIND, span_kind) | ||
test_span.set_tag("out.host", "fake_host") | ||
processor.on_span_finish(test_span) | ||
|
||
assert test_span.get_tag(peer_service_config.source_tag_name) is None | ||
|
||
|
||
@pytest.mark.parametrize("data_source", PeerServiceConfig.prioritized_data_sources) | ||
def test_existing_data_sources(processor, test_span, data_source, peer_service_config): | ||
processor.enabled = True | ||
test_span.set_tag(SPAN_KIND, SpanKind.CLIENT) | ||
test_span.set_tag(data_source, "test_value") | ||
|
||
processor.on_span_finish(test_span) | ||
|
||
assert test_span.get_tag(peer_service_config.tag_name) == "test_value" | ||
assert test_span.get_tag(peer_service_config.source_tag_name) == data_source | ||
|
||
|
||
@pytest.mark.parametrize("data_source", PeerServiceConfig.prioritized_data_sources) | ||
def test_disabled_peer_service(processor, test_span, data_source, peer_service_config): | ||
processor.enabled = False | ||
test_span.set_tag(data_source, "test_value") | ||
processor.on_span_finish(test_span) | ||
|
||
assert test_span.get_tag(peer_service_config.tag_name) is None | ||
assert test_span.get_tag(peer_service_config.source_tag_name) is None | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"schema_peer_enabled", | ||
[ | ||
("v0", False, False), | ||
("v0", True, True), | ||
("v1", False, True), | ||
("v1", True, True), | ||
], | ||
) | ||
def fake_peer_service_enablement(span, schema_peer_enabled): | ||
schema_version, env_enabled, expected = schema_peer_enabled | ||
|
||
with mock.patch.dict(os.environ, {"DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED": env_enabled}): | ||
with mock.patch("ddtrace.internal.schema.SCHEMA_VERSION", schema_version): | ||
assert PeerServiceConfig().enabled == expected | ||
|
||
|
||
@pytest.mark.subprocess(env=dict(DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED="True"), ddtrace_run=True) | ||
def test_tracer_hooks(): | ||
from ddtrace.constants import SPAN_KIND | ||
from ddtrace.ext import SpanKind | ||
from ddtrace.settings.peer_service import PeerServiceConfig | ||
from tests.utils import DummyTracer | ||
|
||
peer_service_config = PeerServiceConfig() | ||
tracer = DummyTracer() | ||
span = tracer.trace( | ||
"test", | ||
service="test_service", | ||
resource="test_resource", | ||
span_type="test_span_type", | ||
) | ||
span.set_tag(SPAN_KIND, SpanKind.CLIENT) | ||
span.set_tag("out.host", "test_value") | ||
|
||
span.finish() | ||
|
||
assert span.get_tag(peer_service_config.tag_name) == "test_value" | ||
assert span.get_tag(peer_service_config.source_tag_name) == "out.host" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.