-
-
Notifications
You must be signed in to change notification settings - Fork 220
feat(logs): initial API for Sentry Logs #4158
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 2 commits
Commits
Show all changes
90 commits
Select commit
Hold shift + click to select a range
0add668
feat(logs): initial experiment
Flash0ver db7d558
feat(logs): basic Logger Module API shape
Flash0ver d96b092
style(logs): consolidate
Flash0ver 2958a47
ref(logs): remove generic WriteSerializable overload
Flash0ver a63371b
ref(logs): consolidate experimental Diagnostic-ID
Flash0ver d8d2567
feat(logs): add experimental options
Flash0ver fefd24c
Merge branch 'main' into feat/logs-initial-api
Flash0ver 165996a
ref(logs): remove custom polyfills now provided through Polyfill
Flash0ver 32e7e25
Format code
getsentry-bot fc88722
Merge branch 'feat/logs-initial-api' of https://github.com/getsentry/…
Flash0ver 2ba87e4
ref(logs): move types out of Experimental namespace
Flash0ver 0f1d4a4
feat(logs): change 'integer' from Int32 to Int64
Flash0ver 8dec5d5
ref(logs): refine API surface area
Flash0ver a664f7e
ref(logs): match SeverityLevel to OTel spec
Flash0ver 96693d0
ref(logs): rename SentrySeverity to LogSeverityLevel
Flash0ver 83964cf
Format code
getsentry-bot dadc69b
ref(logs): hide underlying Dictionary`2 for Attributes
Flash0ver c91cdde
ref(logs): restructure attributes
Flash0ver 0740c3b
ref(logs): extract TraceId and ParentSpanId methods
Flash0ver eee06bf
ref(logs): remove `SentryOptions.LogsSampleRate`
Flash0ver 8c61d8b
feat(logs): support ISystemClock abstraction
Flash0ver 80683ae
ref(logs): disambiguate SentryLogger names
Flash0ver cb20118
ref(logs): consolidate names of Log-Methods
Flash0ver 2cb306f
ref(logs): rename LogSeverityLevel to SentryLogLevel
Flash0ver dcc0ec1
ref(logs): re-rename new logger type
Flash0ver 58dce74
ref(logs): move Logger instances to Hubs
Flash0ver f2e1ba2
test(logs): add tests
Flash0ver 0220015
Merge branch 'main' into feat/logs-initial-api
Flash0ver 6822b23
Format code
getsentry-bot dd39fae
fix(logs): incorrectly serializing attributes
Flash0ver 6eb5b9b
fix(logs): do not capture log when template/parameters are invalid
Flash0ver 69c05b8
fix(logs): do not capture log on user callback exceptions
Flash0ver 430cf82
ref(logs): move new public types to root namespace
Flash0ver 31a8f1f
ref(logs): rework sample
Flash0ver 2ae4476
ref(logs): ensure that DisabledHub dues not capture logs
Flash0ver 69678ce
ref(logs): allow out-of-range Log-Level
Flash0ver 97995a8
docs(logs): add XML comments indicating that logs will be ignored on …
Flash0ver fbe747d
docs(logs): add to changelog
Flash0ver 64adf33
fix(logs): add to Bindable-Options
Flash0ver cdfa901
fix(logs): add to ApiApprovalTests
Flash0ver d2ac53b
test(logs): add missing net48 ApiApproval
Flash0ver 4011ba6
test(logs): fix line endings on Windows
Flash0ver 4ae82d0
Update src/Sentry/Protocol/Envelopes/Envelope.cs
Flash0ver bc1c465
Update SentryLog.cs
Flash0ver 79fb190
test(logs): fix floating-point ToString expectation for .NET Framework
Flash0ver b4e80f4
ref(logs): remove some using declarations
Flash0ver b21adef
test(ci): trying to work around floating-point formatter on .NET Fram…
Flash0ver 0032858
test(logs): skip failing tests on Mono (non-Windows)
Flash0ver a9769f8
test(log): fix Skip.If missing SkippableFact
Flash0ver 9a51033
try: fix floating-point formatting on Windows
Flash0ver 9a09832
Merge branch 'main' into feat/logs-initial-api
Flash0ver f133118
Merge branch 'main' into feat/logs-initial-api
Flash0ver 72c9a93
ref: make SentryStructuredLogger abstract
Flash0ver c97f4ad
docs: add comment to sample usage of SetBeforeSendLog
Flash0ver a9eea90
ref: clarify intent of usages of Debug.Assert
Flash0ver 8bd0ed2
docs: improve XML comments
Flash0ver 51892de
test: range of Severity-Number specification
Flash0ver acc8995
test: GetValuesAsUnderlyingType of the new enum
Flash0ver 6bd4c96
ref: move Log options to Experimental section
Flash0ver f673d1e
ref: move Logger to Experimental section of SDK
Flash0ver daafd7f
test: add Hub tests
Flash0ver 62ee5d5
Merge branch 'feat/logs' into feat/logs-initial-api
Flash0ver 6a54203
test: update verified public API
Flash0ver 479cab8
docs: update CHANGELOG.md
Flash0ver 97a87f8
Merge branch 'main' into feat/logs-initial-api
jamescrosswell 0467449
Merge branch 'feat/logs' into feat/logs-initial-api
Flash0ver 54062d2
ref: reuse Disabled-Instance when Structured-Logging is not enabled
Flash0ver 7107bce
ref: remove Enabled-checks on Default-Logger
Flash0ver c0a1cd5
ref: rename DisabledSentryStructuredLogger to NoOpSentryStructuredLogger
Flash0ver 45b8687
ref: make `BindableSentryOptions.Experimental` internal
Flash0ver 3192534
Revert "ref: make `BindableSentryOptions.Experimental` internal"
Flash0ver b8bcea6
docs: Update CHANGELOG.md
Flash0ver d4c82a2
Revert "ref: rename DisabledSentryStructuredLogger to NoOpSentryStruc…
Flash0ver 9193a96
ref: replace use of ScopeManager with TraceHeader
Flash0ver 7cb4043
test: remove Skip as we no longer test net48 against non-Windows
Flash0ver e3ca5b5
feat: support more numeric types
Flash0ver afb135e
feat: support char attributes
Flash0ver 7f675aa
fix: build error targeting .NET Standard 2.0 and .NET Framework
Flash0ver 750a388
fix: exception when passing null as message parameter
Flash0ver 9f62d3b
test: add Attributes-To-Json test
Flash0ver 5b00c21
fix: missing type on .NET Framework
Flash0ver 6d17918
feat: support Attribute-Types from spec
Flash0ver 7e2c57b
ref: clarify internal identifiers
Flash0ver 7115c42
test: update approved API
Flash0ver cd5246b
test: fix incorrect expectation
Flash0ver baf5569
Merge branch 'feat/logs' into feat/logs-initial-api
Flash0ver 6e13e95
feat: use "wrapping" SDK's Name and Version
Flash0ver 0a9a3b1
fix: Get-Attribute-API
Flash0ver 934fb36
Merge branch 'feat/logs' into feat/logs-initial-api
Flash0ver 2c1608e
docs: update CHANGELOG
Flash0ver 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
using Sentry.Extensibility; | ||
using Sentry.Infrastructure; | ||
using Sentry.Internal.Extensions; | ||
|
||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
|
||
namespace Sentry.Experimental; | ||
|
||
[Experimental(DiagnosticId.ExperimentalSentryLogs)] | ||
public sealed class SentryLog : ISentryJsonSerializable | ||
{ | ||
private Dictionary<string, ValueTypePair>? _attributes; | ||
private int _severityNumber = -1; | ||
|
||
[SetsRequiredMembers] | ||
internal SentryLog(SentrySeverity level, string message, object[]? parameters = null) | ||
{ | ||
Timestamp = DateTimeOffset.UtcNow; | ||
TraceId = SentryId.Empty; | ||
Level = level; | ||
Message = message; | ||
Parameters = parameters; | ||
} | ||
|
||
public required DateTimeOffset Timestamp { get; init; } | ||
|
||
public required SentryId TraceId { get; init; } | ||
|
||
public SentrySeverity Level | ||
{ | ||
get => SentrySeverityExtensions.FromSeverityNumber(_severityNumber); | ||
set => _severityNumber = SentrySeverityExtensions.ToSeverityNumber(value); | ||
} | ||
|
||
public required string Message { get; init; } | ||
|
||
//public Dictionary<string, object>? Attributes { get { return _attributes; } } | ||
|
||
public string? Template { get; init; } | ||
|
||
public object[]? Parameters { get; init; } | ||
|
||
public required int SeverityNumber | ||
{ | ||
get => _severityNumber; | ||
set | ||
{ | ||
// | ||
SentrySeverityExtensions.ThrowIfOutOfRange(value); | ||
_severityNumber = value; | ||
} | ||
} | ||
|
||
public void SetAttribute(string key, string value) | ||
{ | ||
_attributes ??= new Dictionary<string, ValueTypePair>(); | ||
_attributes[key] = new ValueTypePair(value, "string"); | ||
} | ||
|
||
public void SetAttribute(string key, bool value) | ||
{ | ||
_attributes ??= new Dictionary<string, ValueTypePair>(); | ||
_attributes[key] = new ValueTypePair(value, "boolean"); | ||
} | ||
|
||
public void SetAttribute(string key, int value) | ||
{ | ||
_attributes ??= new Dictionary<string, ValueTypePair>(); | ||
_attributes[key] = new ValueTypePair(value, "integer"); | ||
} | ||
|
||
public void SetAttribute(string key, double value) | ||
{ | ||
_attributes ??= new Dictionary<string, ValueTypePair>(); | ||
_attributes[key] = new ValueTypePair(value, "double"); | ||
} | ||
|
||
public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger) | ||
{ | ||
_attributes = new Dictionary<string, ValueTypePair> | ||
{ | ||
{ "sentry.environment", new ValueTypePair("production", "string")}, | ||
{ "sentry.release", new ValueTypePair("1.0.0", "string")}, | ||
{ "sentry.trace.parent_span_id", new ValueTypePair("b0e6f15b45c36b12", "string")}, | ||
}; | ||
|
||
writer.WriteStartObject(); | ||
writer.WriteStartArray("items"); | ||
writer.WriteStartObject(); | ||
|
||
writer.WriteNumber("timestamp", Timestamp.ToUnixTimeSeconds()); | ||
writer.WriteString("trace_id", TraceId); | ||
writer.WriteString("level", Level.ToLogString()); | ||
writer.WriteString("body", Message); | ||
|
||
writer.WritePropertyName("attributes"); | ||
writer.WriteStartObject(); | ||
|
||
if (Template is not null) | ||
{ | ||
writer.WriteSerializable("sentry.message.template", new ValueTypePair(Template, "string"), null); | ||
} | ||
|
||
if (Parameters is not null) | ||
{ | ||
for (var index = 0; index < Parameters.Length; index++) | ||
{ | ||
var type = "string"; | ||
writer.WriteSerializable($"sentry.message.parameters.{index}", new ValueTypePair(Parameters[index], type), null); | ||
} | ||
} | ||
|
||
if (_attributes is not null) | ||
{ | ||
foreach (var attribute in _attributes) | ||
{ | ||
writer.WriteSerializable(attribute.Key, attribute.Value, null); | ||
} | ||
} | ||
|
||
writer.WriteEndObject(); | ||
|
||
if (SeverityNumber != -1) | ||
{ | ||
writer.WriteNumber("severity_number", SeverityNumber); | ||
} | ||
|
||
writer.WriteEndObject(); | ||
writer.WriteEndArray(); | ||
writer.WriteEndObject(); | ||
} | ||
} | ||
|
||
//TODO: remove? perhaps a simple System.ValueTuple`2 suffices | ||
internal readonly struct ValueTypePair : ISentryJsonSerializable | ||
{ | ||
public ValueTypePair(object value, string type) | ||
{ | ||
Value = value.ToString()!; | ||
Type = type; | ||
} | ||
|
||
public string Value { get; } | ||
public string Type { get; } | ||
|
||
public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger) | ||
{ | ||
writer.WriteStartObject(); | ||
|
||
writer.WriteString("value", Value); | ||
writer.WriteString("type", Type); | ||
|
||
writer.WriteEndObject(); | ||
} | ||
} |
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,74 @@ | ||
using Sentry.Infrastructure; | ||
|
||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
|
||
namespace Sentry.Experimental; | ||
|
||
//TODO: QUESTION: not sure about the name | ||
// this is a bit different to Sentry.SentryLevel and Sentry.BreadcrumbLevel | ||
Flash0ver marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[Experimental(DiagnosticId.ExperimentalSentryLogs)] | ||
public enum SentrySeverity : short | ||
{ | ||
Trace, | ||
Debug, | ||
Info, | ||
Warn, | ||
Error, | ||
Fatal, | ||
} | ||
|
||
[Experimental(DiagnosticId.ExperimentalSentryLogs)] | ||
internal static class SentrySeverityExtensions | ||
{ | ||
internal static string ToLogString(this SentrySeverity severity) | ||
{ | ||
return severity switch | ||
{ | ||
SentrySeverity.Trace => "trace", | ||
SentrySeverity.Debug => "debug", | ||
SentrySeverity.Info => "info", | ||
SentrySeverity.Warn => "warn", | ||
SentrySeverity.Error => "error", | ||
SentrySeverity.Fatal => "fatal", | ||
_ => throw new ArgumentOutOfRangeException(nameof(severity), severity, null), | ||
}; | ||
} | ||
|
||
internal static SentrySeverity FromSeverityNumber(int severityNumber) | ||
{ | ||
ThrowIfOutOfRange(severityNumber); | ||
|
||
return severityNumber switch | ||
{ | ||
>= 1 and <= 4 => SentrySeverity.Trace, | ||
>= 5 and <= 8 => SentrySeverity.Debug, | ||
>= 9 and <= 12 => SentrySeverity.Info, | ||
>= 13 and <= 16 => SentrySeverity.Warn, | ||
>= 17 and <= 20 => SentrySeverity.Error, | ||
>= 21 and <= 24 => SentrySeverity.Fatal, | ||
_ => throw new UnreachableException(), | ||
}; | ||
} | ||
|
||
internal static int ToSeverityNumber(SentrySeverity severity) | ||
{ | ||
return severity switch | ||
{ | ||
SentrySeverity.Trace => 1, | ||
SentrySeverity.Debug => 5, | ||
SentrySeverity.Info => 9, | ||
SentrySeverity.Warn => 13, | ||
SentrySeverity.Error => 17, | ||
SentrySeverity.Fatal => 21, | ||
_ => throw new ArgumentOutOfRangeException(nameof(severity), severity, null) | ||
}; | ||
} | ||
|
||
internal static void ThrowIfOutOfRange(int severityNumber) | ||
{ | ||
if (severityNumber is < 1 or > 24) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(severityNumber), severityNumber, "SeverityNumber must be between 1 (inclusive) and 24 (inclusive)."); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Sentry/Experimental/System.Diagnostics.CodeAnalysis.ExperimentalAttribute.cs
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,29 @@ | ||
#if !NET8_0_OR_GREATER | ||
// ReSharper disable CheckNamespace | ||
// ReSharper disable ConvertToPrimaryConstructor | ||
namespace System.Diagnostics.CodeAnalysis; | ||
|
||
[AttributeUsage(AttributeTargets.Assembly | | ||
AttributeTargets.Module | | ||
AttributeTargets.Class | | ||
AttributeTargets.Struct | | ||
AttributeTargets.Enum | | ||
AttributeTargets.Constructor | | ||
AttributeTargets.Method | | ||
AttributeTargets.Property | | ||
AttributeTargets.Field | | ||
AttributeTargets.Event | | ||
AttributeTargets.Interface | | ||
AttributeTargets.Delegate, Inherited = false)] | ||
internal sealed class ExperimentalAttribute : Attribute | ||
Flash0ver marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public ExperimentalAttribute(string diagnosticId) | ||
{ | ||
DiagnosticId = diagnosticId; | ||
} | ||
|
||
public string DiagnosticId { get; } | ||
|
||
public string? UrlFormat { get; set; } | ||
} | ||
#endif |
22 changes: 22 additions & 0 deletions
22
src/Sentry/Experimental/System.Diagnostics.UnreachableException.cs
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,22 @@ | ||
#if !NET7_0_OR_GREATER | ||
// ReSharper disable CheckNamespace | ||
namespace System.Diagnostics; | ||
|
||
internal sealed class UnreachableException : Exception | ||
{ | ||
public UnreachableException() | ||
: base("The program executed an instruction that was thought to be unreachable.") | ||
{ | ||
} | ||
|
||
public UnreachableException(string? message) | ||
: base(message ?? "The program executed an instruction that was thought to be unreachable.") | ||
{ | ||
} | ||
|
||
public UnreachableException(string? message, Exception? innerException) | ||
: base(message ?? "The program executed an instruction that was thought to be unreachable.", innerException) | ||
{ | ||
} | ||
} | ||
#endif |
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
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.