Skip to content
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

Add additional data to transactions #763

Merged
merged 3 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Tyrrrz marked this conversation as resolved.
Show resolved Hide resolved
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?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we should bump this so it's available on any Event (transaction or errors)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is currently IEventLike also encompasses Scope, which does not have Release :/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add it, no problem

/// <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