-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Flow run OTEL instrumentation #16010
Merged
+435
−5
Merged
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
61dea5f
Begin integrating open telementry instrumentation setup
bunchesofdonald fcedab8
add otel instrumentation to flow engine.
collincchoy 878dbc9
Merge branch 'CLOUD-563-config' into flow-run-instrumentation
collincchoy 136fee6
Add flow name as attribute to flow run spans.
collincchoy b9ad0a7
Merge branch 'main' into flow-run-instrumentation
collincchoy a2dc3dc
Propogate flow run labels through span attributes.
collincchoy 595b981
Add test for basic flow run instrumentation.
collincchoy 5dcdaf4
Add more tests to cover spans in error state -- do not use inflight s…
collincchoy 9408447
typing.
collincchoy 585a171
Ensure flow tags are captured in spans.
collincchoy 13fbde5
Add span event assertions.
collincchoy f9d0e5a
Add assertions that spans are instrumented by/for prefect.
collincchoy 9b06873
Merge branch 'main' into flow-run-instrumentation
collincchoy 0aabba1
Dict over dict for typing.
collincchoy 3ca9a15
Prefer Union over | typing.
collincchoy d510c5c
Move opentelemetry-api requirement to top-level clients reqs.
collincchoy 69615f9
Span status descriptions should only be set when status_code is Error.
collincchoy 917af76
Add test impl for labels propagating into span attrs.
collincchoy b428b14
Merge branch 'main' into flow-run-instrumentation
collincchoy 81e4ca1
Remove duplicated KeyValueLabels type.
collincchoy a8ef13f
Move instrumentation tester out of package into tests.
collincchoy f22903c
Flatten nested context managers.
collincchoy fe87a25
Make span ending DRYer.
collincchoy 233a3ba
Remove old test_utils.py in favor of instrumentation_tester.py.
collincchoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ | ||
|
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,105 @@ | ||
from typing import Any, Dict, Protocol, Tuple, Union | ||
collincchoy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
from opentelemetry import metrics as metrics_api | ||
from opentelemetry import trace as trace_api | ||
from opentelemetry.sdk.metrics import MeterProvider | ||
from opentelemetry.sdk.metrics.export import InMemoryMetricReader | ||
from opentelemetry.sdk.trace import ReadableSpan, Span, TracerProvider, export | ||
from opentelemetry.sdk.trace.export.in_memory_span_exporter import ( | ||
InMemorySpanExporter, | ||
) | ||
from opentelemetry.test.globals_test import ( | ||
reset_metrics_globals, | ||
reset_trace_globals, | ||
) | ||
from opentelemetry.util.types import Attributes | ||
|
||
|
||
def create_tracer_provider(**kwargs) -> Tuple[TracerProvider, InMemorySpanExporter]: | ||
"""Helper to create a configured tracer provider. | ||
|
||
Creates and configures a `TracerProvider` with a | ||
`SimpleSpanProcessor` and a `InMemorySpanExporter`. | ||
All the parameters passed are forwarded to the TracerProvider | ||
constructor. | ||
|
||
Returns: | ||
A list with the tracer provider in the first element and the | ||
in-memory span exporter in the second. | ||
""" | ||
tracer_provider = TracerProvider(**kwargs) | ||
memory_exporter = InMemorySpanExporter() | ||
span_processor = export.SimpleSpanProcessor(memory_exporter) | ||
tracer_provider.add_span_processor(span_processor) | ||
|
||
return tracer_provider, memory_exporter | ||
|
||
|
||
def create_meter_provider(**kwargs) -> Tuple[MeterProvider, InMemoryMetricReader]: | ||
"""Helper to create a configured meter provider | ||
Creates a `MeterProvider` and an `InMemoryMetricReader`. | ||
Returns: | ||
A tuple with the meter provider in the first element and the | ||
in-memory metrics exporter in the second | ||
""" | ||
memory_reader = InMemoryMetricReader() | ||
metric_readers = kwargs.get("metric_readers", []) | ||
metric_readers.append(memory_reader) | ||
kwargs["metric_readers"] = metric_readers | ||
meter_provider = MeterProvider(**kwargs) | ||
return meter_provider, memory_reader | ||
|
||
|
||
class HasAttributesViaProperty(Protocol): | ||
@property | ||
def attributes(self) -> Attributes: | ||
... | ||
|
||
|
||
class HasAttributesViaAttr(Protocol): | ||
attributes: Attributes | ||
|
||
|
||
HasAttributes = Union[HasAttributesViaProperty, HasAttributesViaAttr] | ||
|
||
|
||
class InstrumentationTester: | ||
tracer_provider: TracerProvider | ||
memory_exporter: InMemorySpanExporter | ||
meter_provider: MeterProvider | ||
memory_metrics_reader: InMemoryMetricReader | ||
|
||
def __init__(self): | ||
self.tracer_provider, self.memory_exporter = create_tracer_provider() | ||
# This is done because set_tracer_provider cannot override the | ||
# current tracer provider. | ||
reset_trace_globals() | ||
trace_api.set_tracer_provider(self.tracer_provider) | ||
|
||
self.memory_exporter.clear() | ||
# This is done because set_meter_provider cannot override the | ||
# current meter provider. | ||
reset_metrics_globals() | ||
|
||
self.meter_provider, self.memory_metrics_reader = create_meter_provider() | ||
metrics_api.set_meter_provider(self.meter_provider) | ||
|
||
def reset(self): | ||
reset_trace_globals() | ||
reset_metrics_globals() | ||
|
||
def get_finished_spans(self): | ||
return self.memory_exporter.get_finished_spans() | ||
|
||
@staticmethod | ||
def assert_has_attributes(obj: HasAttributes, attributes: Dict[str, Any]): | ||
assert obj.attributes is not None | ||
for key, val in attributes.items(): | ||
assert key in obj.attributes | ||
assert obj.attributes[key] == val | ||
|
||
@staticmethod | ||
def assert_span_instrumented_for(span: Union[Span, ReadableSpan], module): | ||
assert span.instrumentation_scope is not None | ||
assert span.instrumentation_scope.name == module.__name__ | ||
assert span.instrumentation_scope.version == module.__version__ |
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,10 @@ | ||
import pytest | ||
|
||
from prefect.telemetry.test_utils import InstrumentationTester | ||
|
||
|
||
@pytest.fixture | ||
def instrumentation(): | ||
instrumentation_tester = InstrumentationTester() | ||
yield instrumentation_tester | ||
instrumentation_tester.reset() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
labels are only available via cloud atm so adding them onto client schema here with a default dict to populate for non-cloud server responses so that they're available to be serialized as span attrs