-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(web-tracing): attach user attributes to spans (#990)
- Loading branch information
1 parent
f49ac88
commit 76b8578
Showing
7 changed files
with
161 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,7 @@ export { | |
isToString, | ||
isTypeof, | ||
isUndefined, | ||
isEmpty, | ||
InternalLoggerLevel, | ||
LogLevel, | ||
noop, | ||
|
50 changes: 50 additions & 0 deletions
50
packages/web-tracing/src/faroMetaAttributesSpanProcessor.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { FaroMetaAttributesSpanProcessor } from './faroMetaAttributesSpanProcessor'; | ||
|
||
describe('faroMetaAttributesSpanProcessor', () => { | ||
const processor = new FaroMetaAttributesSpanProcessor( | ||
{ | ||
onStart: jest.fn(), | ||
onEnd: jest.fn(), | ||
shutdown: jest.fn(), | ||
forceFlush: jest.fn(), | ||
}, | ||
{ | ||
value: { | ||
session: { | ||
id: 'session-id', | ||
}, | ||
user: { | ||
email: 'email', | ||
id: 'id', | ||
username: 'user-short-name', | ||
fullName: 'user-full-name', | ||
roles: 'admin, editor,viewer', | ||
hash: 'hash', | ||
}, | ||
}, | ||
add: jest.fn(), | ||
remove: jest.fn(), | ||
addListener: jest.fn(), | ||
removeListener: jest.fn(), | ||
} | ||
); | ||
|
||
it('adds attributes to span', () => { | ||
const span = { | ||
attributes: {}, | ||
}; | ||
|
||
processor.onStart(span as any, {} as any); | ||
|
||
expect(span.attributes).toStrictEqual({ | ||
'session.id': 'session-id', | ||
session_id: 'session-id', | ||
'user.email': 'email', | ||
'user.id': 'id', | ||
'user.full_name': 'user-full-name', | ||
'user.name': 'user-short-name', | ||
'user.roles': ['admin', 'editor', 'viewer'], | ||
'user.hash': 'hash', | ||
}); | ||
}); | ||
}); |
66 changes: 66 additions & 0 deletions
66
packages/web-tracing/src/faroMetaAttributesSpanProcessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import type { Context } from '@opentelemetry/api'; | ||
import type { ReadableSpan, Span, SpanProcessor } from '@opentelemetry/sdk-trace-web'; | ||
// False positive. Package can be resolved. | ||
// eslint-disable-next-line import/no-unresolved | ||
import { ATTR_SESSION_ID } from '@opentelemetry/semantic-conventions/incubating'; | ||
|
||
import type { Metas } from '@grafana/faro-web-sdk'; | ||
|
||
export class FaroMetaAttributesSpanProcessor implements SpanProcessor { | ||
constructor( | ||
private processor: SpanProcessor, | ||
private metas: Metas | ||
) {} | ||
|
||
forceFlush(): Promise<void> { | ||
return this.processor.forceFlush(); | ||
} | ||
|
||
onStart(span: Span, parentContext: Context): void { | ||
const session = this.metas.value.session; | ||
|
||
if (session?.id) { | ||
span.attributes[ATTR_SESSION_ID] = session.id; | ||
/** | ||
* @deprecated will be removed in the future and has been replaced by ATTR_SESSION_ID (session.id) | ||
*/ | ||
span.attributes['session_id'] = session.id; | ||
} | ||
|
||
const user = this.metas.value.user ?? {}; | ||
|
||
if (user.email) { | ||
span.attributes['user.email'] = user.email; | ||
} | ||
|
||
if (user.id) { | ||
span.attributes['user.id'] = user.id; | ||
} | ||
|
||
if (user.username) { | ||
span.attributes['user.name'] = user.username; | ||
} | ||
|
||
if (user.fullName) { | ||
span.attributes['user.full_name'] = user.fullName; | ||
} | ||
|
||
if (user.roles) { | ||
span.attributes['user.roles'] = user.roles.split(',').map((role) => role.trim()); | ||
} | ||
|
||
if (user.hash) { | ||
span.attributes['user.hash'] = user.hash; | ||
} | ||
|
||
this.processor.onStart(span, parentContext); | ||
} | ||
|
||
onEnd(span: ReadableSpan): void { | ||
this.processor.onEnd(span); | ||
} | ||
|
||
shutdown(): Promise<void> { | ||
return this.processor.shutdown(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters