-
-
Notifications
You must be signed in to change notification settings - Fork 516
Introduce Sentry.capture_log #2606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e045cc6
Introduce LogEvent
solnic 855048d
Support for LogEvent in Transport
solnic 3cbc33a
Introduce `log` data category in events
solnic cf1296b
Introduce Sentry.capture_log
solnic be910c4
Update CHANGELOG
solnic 4ac7e75
Rework LogEvent serialization
solnic 43de0dc
Add more info to serialized log events
solnic a601ba0
Use each_with_object for consistency
solnic dcf75e7
Grab trace_id from contexts[:trace]
solnic dd2d44b
Add parent_span_id
solnic 97543a0
Add parent_span_id and nest sentry info under attributes
solnic 167c5ef
Set SDK info under attributes correctly
solnic b059014
rubocop
solnic 6d7777c
server_name -> address
solnic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sentry | ||
# Event type that represents a log entry with its attributes | ||
# | ||
# @see https://develop.sentry.dev/sdk/telemetry/logs/#log-envelope-item-payload | ||
class LogEvent < Event | ||
TYPE = "log" | ||
|
||
SERIALIZEABLE_ATTRIBUTES = %i[ | ||
level | ||
body | ||
timestamp | ||
trace_id | ||
attributes | ||
] | ||
|
||
SENTRY_ATTRIBUTES = { | ||
"sentry.trace.parent_span_id" => :parent_span_id, | ||
"sentry.environment" => :environment, | ||
"sentry.release" => :release, | ||
"sentry.address" => :server_name, | ||
"sentry.sdk.name" => :sdk_name, | ||
"sentry.sdk.version" => :sdk_version | ||
} | ||
|
||
LEVELS = %i[trace debug info warn error fatal].freeze | ||
|
||
attr_accessor :level, :body, :attributes, :trace_id | ||
|
||
def initialize(configuration: Sentry.configuration, **options) | ||
super(configuration: configuration) | ||
@type = TYPE | ||
@level = options.fetch(:level) | ||
@body = options[:body] | ||
@attributes = options[:attributes] || {} | ||
@contexts = {} | ||
end | ||
|
||
def to_hash | ||
SERIALIZEABLE_ATTRIBUTES.each_with_object({}) do |name, memo| | ||
memo[name] = serialize(name) | ||
end | ||
end | ||
|
||
private | ||
|
||
def serialize(name) | ||
serializer = :"serialize_#{name}" | ||
|
||
if respond_to?(serializer, true) | ||
__send__(serializer) | ||
else | ||
public_send(name) | ||
end | ||
end | ||
|
||
def serialize_level | ||
level.to_s | ||
end | ||
|
||
def serialize_sdk_name | ||
Sentry.sdk_meta["name"] | ||
end | ||
|
||
def serialize_sdk_version | ||
Sentry.sdk_meta["version"] | ||
end | ||
|
||
def serialize_timestamp | ||
Time.parse(timestamp).to_f | ||
end | ||
|
||
def serialize_trace_id | ||
@contexts.dig(:trace, :trace_id) | ||
end | ||
|
||
def serialize_parent_span_id | ||
@contexts.dig(:trace, :parent_span_id) | ||
end | ||
|
||
def serialize_attributes | ||
hash = @attributes.each_with_object({}) do |(key, value), memo| | ||
memo[key] = attribute_hash(value) | ||
end | ||
|
||
SENTRY_ATTRIBUTES.each do |key, name| | ||
if (value = serialize(name)) | ||
hash[key] = attribute_hash(value) | ||
end | ||
end | ||
|
||
hash | ||
end | ||
|
||
def attribute_hash(value) | ||
{ value: value, type: value_type(value) } | ||
end | ||
|
||
def value_type(value) | ||
case value | ||
when Integer | ||
"integer" | ||
when TrueClass, FalseClass | ||
"boolean" | ||
when Float | ||
"double" | ||
else | ||
"string" | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe Sentry::LogEvent do | ||
let(:configuration) do | ||
Sentry::Configuration.new.tap do |config| | ||
config.dsn = Sentry::TestHelper::DUMMY_DSN | ||
end | ||
end | ||
|
||
describe "#initialize" do | ||
it "initializes with required attributes" do | ||
event = described_class.new( | ||
configuration: configuration, | ||
level: :info, | ||
body: "User John has logged in!" | ||
) | ||
|
||
expect(event).to be_a(described_class) | ||
expect(event.level).to eq(:info) | ||
expect(event.body).to eq("User John has logged in!") | ||
end | ||
|
||
it "accepts attributes" do | ||
attributes = { | ||
"sentry.message.template" => "User %s has logged in!", | ||
"sentry.message.parameters.0" => "John" | ||
} | ||
|
||
event = described_class.new( | ||
configuration: configuration, | ||
level: :info, | ||
body: "User John has logged in!", | ||
attributes: attributes | ||
) | ||
|
||
expect(event.attributes).to eq(attributes) | ||
end | ||
end | ||
|
||
describe "#to_hash" do | ||
before do | ||
configuration.release = "1.2.3" | ||
configuration.environment = "test" | ||
configuration.server_name = "server-123" | ||
end | ||
|
||
it "includes all required fields" do | ||
attributes = { | ||
"sentry.message.template" => "User %s has logged in!", | ||
"sentry.message.parameters.0" => "John" | ||
} | ||
|
||
event = described_class.new( | ||
configuration: configuration, | ||
level: :info, | ||
body: "User John has logged in!", | ||
attributes: attributes | ||
) | ||
|
||
hash = event.to_hash | ||
|
||
expect(hash[:level]).to eq("info") | ||
expect(hash[:body]).to eq("User John has logged in!") | ||
expect(hash[:timestamp]).to be_a(Float) | ||
|
||
attributes = hash[:attributes] | ||
|
||
expect(attributes).to be_a(Hash) | ||
expect(attributes["sentry.message.template"]).to eq({ value: "User %s has logged in!", type: "string" }) | ||
expect(attributes["sentry.message.parameters.0"]).to eq({ value: "John", type: "string" }) | ||
expect(attributes["sentry.environment"]).to eq({ value: "test", type: "string" }) | ||
expect(attributes["sentry.release"]).to eq({ value: "1.2.3", type: "string" }) | ||
expect(attributes["sentry.address"]).to eq({ value: "server-123", type: "string" }) | ||
expect(attributes["sentry.sdk.name"]).to eq({ value: "sentry.ruby", type: "string" }) | ||
expect(attributes["sentry.sdk.version"]).to eq({ value: Sentry::VERSION, type: "string" }) | ||
end | ||
|
||
it "serializes different attribute types correctly" do | ||
attributes = { | ||
"string_attr" => "string value", | ||
"integer_attr" => 42, | ||
"boolean_attr" => true, | ||
"float_attr" => 3.14 | ||
} | ||
|
||
event = described_class.new( | ||
configuration: configuration, | ||
level: :info, | ||
body: "Test message", | ||
attributes: attributes | ||
) | ||
|
||
hash = event.to_hash | ||
|
||
expect(hash[:attributes]["string_attr"]).to eq({ value: "string value", type: "string" }) | ||
expect(hash[:attributes]["integer_attr"]).to eq({ value: 42, type: "integer" }) | ||
expect(hash[:attributes]["boolean_attr"]).to eq({ value: true, type: "boolean" }) | ||
expect(hash[:attributes]["float_attr"]).to eq({ value: 3.14, type: "double" }) | ||
end | ||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.