Skip to content
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

feat(metrics): Move minimetrics code to the SDK #2385

Merged
merged 36 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
012e00d
feat(metrics): Move minimetrics code to the SDK
mitsuhiko Sep 20, 2023
c0de8c2
fix: typing issues
mitsuhiko Sep 20, 2023
4a9c537
fix: add missing metrics module
mitsuhiko Sep 20, 2023
b0c410d
feat: Reuse timestamp in timing
mitsuhiko Sep 20, 2023
587cf9a
test: Add tests
mitsuhiko Sep 20, 2023
70463ca
feat: Enable gauges and add tests
mitsuhiko Sep 20, 2023
8b494b1
test: Added multi-metric test
mitsuhiko Sep 20, 2023
22705c2
fix: lint
mitsuhiko Sep 20, 2023
feb8c9b
feat: add transaction name to metrics
mitsuhiko Sep 20, 2023
cafc747
fix: duplicate test name
mitsuhiko Sep 20, 2023
48abc5d
fix: typing
mitsuhiko Sep 20, 2023
cd2597a
Add missing space
mitsuhiko Sep 20, 2023
c67ddf0
feat: return envelope to allow capturing of metrics
mitsuhiko Sep 20, 2023
daf0ee1
feat: Unicode support for tag values
mitsuhiko Sep 20, 2023
7bd88b0
Add newline
mitsuhiko Sep 20, 2023
a21e683
feat: Added before_emit_metric
mitsuhiko Sep 20, 2023
b1ba79e
feat: Add timed decorator
mitsuhiko Sep 20, 2023
80cef70
ref: Unify timed and timing
mitsuhiko Sep 20, 2023
54a49ff
fix: Remove unused ts
mitsuhiko Sep 20, 2023
799a436
Fix lints
mitsuhiko Sep 20, 2023
0417e7b
Fix typing error
mitsuhiko Sep 20, 2023
e9d2eee
Make 2.7 happy
mitsuhiko Sep 20, 2023
ed866fc
fix: newstyle type syntax
mitsuhiko Sep 20, 2023
2273176
Fix unicode literal
mitsuhiko Sep 20, 2023
de9049a
Reformat
mitsuhiko Sep 20, 2023
c37ed6f
Mark string as unicode
mitsuhiko Sep 20, 2023
b16bee4
Make black not strip u prefixes on some tests
mitsuhiko Sep 20, 2023
be7756b
Add back removed utf-8 coding marker
mitsuhiko Sep 20, 2023
1f18562
feat: Allow non integer tag values
mitsuhiko Sep 21, 2023
79379a4
Fix python 2.7 again
mitsuhiko Sep 21, 2023
c0bb251
Added comment on sanitizing
mitsuhiko Sep 21, 2023
fe71bc6
Split out timing and distribution
mitsuhiko Sep 21, 2023
16f20a8
fix: lint
mitsuhiko Sep 21, 2023
8d8e015
black
mitsuhiko Sep 21, 2023
7cf653b
Make nanosecond_time always work
mitsuhiko Sep 21, 2023
079fb34
raise -> return
mitsuhiko Sep 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions sentry_sdk/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from typing import Any
from typing import Callable
from typing import Dict
from typing import List
from typing import Mapping
from typing import Optional
from typing import Tuple
from typing import Type
Expand Down Expand Up @@ -51,6 +53,7 @@
"session",
"internal",
"profile",
"statsd",
]
SessionStatus = Literal["ok", "exited", "crashed", "abnormal"]
EndpointType = Literal["store", "envelope"]
Expand Down Expand Up @@ -87,3 +90,56 @@
MeasurementUnit = Union[DurationUnit, InformationUnit, FractionUnit, str]

ProfilerMode = Literal["sleep", "thread", "gevent", "unknown"]

# Unit of the metrics.
MetricUnit = Literal[
"none",
"nanosecond",
"microsecond",
"millisecond",
"second",
"minute",
"hour",
"day",
"week",
"bit",
"byte",
"kilobyte",
"kibibyte",
"mebibyte",
"gigabyte",
"terabyte",
"tebibyte",
"petabyte",
"pebibyte",
"exabyte",
"exbibyte",
"ratio",
"percent",
]

# Type of the metric.
MetricType = Literal["d", "s", "g", "c"]

# Value of the metric.
MetricValue = Union[int, float, str]

# Internal representation of tags as a tuple of tuples (this is done in order to allow for the same key to exist
# multiple times).
MetricTagsInternal = Tuple[Tuple[str, str], ...]

# External representation of tags as a dictionary.
MetricTagValue = Union[
str,
int,
float,
mitsuhiko marked this conversation as resolved.
Show resolved Hide resolved
None,
List[Union[int, str, float, None]],
Tuple[Union[int, str, float, None], ...],
]
MetricTags = Mapping[str, MetricTagValue]

# Value inside the generator for the metric value.
FlushedMetricValue = Union[int, float]

BucketKey = Tuple[MetricType, str, MetricUnit, MetricTagsInternal]
12 changes: 12 additions & 0 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@

self.session_flusher = SessionFlusher(capture_func=_capture_envelope)

self.metrics_aggregator = None # type: Optional[MetricsAggregator]
if self.options.get("_experiments", {}).get("enable_metrics"):
from sentry_sdk.metrics import MetricsAggregator

Check warning on line 234 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L234

Added line #L234 was not covered by tests

self.metrics_aggregator = MetricsAggregator(

Check warning on line 236 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L236

Added line #L236 was not covered by tests
capture_func=_capture_envelope
)

max_request_body_size = ("always", "never", "small", "medium")
if self.options["max_request_body_size"] not in max_request_body_size:
raise ValueError(
Expand Down Expand Up @@ -610,6 +618,8 @@
if self.transport is not None:
self.flush(timeout=timeout, callback=callback)
self.session_flusher.kill()
if self.metrics_aggregator is not None:
self.metrics_aggregator.kill()

Check warning on line 622 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L622

Added line #L622 was not covered by tests
if self.monitor:
self.monitor.kill()
self.transport.kill()
Expand All @@ -632,6 +642,8 @@
if timeout is None:
timeout = self.options["shutdown_timeout"]
self.session_flusher.flush()
if self.metrics_aggregator is not None:
self.metrics_aggregator.flush()

Check warning on line 646 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L646

Added line #L646 was not covered by tests
self.transport.flush(timeout=timeout, callback=callback)

def __enter__(self):
Expand Down
3 changes: 3 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ProfilerMode,
TracesSampler,
TransactionProcessor,
MetricTags,
)

# Experiments are feature flags to enable and disable certain unstable SDK
Expand All @@ -41,6 +42,8 @@
"profiler_mode": Optional[ProfilerMode],
"otel_powered_performance": Optional[bool],
"transport_zlib_compression_level": Optional[int],
"enable_metrics": Optional[bool],
"before_emit_metric": Optional[Callable[[str, MetricTags], bool]],
},
total=False,
)
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@
return "internal"
elif ty == "profile":
return "profile"
elif ty == "statsd":
return "statsd"

Check warning on line 264 in sentry_sdk/envelope.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/envelope.py#L264

Added line #L264 was not covered by tests
else:
return "default"

Expand Down
Loading
Loading