-
-
Notifications
You must be signed in to change notification settings - Fork 521
Add new Sentry.logger #2620
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
Add new Sentry.logger #2620
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
6d00939
Add StructuredLogger and re-purpose Sentry.logger
solnic 570b449
YARD docs for structured logger
solnic 4d808cc
Remove obsolete comment
solnic 8ce961e
Add support for message templates
solnic b8ef85e
Support Hash-based log templates too
solnic 3aba1f5
Remove redundant assignment
solnic 4d83fac
Ensure we don't set message.template when body is not a template
solnic 5645fb4
Lazy-load parameters
solnic 4d5571e
Reduce object allocations
solnic 4c7c762
Use attr readers consistently
solnic 02da067
Remove unused reader
solnic 8bc6add
Update docs
solnic 9658adf
Fix issues with parameters handling and extra attributes
solnic 633a135
Fix spec
solnic d074fac
Auto-flush log events when adding and fix flush
solnic bdf604d
Add enabled_logs toplevel config
solnic 13863bc
Rework logger init to work with enabled_logs config
solnic d879a8b
Update CHANGELOG
solnic 7a96c56
Remove unused Device logging class
solnic c41de52
More examples in CHANGELOG
solnic 8514a25
Fix parameter naming in log event attributes
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,10 @@ module Sentry | |
| class LogEvent < Event | ||
| TYPE = "log" | ||
|
|
||
| DEFAULT_PARAMETERS = [].freeze | ||
| DEFAULT_ATTRIBUTES = {}.freeze | ||
| DEFAULT_CONTEXT = {}.freeze | ||
|
|
||
| SERIALIZEABLE_ATTRIBUTES = %i[ | ||
| level | ||
| body | ||
|
|
@@ -21,20 +25,23 @@ class LogEvent < Event | |
| "sentry.release" => :release, | ||
| "sentry.address" => :server_name, | ||
| "sentry.sdk.name" => :sdk_name, | ||
| "sentry.sdk.version" => :sdk_version | ||
| "sentry.sdk.version" => :sdk_version, | ||
| "sentry.message.template" => :template | ||
| } | ||
|
|
||
| LEVELS = %i[trace debug info warn error fatal].freeze | ||
|
|
||
| attr_accessor :level, :body, :attributes, :trace_id | ||
| attr_accessor :level, :body, :template, :attributes | ||
|
|
||
| def initialize(configuration: Sentry.configuration, **options) | ||
| super(configuration: configuration) | ||
|
|
||
| @type = TYPE | ||
| @level = options.fetch(:level) | ||
| @body = options[:body] | ||
| @attributes = options[:attributes] || {} | ||
| @contexts = {} | ||
| @template = @body if is_template? | ||
| @attributes = options[:attributes] || DEFAULT_ATTRIBUTES | ||
| @contexts = DEFAULT_CONTEXT | ||
| end | ||
|
|
||
| def to_hash | ||
|
|
@@ -72,15 +79,25 @@ def serialize_timestamp | |
| end | ||
|
|
||
| def serialize_trace_id | ||
| @contexts.dig(:trace, :trace_id) | ||
| contexts.dig(:trace, :trace_id) | ||
| end | ||
|
|
||
| def serialize_parent_span_id | ||
| @contexts.dig(:trace, :parent_span_id) | ||
| contexts.dig(:trace, :parent_span_id) | ||
| end | ||
|
|
||
| def serialize_body | ||
| if parameters.empty? | ||
| body | ||
| elsif parameters.is_a?(Hash) | ||
| body % parameters | ||
| else | ||
| sprintf(body, *parameters) | ||
| end | ||
| end | ||
|
|
||
| def serialize_attributes | ||
| hash = @attributes.each_with_object({}) do |(key, value), memo| | ||
| hash = attributes.each_with_object({}) do |(key, value), memo| | ||
| memo[key] = attribute_hash(value) | ||
| end | ||
|
|
||
|
|
@@ -109,5 +126,34 @@ def value_type(value) | |
| "string" | ||
| end | ||
| end | ||
|
|
||
| def parameters | ||
| @parameters ||= begin | ||
| return DEFAULT_PARAMETERS unless template | ||
|
|
||
| parameters = template_tokens.empty? ? | ||
| attributes.fetch(:parameters, DEFAULT_PARAMETERS) : attributes.slice(*template_tokens) | ||
|
|
||
| if parameters.is_a?(Hash) | ||
| parameters.each do |key, value| | ||
| attributes["sentry.message.parameter.#{key}"] = value | ||
| end | ||
| else | ||
| parameters.each_with_index do |param, index| | ||
| attributes["sentry.message.parameter.#{index}"] = param | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| TOKEN_REGEXP = /%\{(\w+)\}/ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should have a clear note in docs that our templates are expected to be like "This is a template with a %{var}"and not what ruby people are used to "This is a template with a #{var}" |
||
|
|
||
| def template_tokens | ||
| @template_tokens ||= body.scan(TOKEN_REGEXP).flatten.map(&:to_sym) | ||
| end | ||
|
|
||
| def is_template? | ||
| body.include?("%s") || TOKEN_REGEXP.match?(body) | ||
| 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
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.