Skip to content

Commit

Permalink
Add SDK information to envelope (#1084)
Browse files Browse the repository at this point in the history
* Include SDK information in envelope

* Remove 1 todo

* Changelog

Co-authored-by: Bruno Garcia <bruno@brunogarcia.com>
  • Loading branch information
Tyrrrz and bruno-garcia committed Jun 23, 2021
1 parent 0d73cc4 commit 1d2efd5
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Implement pause & resume session ([#1069](https://github.com/getsentry/sentry-dotnet/pull/1069))
- Add auto session tracking ([#1068](https://github.com/getsentry/sentry-dotnet/pull/1068))
- Add SDK information to envelope ([#1084](https://github.com/getsentry/sentry-dotnet/pull/1084))
- Add ReportAssembliesMode in favor of ReportAssemblies ([#1079](https://github.com/getsentry/sentry-dotnet/pull/1079))

### Fixes
Expand Down
44 changes: 26 additions & 18 deletions src/Sentry/Envelopes/Envelope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public Envelope(IReadOnlyDictionary<string, object?> header, IReadOnlyList<Envel
Items = items;
}

public Envelope(IReadOnlyList<EnvelopeItem> items)
: this(Empty.Dictionary<string, object?>(), items)
{
}

/// <summary>
/// Attempts to extract the value of "sentry_id" header if it's present.
/// </summary>
Expand Down Expand Up @@ -76,6 +71,26 @@ public async Task SerializeAsync(Stream stream, CancellationToken cancellationTo
/// <inheritdoc />
public void Dispose() => Items.DisposeAll();

private static Dictionary<string, object?> CreateHeader(SentryId? eventId = null)
{
var header = new Dictionary<string, object?>(2, StringComparer.Ordinal)
{
// Include limited SDK information (no packages)
["sdk"] = new Dictionary<string, string?>(2, StringComparer.Ordinal)
{
["name"] = SdkVersion.Instance.Name,
["version"] = SdkVersion.Instance.Version
}
};

if (eventId is not null)
{
header[EventIdKey] = eventId.Value.ToString();
}

return header;
}

/// <summary>
/// Creates an envelope that contains a single event.
/// </summary>
Expand All @@ -84,10 +99,7 @@ public async Task SerializeAsync(Stream stream, CancellationToken cancellationTo
IReadOnlyCollection<Attachment>? attachments = null,
SessionUpdate? sessionUpdate = null)
{
var header = new Dictionary<string, object?>(1, StringComparer.Ordinal)
{
[EventIdKey] = @event.EventId.ToString()
};
var header = CreateHeader(@event.EventId);

var items = new List<EnvelopeItem>
{
Expand All @@ -112,10 +124,7 @@ public async Task SerializeAsync(Stream stream, CancellationToken cancellationTo
/// </summary>
public static Envelope FromUserFeedback(UserFeedback sentryUserFeedback)
{
var header = new Dictionary<string, object?>(1, StringComparer.Ordinal)
{
[EventIdKey] = sentryUserFeedback.EventId.ToString()
};
var header = CreateHeader(sentryUserFeedback.EventId);

var items = new[]
{
Expand All @@ -130,10 +139,7 @@ public static Envelope FromUserFeedback(UserFeedback sentryUserFeedback)
/// </summary>
public static Envelope FromTransaction(Transaction transaction)
{
var header = new Dictionary<string, object?>(1, StringComparer.Ordinal)
{
[EventIdKey] = transaction.EventId.ToString()
};
var header = CreateHeader(transaction.EventId);

var items = new[]
{
Expand All @@ -148,12 +154,14 @@ public static Envelope FromTransaction(Transaction transaction)
/// </summary>
public static Envelope FromSession(SessionUpdate sessionUpdate)
{
var header = CreateHeader();

var items = new[]
{
EnvelopeItem.FromSession(sessionUpdate)
};

return new Envelope(items);
return new Envelope(header, items);
}

private static async Task<IReadOnlyDictionary<string, object?>> DeserializeHeaderAsync(
Expand Down
17 changes: 0 additions & 17 deletions src/Sentry/Internal/Empty.cs

This file was deleted.

10 changes: 3 additions & 7 deletions src/Sentry/Internal/Enricher.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Runtime.InteropServices;
using Sentry.PlatformAbstractions;
using Sentry.Reflection;
using OperatingSystem = Sentry.Protocol.OperatingSystem;
using Runtime = Sentry.Protocol.Runtime;

Expand All @@ -22,9 +21,6 @@ internal class Enricher
};
});

private readonly Lazy<SdkVersion> _sdkVersionLazy =
new(() => typeof(ISentryClient).Assembly.GetNameAndVersion());

public Enricher(SentryOptions options)
{
_options = options;
Expand Down Expand Up @@ -54,12 +50,12 @@ public void Apply(IEventLike eventLike)
if (eventLike.Sdk.Version is null && eventLike.Sdk.Name is null)
{
eventLike.Sdk.Name = Constants.SdkName;
eventLike.Sdk.Version = _sdkVersionLazy.Value.Version;
eventLike.Sdk.Version = SdkVersion.Instance.Version;
}

if (_sdkVersionLazy.Value.Version is not null)
if (SdkVersion.Instance.Version is not null)
{
eventLike.Sdk.AddPackage("nuget:" + _sdkVersionLazy.Value.Name, _sdkVersionLazy.Value.Version);
eventLike.Sdk.AddPackage("nuget:" + SdkVersion.Instance.Name, SdkVersion.Instance.Version);
}

// Platform
Expand Down
7 changes: 7 additions & 0 deletions src/Sentry/SdkVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Text.Json;
using Sentry.Internal.Extensions;
using Sentry.Reflection;

namespace Sentry
{
Expand All @@ -14,6 +15,12 @@ namespace Sentry
/// <remarks>Requires Sentry version 8.4 or higher.</remarks>
public sealed class SdkVersion : IJsonSerializable
{
private static readonly Lazy<SdkVersion> InstanceLazy = new(
() => typeof(ISentryClient).Assembly.GetNameAndVersion()
);

internal static SdkVersion Instance => InstanceLazy.Value;

internal ConcurrentBag<Package> InternalPackages { get; set; } = new();

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions test/Sentry.Tests/PlatformAbstractions/RuntimeInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public void SetNetCoreVersion_NetCoreAsName()
}
#endif

// TODO: Should be NET5_0_OR_GREATER which isn't available (to be added when NET6 lands)
#if NET5_0
#if NET5_0_OR_GREATER
[Fact]
public void SetNetCoreVersion_Net5Runtime_NullNetCoreVersion()
{
Expand Down
20 changes: 20 additions & 0 deletions test/Sentry.Tests/Protocol/Envelopes/EnvelopeTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Sentry.Protocol;
Expand Down Expand Up @@ -608,5 +609,24 @@ public async Task Deserialization_MalformedData_Throws()
async () => await Envelope.DeserializeAsync(input)
);
}

[Fact]
public void FromEvent_Header_IncludesSdkInformation()
{
// Act
var envelope = Envelope.FromEvent(new SentryEvent());

// Assert
envelope.Header.Any(kvp =>
{
var (key, value) = kvp;
return
key == "sdk" &&
value is IReadOnlyDictionary<string, string> nested &&
nested["name"] == SdkVersion.Instance.Name &&
nested["version"] == SdkVersion.Instance.Version;
}).Should().BeTrue();
}
}
}

0 comments on commit 1d2efd5

Please sign in to comment.