Skip to content

Commit

Permalink
feat: Customize SentryEvent tags (#72)
Browse files Browse the repository at this point in the history
Resolves GH-69
  • Loading branch information
limbonaut authored Jan 30, 2025
1 parent 07830ee commit 57019ff
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Configure the SDK via GDScript and filter events using event hooks `before_send` and `on_crash`. The new `SentryConfiguration` class can be extended in a script and assigned in options to configure the SDK during initialization. However, due to the way scripting is initialized in the Godot Engine, this comes with a trade-off: a slightly later initialization. If a configuration script is assigned, initialization must be delayed until ScriptServer is ready to load and run the user script. ([#60](https://github.com/getsentry/sentry-godot/pull/60))
- New `dist` property in `SentryOptions` ([#74](https://github.com/getsentry/sentry-godot/pull/74))
- Click to copy UUIDs in the demo project ([#78](https://github.com/getsentry/sentry-godot/pull/78))
- Customize `SentryEvent` tags ([#72](https://github.com/getsentry/sentry-godot/pull/72))

### Dependencies

Expand Down
10 changes: 10 additions & 0 deletions project/test/test_event.gd
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ func test_event_environment() -> void:
assert_str(event.environment).is_equal("custom-environment")


## SentryEvent.set_tag() should set tag to the specified value, remove_tag() should unset it.
func test_event_tags() -> void:
var event := SentrySDK.create_event()
assert_str(event.get_tag("test_tag")).is_empty()
event.set_tag("test_tag", "test_value")
assert_str(event.get_tag("test_tag")).is_equal("test_value")
event.remove_tag("test_tag")
assert_str(event.get_tag("test_tag")).is_empty()


## SentrySDK.capture_event() should return a non-empty event ID, which must match the ID returned by the get_last_event_id() call.
func test_capture_event() -> void:
var event := SentrySDK.create_event()
Expand Down
2 changes: 2 additions & 0 deletions project/test/test_event_integrity.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func test_event_integrity(timeout := 10000) -> void:
event.release = "custom-release"
event.dist = "custom-dist"
event.environment = "custom-environment"
event.set_tag("custom-tag", "custom-tag-value")
created_id = event.id

var captured_id := SentrySDK.capture_event(event)
Expand All @@ -42,6 +43,7 @@ func _before_send(event: SentryEvent) -> SentryEvent:
assert_str(event.release).is_equal("custom-release")
assert_str(event.dist).is_equal("custom-dist")
assert_str(event.environment).is_equal("custom-environment")
assert_str(event.get_tag("custom-tag")).is_equal("custom-tag-value")
assert_str(event.id).is_equal(created_id)
callback_processed.emit()
return null # discard event
4 changes: 4 additions & 0 deletions src/sentry/disabled_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class DisabledEvent : public SentryEvent {

virtual void set_environment(const String &p_environment) override { environment = p_environment; }
virtual String get_environment() const override { return environment; }

virtual void set_tag(const String &p_key, const String &p_value) override {}
virtual void remove_tag(const String &p_key) override {}
virtual String get_tag(const String &p_key) override { return ""; }
};

#endif // DISABLED_EVENT_H
29 changes: 29 additions & 0 deletions src/sentry/native/native_event.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "native_event.h"

#include "godot_cpp/core/error_macros.hpp"
#include "sentry/level.h"
#include "sentry/native/native_util.h"

Expand Down Expand Up @@ -110,6 +111,34 @@ String NativeEvent::get_environment() const {
return sentry_value_as_string(environment);
}

void NativeEvent::set_tag(const String &p_key, const String &p_value) {
ERR_FAIL_COND_MSG(p_key.is_empty(), "Sentry: Can't set tag with an empty key.");
sentry_value_t tags = sentry_value_get_by_key(native_event, "tags");
if (sentry_value_is_null(tags)) {
tags = sentry_value_new_object();
sentry_value_set_by_key(native_event, "tags", tags);
}
sentry_value_set_by_key(tags, p_key.utf8(), sentry_value_new_string(p_value.utf8()));
}

void NativeEvent::remove_tag(const String &p_key) {
ERR_FAIL_COND_MSG(p_key.is_empty(), "Sentry: Can't remove tag with an empty key.");
sentry_value_t tags = sentry_value_get_by_key(native_event, "tags");
if (!sentry_value_is_null(tags)) {
sentry_value_remove_by_key(tags, p_key.utf8());
}
}

String NativeEvent::get_tag(const String &p_key) {
ERR_FAIL_COND_V_MSG(p_key.is_empty(), String(), "Sentry: Can't get tag with an empty key.");
sentry_value_t tags = sentry_value_get_by_key(native_event, "tags");
if (!sentry_value_is_null(tags)) {
sentry_value_t value = sentry_value_get_by_key(tags, p_key.utf8());
return String(sentry_value_as_string(value));
}
return String();
}

NativeEvent::NativeEvent(sentry_value_t p_native_event) {
if (sentry_value_refcount(p_native_event) > 0) {
sentry_value_incref(p_native_event); // acquire ownership
Expand Down
4 changes: 4 additions & 0 deletions src/sentry/native/native_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class NativeEvent : public SentryEvent {
virtual void set_environment(const String &p_environment) override;
virtual String get_environment() const override;

virtual void set_tag(const String &p_key, const String &p_value) override;
virtual void remove_tag(const String &p_key) override;
virtual String get_tag(const String &p_key) override;

NativeEvent(sentry_value_t p_event);
NativeEvent();
virtual ~NativeEvent() override;
Expand Down
3 changes: 3 additions & 0 deletions src/sentry_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ void SentryEvent::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_dist"), &SentryEvent::get_dist);
ClassDB::bind_method(D_METHOD("set_environment", "environment"), &SentryEvent::set_environment);
ClassDB::bind_method(D_METHOD("get_environment"), &SentryEvent::get_environment);
ClassDB::bind_method(D_METHOD("set_tag", "key", "value"), &SentryEvent::set_tag);
ClassDB::bind_method(D_METHOD("remove_tag", "key"), &SentryEvent::remove_tag);
ClassDB::bind_method(D_METHOD("get_tag", "key"), &SentryEvent::get_tag);

ADD_PROPERTY(PropertyInfo(Variant::STRING, "id"), "", "get_id");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "message"), "set_message", "get_message");
Expand Down
4 changes: 4 additions & 0 deletions src/sentry_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class SentryEvent : public RefCounted {
virtual void set_environment(const String &p_environment) = 0;
virtual String get_environment() const = 0;

virtual void set_tag(const String &p_key, const String &p_value) = 0;
virtual void remove_tag(const String &p_key) = 0;
virtual String get_tag(const String &p_key) = 0;

virtual ~SentryEvent() = default;
};

Expand Down

0 comments on commit 57019ff

Please sign in to comment.