Skip to content

Commit

Permalink
Add additional data to transactions (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz authored Jan 26, 2021
1 parent 281ed1d commit 6d9083e
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add support for dynamic transaction sampling. (#753) @Tyrrrz
- Integrate trace headers. (#758) @Tyrrrz
- Renamed Option `DiagnosticsLevel` to `DiagnosticLevel` (#759) @bruno-garcia
- Add additional data to transactions (#763) @Tyrrrz

## 3.0.0-beta.0

Expand Down
17 changes: 17 additions & 0 deletions src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public ITransaction StartTransaction(
{
var transaction = new Transaction(this, context);

// Transactions are not handled by event processors, so some things need to be added manually

// Apply scope
ScopeManager.GetCurrent().Key.Apply(transaction);

// SDK information
var nameAndVersion = MainSentryEventProcessor.NameAndVersion;
var protocolPackageName = MainSentryEventProcessor.ProtocolPackageName;

Expand All @@ -125,6 +131,17 @@ public ITransaction StartTransaction(
transaction.Sdk.AddPackage(protocolPackageName, nameAndVersion.Version);
}

// Release information
transaction.Release ??= _options.Release ?? ReleaseLocator.GetCurrent();

// Environment information
var foundEnvironment = EnvironmentLocator.Locate();
transaction.Environment ??= (string.IsNullOrWhiteSpace(foundEnvironment)
? string.IsNullOrWhiteSpace(_options.Environment)
? Constants.ProductionEnvironmentSetting
: _options.Environment
: foundEnvironment);

// Make a sampling decision
var samplingContext = new TransactionSamplingContext(
context,
Expand Down
6 changes: 6 additions & 0 deletions src/Sentry/Protocol/ITransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public interface ITransaction : ISpan, ITransactionContext, IEventLike
// 'new' because it adds a setter
new string Name { get; set; }

// Should this be on IEventLike?
/// <summary>
/// The release version of the application.
/// </summary>
string? Release { get; set; }

/// <summary>
/// Flat list of spans within this transaction.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Sentry/Protocol/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public SentryId TraceId
/// <inheritdoc cref="ITransaction.Name" />
public string Name { get; set; }

/// <inheritdoc />
public string? Release { get; set; }

/// <inheritdoc />
public DateTimeOffset StartTimestamp { get; internal set; } = DateTimeOffset.UtcNow;

Expand Down Expand Up @@ -277,6 +280,11 @@ public void WriteTo(Utf8JsonWriter writer)
writer.WriteString("level", level.ToString().ToLowerInvariant());
}

if (!string.IsNullOrWhiteSpace(Release))
{
writer.WriteString("release", Release);
}

if (!string.IsNullOrWhiteSpace(Name))
{
writer.WriteString("transaction", Name);
Expand Down Expand Up @@ -392,6 +400,7 @@ public static Transaction FromJson(JsonElement json)
var startTimestamp = json.GetProperty("start_timestamp").GetDateTimeOffset();
var endTimestamp = json.GetPropertyOrNull("timestamp")?.GetDateTimeOffset();
var level = json.GetPropertyOrNull("level")?.GetString()?.Pipe(s => s.ParseEnum<SentryLevel>());
var release = json.GetPropertyOrNull("release")?.GetString();
var request = json.GetPropertyOrNull("request")?.Pipe(Request.FromJson);
var contexts = json.GetPropertyOrNull("contexts")?.Pipe(Contexts.FromJson);
var user = json.GetPropertyOrNull("user")?.Pipe(User.FromJson);
Expand All @@ -409,6 +418,7 @@ public static Transaction FromJson(JsonElement json)
StartTimestamp = startTimestamp,
EndTimestamp = endTimestamp,
Level = level,
Release = release,
_request = request,
_contexts = contexts,
_user = user,
Expand Down
70 changes: 70 additions & 0 deletions test/Sentry.Tests/HubTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,76 @@ public void StartTransaction_DynamicSampling_FallbackToStatic_SampledOut()
transaction.IsSampled.Should().BeFalse();
}

[Fact]
public void StartTransaction_ContainsSdk()
{
// Arrange
var hub = new Hub(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithSecret
});

// Act
var transaction = hub.StartTransaction("name", "operation");

// Assert
transaction.Sdk.Name.Should().NotBeNullOrWhiteSpace();
transaction.Sdk.Version.Should().NotBeNullOrWhiteSpace();
}

[Fact]
public void StartTransaction_ContainsRelease()
{
// Arrange
var hub = new Hub(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithSecret
});

// Act
var transaction = hub.StartTransaction("name", "operation");

// Assert
transaction.Release.Should().NotBeNullOrWhiteSpace();
}

[Fact]
public void StartTransaction_ContainsEnvironment()
{
// Arrange
var hub = new Hub(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithSecret
});

// Act
var transaction = hub.StartTransaction("name", "operation");

// Assert
transaction.Environment.Should().NotBeNullOrWhiteSpace();
}

[Fact]
public void StartTransaction_ContainsTagsFromScope()
{
// Arrange
var hub = new Hub(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithSecret
});

hub.ConfigureScope(scope =>
{
scope.SetTag("foo", "bar");

// Act
var transaction = hub.StartTransaction("name", "operation");

// Assert
transaction.Tags.Should().Contain(tag => tag.Key == "foo" && tag.Value == "bar");
});
}

[Fact]
public void GetTraceHeader_ReturnsHeaderForActiveSpan()
{
Expand Down

0 comments on commit 6d9083e

Please sign in to comment.