diff --git a/sentry-ruby/lib/sentry-ruby.rb b/sentry-ruby/lib/sentry-ruby.rb index 9efb6e6ec..7a56054ee 100644 --- a/sentry-ruby/lib/sentry-ruby.rb +++ b/sentry-ruby/lib/sentry-ruby.rb @@ -489,6 +489,24 @@ def add_global_event_processor(&block) Scope.add_global_event_processor(&block) end + # Returns the traceparent (sentry-trace) header for distributed tracing. + # Can be either from the currently active span or the propagation context. + # + # @return [String, nil] + def get_traceparent + return nil unless initialized? + get_current_hub.get_traceparent + end + + # Returns the baggage header for distributed tracing. + # Can be either from the currently active span or the propagation context. + # + # @return [String, nil] + def get_baggage + return nil unless initialized? + get_current_hub.get_baggage + end + ##### Helpers ##### # @!visibility private diff --git a/sentry-ruby/lib/sentry/hub.rb b/sentry-ruby/lib/sentry/hub.rb index 2b87b2694..0d9c02dba 100644 --- a/sentry-ruby/lib/sentry/hub.rb +++ b/sentry-ruby/lib/sentry/hub.rb @@ -229,6 +229,20 @@ def with_session_tracking(&block) end_session end + def get_traceparent + return nil unless current_scope + + current_scope.get_span&.to_sentry_trace || + current_scope.propagation_context&.get_traceparent + end + + def get_baggage + return nil unless current_scope + + current_scope.get_span&.to_baggage || + current_scope.propagation_context&.get_baggage&.serialize + end + private def current_layer