|
| 1 | +using System.Text.Encodings.Web; |
| 2 | + |
| 3 | +namespace Sentry.Tests.Protocol; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// <see href="https://develop.sentry.dev/sdk/telemetry/logs/"/> |
| 7 | +/// </summary> |
| 8 | +public class SentryLogTests |
| 9 | +{ |
| 10 | + private static readonly DateTimeOffset Timestamp = new(2025, 04, 22, 14, 51, 00, TimeSpan.FromHours(2)); |
| 11 | + private static readonly SentryId TraceId = SentryId.Create(); |
| 12 | + private static readonly SpanId? ParentSpanId = SpanId.Create(); |
| 13 | + |
| 14 | + private static readonly ISystemClock Clock = new MockClock(Timestamp); |
| 15 | + private static readonly JsonSerializerOptions JsonSerializerOptions = new() |
| 16 | + { |
| 17 | + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, |
| 18 | + WriteIndented = true, |
| 19 | + }; |
| 20 | + |
| 21 | + private readonly IDiagnosticLogger _output; |
| 22 | + |
| 23 | + public SentryLogTests(ITestOutputHelper output) |
| 24 | + { |
| 25 | + _output = new TestOutputDiagnosticLogger(output); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void WriteTo_Envelope_MinimalSerializedSentryLog() |
| 30 | + { |
| 31 | + var options = new SentryOptions |
| 32 | + { |
| 33 | + Environment = "my-environment", |
| 34 | + Release = "my-release", |
| 35 | + }; |
| 36 | + |
| 37 | + var log = new SentryLog(Timestamp, TraceId, SentryLogLevel.Trace, "message"); |
| 38 | + log.SetAttributes(options); |
| 39 | + |
| 40 | + var envelope = Envelope.FromLog(log); |
| 41 | + |
| 42 | + using var stream = new MemoryStream(); |
| 43 | + envelope.Serialize(stream, _output, Clock); |
| 44 | + stream.Seek(0, SeekOrigin.Begin); |
| 45 | + using var reader = new StreamReader(stream); |
| 46 | + var header = JsonDocument.Parse(reader.ReadLine()!); |
| 47 | + var item = JsonDocument.Parse(reader.ReadLine()!); |
| 48 | + var payload = JsonDocument.Parse(reader.ReadLine()!); |
| 49 | + |
| 50 | + reader.EndOfStream.Should().BeTrue(); |
| 51 | + |
| 52 | + JsonSerializer.Serialize(header, JsonSerializerOptions).Should().Be($$""" |
| 53 | + { |
| 54 | + "sdk": { |
| 55 | + "name": "{{SdkVersion.Instance.Name}}", |
| 56 | + "version": "{{SdkVersion.Instance.Version}}" |
| 57 | + }, |
| 58 | + "sent_at": "{{Timestamp.Format()}}" |
| 59 | + } |
| 60 | + """); |
| 61 | + |
| 62 | + JsonSerializer.Serialize(item, JsonSerializerOptions).Should().Match(""" |
| 63 | + { |
| 64 | + "type": "log", |
| 65 | + "item_count": 1, |
| 66 | + "content_type": "application/vnd.sentry.items.log+json", |
| 67 | + "length": ?* |
| 68 | + } |
| 69 | + """); |
| 70 | + |
| 71 | + JsonSerializer.Serialize(payload, JsonSerializerOptions).Should().Be($$""" |
| 72 | + { |
| 73 | + "items": [ |
| 74 | + { |
| 75 | + "timestamp": {{Timestamp.ToUnixTimeSeconds()}}, |
| 76 | + "level": "trace", |
| 77 | + "body": "message", |
| 78 | + "trace_id": "{{TraceId.ToString()}}", |
| 79 | + "attributes": { |
| 80 | + "sentry.environment": { |
| 81 | + "value": "my-environment", |
| 82 | + "type": "string" |
| 83 | + }, |
| 84 | + "sentry.release": { |
| 85 | + "value": "my-release", |
| 86 | + "type": "string" |
| 87 | + }, |
| 88 | + "sentry.sdk.name": { |
| 89 | + "value": "{{SdkVersion.Instance.Name}}", |
| 90 | + "type": "string" |
| 91 | + }, |
| 92 | + "sentry.sdk.version": { |
| 93 | + "value": "{{SdkVersion.Instance.Version}}", |
| 94 | + "type": "string" |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + ] |
| 99 | + } |
| 100 | + """); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public void WriteTo_EnvelopeItem_MaximalSerializedSentryLog() |
| 105 | + { |
| 106 | + var options = new SentryOptions |
| 107 | + { |
| 108 | + Environment = "my-environment", |
| 109 | + Release = "my-release", |
| 110 | + }; |
| 111 | + |
| 112 | + var log = new SentryLog(Timestamp, TraceId, (SentryLogLevel)24, "message") |
| 113 | + { |
| 114 | + Template = "template", |
| 115 | + Parameters = ImmutableArray.Create<object>("string", false, 1, 2.2), |
| 116 | + ParentSpanId = ParentSpanId, |
| 117 | + }; |
| 118 | + log.SetAttribute("string-attribute", "string-value"); |
| 119 | + log.SetAttribute("boolean-attribute", true); |
| 120 | + log.SetAttribute("integer-attribute", 3); |
| 121 | + log.SetAttribute("double-attribute", 4.4); |
| 122 | + log.SetAttributes(options); |
| 123 | + |
| 124 | + var envelope = EnvelopeItem.FromLog(log); |
| 125 | + |
| 126 | + using var stream = new MemoryStream(); |
| 127 | + envelope.Serialize(stream, _output); |
| 128 | + stream.Seek(0, SeekOrigin.Begin); |
| 129 | + using var reader = new StreamReader(stream); |
| 130 | + var item = JsonDocument.Parse(reader.ReadLine()!); |
| 131 | + var payload = JsonDocument.Parse(reader.ReadLine()!); |
| 132 | + |
| 133 | + reader.EndOfStream.Should().BeTrue(); |
| 134 | + |
| 135 | + JsonSerializer.Serialize(item, JsonSerializerOptions).Should().Match(""" |
| 136 | + { |
| 137 | + "type": "log", |
| 138 | + "item_count": 1, |
| 139 | + "content_type": "application/vnd.sentry.items.log+json", |
| 140 | + "length": ?* |
| 141 | + } |
| 142 | + """); |
| 143 | + |
| 144 | + JsonSerializer.Serialize(payload, JsonSerializerOptions).Should().Be($$""" |
| 145 | + { |
| 146 | + "items": [ |
| 147 | + { |
| 148 | + "timestamp": {{Timestamp.ToUnixTimeSeconds()}}, |
| 149 | + "level": "fatal", |
| 150 | + "body": "message", |
| 151 | + "trace_id": "{{TraceId.ToString()}}", |
| 152 | + "severity_number": 24, |
| 153 | + "attributes": { |
| 154 | + "sentry.message.template": { |
| 155 | + "value": "template", |
| 156 | + "type": "string" |
| 157 | + }, |
| 158 | + "sentry.message.parameter.0": { |
| 159 | + "value": "string", |
| 160 | + "type": "string" |
| 161 | + }, |
| 162 | + "sentry.message.parameter.1": { |
| 163 | + "value": false, |
| 164 | + "type": "boolean" |
| 165 | + }, |
| 166 | + "sentry.message.parameter.2": { |
| 167 | + "value": 1, |
| 168 | + "type": "integer" |
| 169 | + }, |
| 170 | + "sentry.message.parameter.3": { |
| 171 | + "value": 2.2, |
| 172 | + "type": "double" |
| 173 | + }, |
| 174 | + "string-attribute": { |
| 175 | + "value": "string-value", |
| 176 | + "type": "string" |
| 177 | + }, |
| 178 | + "boolean-attribute": { |
| 179 | + "value": true, |
| 180 | + "type": "boolean" |
| 181 | + }, |
| 182 | + "integer-attribute": { |
| 183 | + "value": 3, |
| 184 | + "type": "integer" |
| 185 | + }, |
| 186 | + "double-attribute": { |
| 187 | + "value": 4.4, |
| 188 | + "type": "double" |
| 189 | + }, |
| 190 | + "sentry.environment": { |
| 191 | + "value": "my-environment", |
| 192 | + "type": "string" |
| 193 | + }, |
| 194 | + "sentry.release": { |
| 195 | + "value": "my-release", |
| 196 | + "type": "string" |
| 197 | + }, |
| 198 | + "sentry.sdk.name": { |
| 199 | + "value": "{{SdkVersion.Instance.Name}}", |
| 200 | + "type": "string" |
| 201 | + }, |
| 202 | + "sentry.sdk.version": { |
| 203 | + "value": "{{SdkVersion.Instance.Version}}", |
| 204 | + "type": "string" |
| 205 | + }, |
| 206 | + "sentry.trace.parent_span_id": { |
| 207 | + "value": "{{ParentSpanId.ToString()}}", |
| 208 | + "type": "string" |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + ] |
| 213 | + } |
| 214 | + """); |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +file static class JsonFormatterExtensions |
| 219 | +{ |
| 220 | + public static string Format(this DateTimeOffset value) |
| 221 | + { |
| 222 | + return value.ToString("yyyy-MM-ddTHH:mm:sszzz", DateTimeFormatInfo.InvariantInfo); |
| 223 | + } |
| 224 | +} |
0 commit comments