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

feat: Add transaction to baggage and trace headers #1992

Merged
merged 3 commits into from
Jul 28, 2022
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 @@
### Features

- Read free_memory when the event is captured, not only at SDK startup (#1962)
- Add transaction to baggage and trace headers (#1992)
- Provide private access to SentryOptions for hybrid SDKs (#1991)

### Fixes
Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/SentryBaggage.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ - (instancetype)initWithTraceId:(SentryId *)traceId
publicKey:(NSString *)publicKey
releaseName:(nullable NSString *)releaseName
environment:(nullable NSString *)environment
transaction:(nullable NSString *)transaction
userSegment:(nullable NSString *)userSegment
sampleRate:(nullable NSString *)sampleRate
{
Expand All @@ -23,6 +24,7 @@ - (instancetype)initWithTraceId:(SentryId *)traceId
_publicKey = publicKey;
_releaseName = releaseName;
_environment = environment;
_transaction = transaction;
_userSegment = userSegment;
_sampleRate = sampleRate;
}
Expand All @@ -42,6 +44,9 @@ - (NSString *)toHTTPHeader
if (_environment != nil)
[information setValue:_environment forKey:@"sentry-environment"];

if (_transaction != nil)
[information setValue:_transaction forKey:@"sentry-transaction"];

if (_userSegment != nil)
[information setValue:_userSegment forKey:@"sentry-user_segment"];

Expand Down
8 changes: 8 additions & 0 deletions Sources/Sentry/SentryTraceContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ - (instancetype)initWithTraceId:(SentryId *)traceId
publicKey:(NSString *)publicKey
releaseName:(nullable NSString *)releaseName
environment:(nullable NSString *)environment
transaction:(nullable NSString *)transaction
userSegment:(nullable NSString *)userSegment
sampleRate:(nullable NSString *)sampleRate
{
Expand All @@ -25,6 +26,7 @@ - (instancetype)initWithTraceId:(SentryId *)traceId
_publicKey = publicKey;
_environment = environment;
_releaseName = releaseName;
_transaction = transaction;
_userSegment = userSegment;
_sampleRate = sampleRate;
}
Expand Down Expand Up @@ -63,6 +65,7 @@ - (nullable instancetype)initWithTracer:(SentryTracer *)tracer
publicKey:options.parsedDsn.url.user
releaseName:options.releaseName
environment:options.environment
transaction:tracer.name
userSegment:userSegment
sampleRate:sampleRate];
}
Expand All @@ -87,6 +90,7 @@ - (nullable instancetype)initWithDict:(NSDictionary<NSString *, id> *)dictionary
publicKey:publicKey
releaseName:dictionary[@"release"]
environment:dictionary[@"environment"]
transaction:dictionary[@"transaction"]
userSegment:userSegment
sampleRate:dictionary[@"sample_rate"]];
}
Expand All @@ -97,6 +101,7 @@ - (SentryBaggage *)toBaggage
publicKey:_publicKey
releaseName:_releaseName
environment:_environment
transaction:_transaction
userSegment:_userSegment
sampleRate:_sampleRate];
return result;
Expand All @@ -113,6 +118,9 @@ - (SentryBaggage *)toBaggage
if (_environment != nil)
[result setValue:_environment forKey:@"environment"];

if (_transaction != nil)
[result setValue:_transaction forKey:@"transaction"];

if (_userSegment != nil)
[result setValue:_userSegment forKey:@"user_segment"];

Expand Down
1 change: 1 addition & 0 deletions Sources/Sentry/include/SentryBaggage.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static NSString *const SENTRY_BAGGAGE_HEADER = @"baggage";
publicKey:(NSString *)publicKey
releaseName:(nullable NSString *)releaseName
environment:(nullable NSString *)environment
transaction:(nullable NSString *)transaction
userSegment:(nullable NSString *)userSegment
sampleRate:(nullable NSString *)sampleRate;

Expand Down
6 changes: 6 additions & 0 deletions Sources/Sentry/include/SentryTraceContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nullable, nonatomic, readonly) NSString *environment;

/**
* The transaction name set on the scope.
*/
@property (nullable, nonatomic, readonly) NSString *transaction;

/**
* A subset of the scope's user context.
*/
Expand All @@ -46,6 +51,7 @@ NS_ASSUME_NONNULL_BEGIN
publicKey:(NSString *)publicKey
releaseName:(nullable NSString *)releaseName
environment:(nullable NSString *)environment
transaction:(nullable NSString *)transaction
userSegment:(nullable NSString *)userSegment
sampleRate:(nullable NSString *)sampleRate;

