-
-
Notifications
You must be signed in to change notification settings - Fork 225
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
Changes from all commits
0add668
db7d558
d96b092
2958a47
a63371b
d8d2567
fefd24c
165996a
32e7e25
fc88722
2ba87e4
0f1d4a4
8dec5d5
a664f7e
96693d0
83964cf
dadc69b
c91cdde
0740c3b
eee06bf
8c61d8b
80683ae
cb20118
2cb306f
dcc0ec1
58dce74
f2e1ba2
0220015
6822b23
dd39fae
6eb5b9b
69c05b8
430cf82
31a8f1f
2ae4476
69678ce
97995a8
fbe747d
64adf33
cdfa901
d2ac53b
4011ba6
4ae82d0
bc1c465
79fb190
b4e80f4
b21adef
0032858
a9769f8
9a51033
9a09832
f133118
72c9a93
c97f4ad
a9eea90
8bd0ed2
51892de
acc8995
6bd4c96
f673d1e
daafd7f
62ee5d5
6a54203
479cab8
97a87f8
0467449
54062d2
7107bce
c0a1cd5
45b8687
3192534
b8bcea6
d4c82a2
9193a96
7cb4043
e3ca5b5
afb135e
7f675aa
750a388
9f62d3b
5b00c21
6d17918
7e2c57b
7115c42
cd5246b
baf5569
6e13e95
0a9a3b1
934fb36
2c1608e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| using Sentry.Extensibility; | ||
| using Sentry.Infrastructure; | ||
| using Sentry.Protocol.Envelopes; | ||
|
|
||
| namespace Sentry.Internal; | ||
|
|
||
| internal sealed class DefaultSentryStructuredLogger : SentryStructuredLogger | ||
| { | ||
| private readonly IHub _hub; | ||
| private readonly SentryOptions _options; | ||
| private readonly ISystemClock _clock; | ||
|
|
||
| internal DefaultSentryStructuredLogger(IHub hub, SentryOptions options, ISystemClock clock) | ||
| { | ||
| Debug.Assert(options is { Experimental.EnableLogs: true }); | ||
|
|
||
| _hub = hub; | ||
| _options = options; | ||
| _clock = clock; | ||
| } | ||
|
|
||
| private protected override void CaptureLog(SentryLogLevel level, string template, object[]? parameters, Action<SentryLog>? configureLog) | ||
| { | ||
| var timestamp = _clock.GetUtcNow(); | ||
| var traceHeader = _hub.GetTraceHeader() ?? SentryTraceHeader.Empty; | ||
|
|
||
| string message; | ||
| try | ||
| { | ||
| message = string.Format(CultureInfo.InvariantCulture, template, parameters ?? []); | ||
| } | ||
| catch (FormatException e) | ||
| { | ||
| _options.DiagnosticLogger?.LogError(e, "Template string does not match the provided argument. The Log will be dropped."); | ||
| return; | ||
| } | ||
|
|
||
| SentryLog log = new(timestamp, traceHeader.TraceId, level, message) | ||
| { | ||
| Template = template, | ||
| Parameters = ImmutableArray.Create(parameters), | ||
| ParentSpanId = traceHeader.SpanId, | ||
| }; | ||
|
|
||
| try | ||
| { | ||
| configureLog?.Invoke(log); | ||
|
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. I find it odd that we have this callback to be invoked directly when capturing a log. What are the use cases that we'll call explicitly: The use case for a callback when the user isn't calling the |
||
| } | ||
| catch (Exception e) | ||
| { | ||
| _options.DiagnosticLogger?.LogError(e, "The configureLog callback threw an exception. The Log will be dropped."); | ||
| return; | ||
| } | ||
|
|
||
| var scope = _hub.GetScope(); | ||
| log.SetDefaultAttributes(_options, scope?.Sdk ?? SdkVersion.Instance); | ||
|
|
||
| var configuredLog = log; | ||
| if (_options.Experimental.BeforeSendLogInternal is { } beforeSendLog) | ||
| { | ||
| try | ||
| { | ||
| configuredLog = beforeSendLog.Invoke(log); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| _options.DiagnosticLogger?.LogError(e, "The BeforeSendLog callback threw an exception. The Log will be dropped."); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (configuredLog is not null) | ||
| { | ||
| //TODO: enqueue in Batch-Processor / Background-Worker | ||
| // see https://github.com/getsentry/sentry-dotnet/issues/4132 | ||
| _ = _hub.CaptureEnvelope(Envelope.FromLog(configuredLog)); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| namespace Sentry.Internal; | ||
|
|
||
| internal sealed class DisabledSentryStructuredLogger : SentryStructuredLogger | ||
| { | ||
| internal static DisabledSentryStructuredLogger Instance { get; } = new DisabledSentryStructuredLogger(); | ||
|
|
||
| internal DisabledSentryStructuredLogger() | ||
| { | ||
| } | ||
|
|
||
| private protected override void CaptureLog(SentryLogLevel level, string template, object[]? parameters, Action<SentryLog>? configureLog) | ||
| { | ||
| // disabled | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.