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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Add attachment support to user feedback. ([#1414](https://github.com/getsentry/sentry-native/pull/1414))

**Fixes**:

- Structured logs: avoid modifying custom per-log attributes when merging with scope attributes. ([#1500](https://github.com/getsentry/sentry-native/pull/1500))

## 0.12.4

**Fixes**:
Expand Down
14 changes: 5 additions & 9 deletions src/sentry_logs.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,7 @@ static sentry_value_t
construct_log(sentry_level_t level, const char *message, va_list args)
{
sentry_value_t log = sentry_value_new_object();
sentry_value_t attributes = sentry_value_new_null();
SENTRY_WITH_SCOPE (scope) {
attributes = sentry__value_clone(scope->attributes);
}
sentry_value_t attributes = sentry_value_new_object();

SENTRY_WITH_OPTIONS (options) {
// Extract custom attributes if the option is enabled
Expand All @@ -258,16 +255,15 @@ 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);
// Clone custom attributes first (per-log attributes take
// precedence for conflicts)
sentry_value_decref(attributes);
attributes = custom_attributes;
attributes = sentry__value_clone(custom_attributes);
} 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
2 changes: 2 additions & 0 deletions src/sentry_scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,8 @@ void
sentry__scope_apply_attributes(const sentry_scope_t *scope,
sentry_value_t telemetry, sentry_value_t attributes)
{
sentry__value_merge_objects(attributes, scope->attributes);

sentry_value_t trace_id = sentry_value_get_by_key(
sentry_value_get_by_key(scope->propagation_context, "trace"),
"trace_id");
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/test_logs.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,34 @@ SENTRY_TEST(logs_custom_attributes_with_format_strings)
TEST_CHECK(!validation_data.has_validation_error);
TEST_CHECK_INT_EQUAL(validation_data.called_count, 1);
}

SENTRY_TEST(logs_custom_attributes_not_modified)
{
SENTRY_TEST_OPTIONS_NEW(options);
sentry_options_set_dsn(options, "https://foo@sentry.invalid/42");
sentry_options_set_enable_logs(options, true);
sentry_options_set_logs_with_attributes(options, true);
sentry_init(options);

sentry_set_attribute("global.key",
sentry_value_new_attribute(
sentry_value_new_string("global_value"), NULL));

sentry_value_t attrs = sentry_value_new_object();
sentry_value_set_by_key(attrs, "local.key",
sentry_value_new_attribute(
sentry_value_new_string("local_value"), NULL));
sentry_value_incref(attrs);

sentry_log_info("Test message", attrs);

// attrs should still contain only local.key, not global.key
TEST_CHECK_INT_EQUAL(sentry_value_get_length(attrs), 1);
TEST_CHECK(
!sentry_value_is_null(sentry_value_get_by_key(attrs, "local.key")));
TEST_CHECK(
sentry_value_is_null(sentry_value_get_by_key(attrs, "global.key")));

sentry_value_decref(attrs);
sentry_close();
}
1 change: 1 addition & 0 deletions tests/unit/tests.inc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ XX(iso_time)
XX(lazy_attachments)
XX(logger_enable_disable_functionality)
XX(logger_level)
XX(logs_custom_attributes_not_modified)
XX(logs_custom_attributes_with_format_strings)
XX(logs_disabled_by_default)
XX(logs_force_flush)
Expand Down
Loading