-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(flags): track feature flag evaluations in span attributes #16475
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
/* eslint-disable max-lines */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 304 lines after changes |
||
|
||
import { getClient, getCurrentScope } from '../currentScopes'; | ||
import { DEBUG_BUILD } from '../debug-build'; | ||
import { createSpanEnvelope } from '../envelope'; | ||
|
@@ -45,6 +47,8 @@ import { timedEventsToMeasurements } from './measurement'; | |
import { getCapturedScopesOnSpan } from './utils'; | ||
|
||
const MAX_SPAN_COUNT = 1000; | ||
const MAX_FEATURE_FLAGS_PER_SPAN = 10; // The maximum number of feature flag evaluations that can be recorded in span attributes. | ||
const FEATURE_FLAG_ATTRIBUTE_PREFIX = 'sentry.flag.evaluation.'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since users can call |
||
|
||
/** | ||
* Span contains all data about a span | ||
|
@@ -57,6 +61,7 @@ export class SentrySpan implements Span { | |
protected _name?: string | undefined; | ||
protected _attributes: SpanAttributes; | ||
protected _links?: SpanLink[]; | ||
protected _numFeatureFlags: number; | ||
/** Epoch timestamp in seconds when the span started. */ | ||
protected _startTime: number; | ||
/** Epoch timestamp in seconds when the span ended. */ | ||
|
@@ -81,6 +86,7 @@ export class SentrySpan implements Span { | |
this._spanId = spanContext.spanId || generateSpanId(); | ||
this._startTime = spanContext.startTimestamp || timestampInSeconds(); | ||
this._links = spanContext.links; | ||
this._numFeatureFlags = 0; | ||
|
||
this._attributes = {}; | ||
this.setAttributes({ | ||
|
@@ -132,6 +138,17 @@ export class SentrySpan implements Span { | |
return this; | ||
} | ||
|
||
/** @inheritDoc */ | ||
public addFeatureFlag(name: string, value: boolean): this { | ||
if (!(name in this._attributes)) { | ||
if (this._numFeatureFlags >= MAX_FEATURE_FLAGS_PER_SPAN) { | ||
return this; | ||
} | ||
Comment on lines
+144
to
+146
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be a buffer so that we can evict the least recently added flags (similar to what we do w/ breadcrumbs)? Silently ignoring the flag here feels not great. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's what we do on in python atm, cc @cmanallen. We could have a buffer but need to make sure the attribute is serializable - Doesn't look like SpanAttributeValue allows nested objects. Maybe we could store a buffer of keys internally There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For spans the oldest flag evaluations are the most important because they're likely to have the largest performance impact. We drop late arrivals due to request size concerns. |
||
this._numFeatureFlags++; | ||
} | ||
return this.setAttribute(`${FEATURE_FLAG_ATTRIBUTE_PREFIX}${name}`, value); | ||
} | ||
|
||
/** | ||
* This should generally not be used, | ||
* but it is needed for being compliant with the OTEL Span interface. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a new method onto a span breaks our compatibility with OTEL, which we leverage for duck typing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This strategy makes sense, but how can we track the num current flags? (currently a protected field on the span)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking we can expose this as an API for users, following the pattern we have for adding flags to scope in
featureFlagIntegration
. It'd be the same as theaddFeatureFlag
method, without the need for an integration