Expand Down
4 changes: 2 additions & 2 deletions Tests/SentryTests/Helper/SentrySerializationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SentrySerializationTests: XCTestCase {

private class Fixture {
static var invalidData = "hi".data(using: .utf8)!
static var traceContext = SentryTraceContext(trace: SentryId(), publicKey: "PUBLIC_KEY", releaseName: "RELEASE_NAME", environment: "TEST", userSegment: "some segment", sampleRate: "0.25")
static var traceContext = SentryTraceContext(trace: SentryId(), publicKey: "PUBLIC_KEY", releaseName: "RELEASE_NAME", environment: "TEST", transaction: "transaction", userSegment: "some segment", sampleRate: "0.25")
}

func testSentryEnvelopeSerializer_WithSingleEvent() {
Expand Down Expand Up @@ -111,7 +111,7 @@ class SentrySerializationTests: XCTestCase {
}

func testSentryEnvelopeSerializer_TraceStateWithoutUser() {
let trace = SentryTraceContext(trace: SentryId(), publicKey: "PUBLIC_KEY", releaseName: "RELEASE_NAME", environment: "TEST", userSegment: nil, sampleRate: nil)
let trace = SentryTraceContext(trace: SentryId(), publicKey: "PUBLIC_KEY", releaseName: "RELEASE_NAME", environment: "TEST", transaction: "transaction", userSegment: nil, sampleRate: nil)

let envelopeHeader = SentryEnvelopeHeader(id: nil, traceContext: trace)
let envelope = SentryEnvelope(header: envelopeHeader, singleItem: createItemWithEmptyAttachment())
Expand Down
2 changes: 1 addition & 1 deletion Tests/SentryTests/Protocol/SentryEnvelopeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class SentryEnvelopeTests: XCTestCase {

func testInitSentryEnvelopeHeader_SetIdAndTraceState() {
let eventId = SentryId()
let traceContext = SentryTraceContext(trace: SentryId(), publicKey: "publicKey", releaseName: "releaseName", environment: "environment", userSegment: nil, sampleRate: nil)
let traceContext = SentryTraceContext(trace: SentryId(), publicKey: "publicKey", releaseName: "releaseName", environment: "environment", transaction: "transaction", userSegment: nil, sampleRate: nil)

let envelopeHeader = SentryEnvelopeHeader(id: eventId, traceContext: traceContext)
XCTAssertEqual(eventId, envelopeHeader.eventId)
Expand Down
6 changes: 3 additions & 3 deletions Tests/SentryTests/Transaction/SentryBaggageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import XCTest
class SentryBaggageTests: XCTestCase {

func test_baggageToHeader() {
let header = SentryBaggage(trace: SentryId.empty, publicKey: "publicKey", releaseName: "release name", environment: "teste", userSegment: "test user", sampleRate: "0.49").toHTTPHeader()
let header = SentryBaggage(trace: SentryId.empty, publicKey: "publicKey", releaseName: "release name", environment: "teste", transaction: "transaction", userSegment: "test user", sampleRate: "0.49").toHTTPHeader()

XCTAssertEqual(header, "sentry-environment=teste,sentry-public_key=publicKey,sentry-release=release%20name,sentry-sample_rate=0.49,sentry-trace_id=00000000000000000000000000000000,sentry-user_segment=test%20user")
XCTAssertEqual(header, "sentry-environment=teste,sentry-public_key=publicKey,sentry-release=release%20name,sentry-sample_rate=0.49,sentry-trace_id=00000000000000000000000000000000,sentry-transaction=transaction,sentry-user_segment=test%20user")
}

func test_baggageToHeader_onlyTrace_ignoreNils() {
let header = SentryBaggage(trace: SentryId.empty, publicKey: "publicKey", releaseName: nil, environment: nil, userSegment: nil, sampleRate: nil).toHTTPHeader()
let header = SentryBaggage(trace: SentryId.empty, publicKey: "publicKey", releaseName: nil, environment: nil, transaction: nil, userSegment: nil, sampleRate: nil).toHTTPHeader()

XCTAssertEqual(header, "sentry-public_key=publicKey,sentry-trace_id=00000000000000000000000000000000")
}
Expand Down
3 changes: 3 additions & 0 deletions Tests/SentryTests/Transaction/SentryTraceStateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class SentryTraceContextTests: XCTestCase {
publicKey: fixture.publicKey,
releaseName: fixture.releaseName,
environment: fixture.environment,
transaction: fixture.transactionName,
userSegment: fixture.userSegment,
sampleRate: fixture.sampleRate)

Expand Down Expand Up @@ -92,6 +93,7 @@ class SentryTraceContextTests: XCTestCase {
publicKey: fixture.publicKey,
releaseName: fixture.releaseName,
environment: fixture.environment,
transaction: fixture.transactionName,
userSegment: fixture.userSegment,
sampleRate: fixture.sampleRate)

Expand All @@ -110,6 +112,7 @@ class SentryTraceContextTests: XCTestCase {
XCTAssertEqual(traceContext.publicKey, fixture.publicKey)
XCTAssertEqual(traceContext.releaseName, fixture.releaseName)
XCTAssertEqual(traceContext.environment, fixture.environment)
XCTAssertEqual(traceContext.transaction, fixture.transactionName)
XCTAssertEqual(traceContext.userSegment, fixture.userSegment)
}

Expand Down