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(am): Introduce startTransaction #2600

Merged
merged 30 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2685577
feat: Introduce startTransaction
HazAT May 19, 2020
59586bb
fix: Correctly export interfaces
HazAT May 19, 2020
1409700
fix: Additional undefined check
HazAT May 19, 2020
95680f9
Merge branch 'master' into hazat/am-changes
HazAT May 20, 2020
5192e55
Apply suggestions from code review
HazAT May 20, 2020
0d5781a
fix: Log wording
HazAT May 20, 2020
5dcc194
fix: Don't log trace context with non transactions
HazAT May 20, 2020
e416400
fix: Doc Strings
HazAT May 20, 2020
b8dc363
feat: Added new option `startTraceForUserSession`
HazAT May 20, 2020
d44c123
fix: Typing docs
HazAT May 20, 2020
c6338cf
ref: Remove trace option
HazAT May 20, 2020
6556ae7
Update packages/types/src/span.ts
HazAT May 25, 2020
0631c5a
fix: Node, Add startChild
HazAT May 25, 2020
20ec671
fix: Tests
HazAT May 25, 2020
da67297
fix: Remove trace from apply to event
HazAT May 25, 2020
b5f16d9
fix: Lint errors + Vue Integration
HazAT May 25, 2020
0fd1fab
meta: Add changelog
HazAT May 25, 2020
78ecad8
fix: Vue integration
HazAT May 25, 2020
f088aad
fix: Transaction interface
HazAT May 25, 2020
46d2526
ref: CodeReview
HazAT May 26, 2020
de78c96
feat: Safeguard for name prop in transaction
HazAT May 26, 2020
6269bfd
fix: SampleRate and BeforeSend for Transaction
HazAT May 26, 2020
19840c3
ref: StartSpan creates a child of the span on scope
HazAT May 26, 2020
171e2a1
ref: Set unlabled transaction name
HazAT May 26, 2020
39312e8
ref: Slight refactor of startSpan
HazAT May 26, 2020
c288ad4
ref: Also fix traceHeaders
HazAT May 26, 2020
b077373
feat: Add additional tests
HazAT May 26, 2020
43940d7
fix: Small typo in tests
HazAT May 26, 2020
5446d82
fix: One off tests
HazAT May 26, 2020
0f0f051
Merge branch 'master' into hazat/am-changes
HazAT May 27, 2020
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
35 changes: 16 additions & 19 deletions packages/apm/src/hubextensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,6 @@ function startSpan(context: SpanContext | TransactionContext): Transaction | Spa
const scope = hub.getScope();
HazAT marked this conversation as resolved.
Show resolved Hide resolved
const client = hub.getClient();

let newSpanContext = context;

if (scope) {
// If there is a Span on the Scope we use the span_id / trace_id
// To define the parent <-> child relationship
const parentSpan = scope.getSpan();
if (parentSpan) {
const { trace_id } = parentSpan.getTraceContext();
newSpanContext = {
traceId: trace_id,
...context,
};
}
}

// This is our safeguard so people always get a Transaction in return.
// We set `_isTransaction: true` in {@link Sentry.startTransaction} to have a runtime check
// if the user really wanted to create a Transaction.
Expand All @@ -57,14 +42,17 @@ function startSpan(context: SpanContext | TransactionContext): Transaction | Spa
logger.warn('Will fall back to <unlabeled transaction>, use `transaction.setName()` to change it.');
}

if ((newSpanContext as TransactionContext).name) {
if ((context as TransactionContext).name) {
HazAT marked this conversation as resolved.
Show resolved Hide resolved
// We are dealing with a Transaction
const transaction = new Transaction(newSpanContext as TransactionContext, hub);
const transaction = new Transaction(context as TransactionContext, hub);

// We only roll the dice on sampling for root spans of transactions because all child spans inherit this state
if (transaction.sampled === undefined) {
const sampleRate = (client && client.getOptions().tracesSampleRate) || 0;
transaction.sampled = Math.random() <= sampleRate;
// if true = we want to have the transaction
// if false = we don't want to have it
// Math.random (inclusive of 0, but not 1)
transaction.sampled = Math.random() < sampleRate;
}

// We only want to create a span list if we sampled the transaction
Expand All @@ -77,7 +65,16 @@ function startSpan(context: SpanContext | TransactionContext): Transaction | Spa
return transaction;
}

return new Span(newSpanContext);
if (scope) {
// If there is a Span on the Scope we start a child and return that instead
const parentSpan = scope.getSpan();
if (parentSpan) {
return parentSpan.startChild(context);
}
}

// Otherwise we return a new Span
return new Span(context);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions packages/apm/src/integrations/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { logger } from '@sentry/utils';
// tslint:disable-next-line:no-implicit-dependencies
import { Application, ErrorRequestHandler, NextFunction, Request, RequestHandler, Response } from 'express';

/**
* Internal helper for `__sentry_transaction`
* @hidden
*/
interface SentryTracingResponse {
__sentry_transaction?: Transaction;
}
Expand Down Expand Up @@ -73,9 +77,7 @@ function wrap(fn: Function): RequestHandler | ErrorRequestHandler {
op: 'middleware',
});
res.once('finish', () => {
if (span) {
span.finish();
}
span.finish();
});
}
return fn.apply(this, arguments);
Expand Down
6 changes: 3 additions & 3 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ export function tracingHandler(): (
if (req.headers && isString(req.headers['sentry-trace'])) {
const span = Span.fromTraceparent(req.headers['sentry-trace'] as string);
if (span) {
const spanData = span.toJSON();
traceId = spanData.trace_id;
parentSpanId = spanData.span_id;
traceId = span.traceId;
parentSpanId = span.parentSpanId;
}
}

Expand All @@ -57,6 +56,7 @@ export function tracingHandler(): (
getCurrentHub().configureScope(scope => {
scope.setSpan(transaction);
});

// We also set __sentry_transaction on the response so people can grab the transaction there to add
// spans to it later.
(res as any).__sentry_transaction = transaction;
Expand Down