Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,13 @@ def _capture_metric(self, metric):
metric["attributes"]["sentry.sdk.name"] = SDK_INFO["name"]
metric["attributes"]["sentry.sdk.version"] = SDK_INFO["version"]

server_name = self.options.get("server_name")
if (
server_name is not None
and SPANDATA.SERVER_ADDRESS not in metric["attributes"]
):
metric["attributes"][SPANDATA.SERVER_ADDRESS] = server_name

environment = self.options.get("environment")
if environment is not None and "sentry.environment" not in metric["attributes"]:
metric["attributes"]["sentry.environment"] = environment
Expand Down
7 changes: 6 additions & 1 deletion tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sentry_sdk import get_client
from sentry_sdk.envelope import Envelope
from sentry_sdk.types import Metric
from sentry_sdk.consts import SPANDATA, VERSION


def envelopes_to_metrics(envelopes):
Expand Down Expand Up @@ -93,7 +94,7 @@ def test_metrics_experimental_option(sentry_init, capture_envelopes):


def test_metrics_with_attributes(sentry_init, capture_envelopes):
sentry_init(release="1.0.0", environment="test")
sentry_init(release="1.0.0", environment="test", server_name="test-server")
envelopes = capture_envelopes()

sentry_sdk.metrics.count(
Expand All @@ -110,6 +111,10 @@ def test_metrics_with_attributes(sentry_init, capture_envelopes):
assert metrics[0]["attributes"]["sentry.release"] == "1.0.0"
assert metrics[0]["attributes"]["sentry.environment"] == "test"

assert metrics[0]["attributes"][SPANDATA.SERVER_ADDRESS] == "test-server"
assert metrics[0]["attributes"]["sentry.sdk.name"].startswith("sentry.python")
assert metrics[0]["attributes"]["sentry.sdk.version"] == VERSION


def test_metrics_with_user(sentry_init, capture_envelopes):
sentry_init()
Expand Down