Note: This is an experimental package under active development. New releases may include breaking changes.
This package contains classes and utils that are common for web use cases.
npm install --save @opentelemetry/web-common
Sessions correlate multiple traces, events and logs that happen within a given time period. Sessions are represented as span/log attributes prefixed with the session. namespace. For additional information, see documentation in semantic conventions.
We provide a default implementation of managing sessions that:
Example:
const {
createSessionSpanProcessor,
createSessionLogRecordProcessor,
createDefaultSessionIdGenerator,
createSessionManager,
SessionManager,
DefaultIdGenerator,
LocalStorageSessionStore
} = require('@opentelemetry/web-common');
// session manager
const sessionManager = createSessionManager({
sessionIdGenerator: createDefaultSessionIdGenerator(),
sessionStore: new LocalStorageSessionStore(),
maxDuration: 7200, // 4 hours
inactivityTimeout: 1800 // 30 minutes
});
// restore or start session
sessionManager.start();
// configure tracer
const tracerProvider = new WebTracerProvider({
spanProcessors: [
createSessionSpanProcessor(sessionManager),
]
});
// configure logger
const loggerProvider = new LoggerProvider({
processors: [
createSessionLogRecordProcessor(sessionManager),
]
});
The above implementation can be customized by providing different implementations of SessionStore and SessionIdGenerator.
The SessionManager class provides a mechanism for observing sessions. This is useful when other components should be notified when a session is started or ended.
sessionManager.addObserver({
onSessionStarted: (newSession, previousSession) => {
console.log('Session started', newSession, previousSession);
},
onSessionEnded: (session) => {
console.log('Session ended', session);
}
});
If you require a completely custom solution for managing sessions, you can still use the processors that attach attributes to spans/logs. Here is an example:
function getSessionId() {
return 'abcd1234';
}
// configure tracer
const tracerProvider = new WebTracerProvider({
spanProcessors: [
createSessionSpanProcessor({
getSessionId: getSessionId
}),
]
});
// configure logger
const loggerProvider = new LoggerProvider({
processors: [
createSessionLogRecordProcessor({
getSessionId: getSessionId
}),
]
});
Apache 2.0 - See LICENSE for more information.