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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**Fixes**:

- Crashpad: namespace mpack to avoid ODR violation. ([#1476](https://github.com/getsentry/sentry-native/pull/1476), [crashpad#143](https://github.com/getsentry/crashpad/pull/143))
- Structured logs: stop local attributes overwriting all globally set attributes. They now get merged, and local values overwrite existing global values for the same key. ([#1486](https://github.com/getsentry/sentry-native/pull/1486))

## 0.12.3

Expand Down
21 changes: 21 additions & 0 deletions examples/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,27 @@ main(int argc, char **argv)
return EXIT_FAILURE;
}

if (has_arg(argc, argv, "set-global-attribute")) {
sentry_set_attribute("global.attribute.bool",
sentry_value_new_attribute(sentry_value_new_bool(true), NULL));
sentry_set_attribute("global.attribute.int",
sentry_value_new_attribute(sentry_value_new_int64(123), NULL));
sentry_set_attribute("global.attribute.double",
sentry_value_new_attribute(sentry_value_new_double(1.23), NULL));
sentry_set_attribute("global.attribute.string",
sentry_value_new_attribute(
sentry_value_new_string("my_global_value"), NULL));
sentry_value_t array = sentry_value_new_list();
sentry_value_append(array, sentry_value_new_string("item1"));
sentry_value_append(array, sentry_value_new_string("item2"));
sentry_set_attribute(
"global.attribute.array", sentry_value_new_attribute(array, NULL));
// same key as local attribute; value should be overwritten
sentry_set_attribute("my.custom.attribute",
sentry_value_new_attribute(
sentry_value_new_string("my_global_value"), NULL));
}

if (has_arg(argc, argv, "log-attributes")) {
sentry_value_t attributes = sentry_value_new_object();
sentry_value_t attr = sentry_value_new_attribute(
Expand Down
7 changes: 5 additions & 2 deletions src/sentry_logs.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,13 +686,16 @@ construct_log(sentry_level_t level, const char *message, va_list args)
va_end(args_copy);
if (sentry_value_get_type(custom_attributes)
== SENTRY_VALUE_TYPE_OBJECT) {
// Merge global attributes INTO per-log attributes
// (per-log attributes take precedence for conflicts)
sentry__value_merge_objects(custom_attributes, attributes);
sentry_value_decref(attributes);
attributes = sentry__value_clone(custom_attributes);
attributes = custom_attributes;
Comment thread
JoshuaMoelans marked this conversation as resolved.
} else {
SENTRY_DEBUG("Discarded custom attributes on log: non-object "
"sentry_value_t passed in");
sentry_value_decref(custom_attributes);
}
sentry_value_decref(custom_attributes);
}

// Format the message with remaining args (or all args if not using
Expand Down
65 changes: 65 additions & 0 deletions tests/test_integration_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1734,3 +1734,68 @@ def test_logs_with_custom_attributes(cmake, httpserver):
assert "sentry.sdk.name" in attributes_2
assert attributes_2["sentry.sdk.name"]["value"] == "custom-sdk-name"
assert attributes_2["sentry.sdk.name"]["type"] == "string"


def test_logs_global_and_local_attributes_merge(cmake, httpserver):
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": "none"})

httpserver.expect_oneshot_request(
"/api/123456/envelope/",
headers={"x-sentry-auth": auth_header},
).respond_with_data("OK")
env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver))

run(
tmp_path,
"sentry_example",
["log", "enable-logs", "set-global-attribute", "log-attributes"],
env=env,
)

assert len(httpserver.log) == 1
req = httpserver.log[0][0]
body = req.get_data()

envelope = Envelope.deserialize(body)

# Show what the envelope looks like if the test fails
envelope.print_verbose()

# Extract the log item
(log_item,) = envelope.items

assert log_item.headers["type"] == "log"
payload = log_item.payload.json

# We expect 3 log entries based on the log-attributes example
assert len(payload["items"]) == 3

log_entry_0 = payload["items"][0]
attributes_0 = log_entry_0["attributes"]

# Verify per-log (local) attributes are present

# passed as global and local attribute, but local should overwrite
assert "my.custom.attribute" in attributes_0
assert attributes_0["my.custom.attribute"]["value"] == "my_attribute"
assert attributes_0["my.custom.attribute"]["type"] == "string"

assert "global.attribute.bool" in attributes_0
assert attributes_0["global.attribute.bool"]["value"] == True
assert attributes_0["global.attribute.bool"]["type"] == "boolean"

assert "global.attribute.int" in attributes_0
assert attributes_0["global.attribute.int"]["value"] == 123
assert attributes_0["global.attribute.int"]["type"] == "integer"

assert "global.attribute.double" in attributes_0
assert attributes_0["global.attribute.double"]["value"] == 1.23
assert attributes_0["global.attribute.double"]["type"] == "double"

assert "global.attribute.string" in attributes_0
assert attributes_0["global.attribute.string"]["value"] == "my_global_value"
assert attributes_0["global.attribute.string"]["type"] == "string"

assert "global.attribute.array" in attributes_0
assert attributes_0["global.attribute.array"]["value"] == ["item1", "item2"]
assert attributes_0["global.attribute.array"]["type"] == "string[]"
Loading