-
Notifications
You must be signed in to change notification settings - Fork 837
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(web): Add session handling implementation #5173
Open
martinkuba
wants to merge
2
commits into
open-telemetry:main
Choose a base branch
from
martinkuba:session-manager
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Session Example</title> | ||
<base href="/"> | ||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
</head> | ||
|
||
<body> | ||
Example of using attaching session attributes to spans and logs. | ||
<script type="text/javascript" src="session.js"></script> | ||
<button id="button1">generate span</button> | ||
<button id="button2">generate log</button> | ||
</body> | ||
|
||
</html> |
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,69 @@ | ||
const { ConsoleSpanExporter, SimpleSpanProcessor } = require( '@opentelemetry/sdk-trace-base'); | ||
const { WebTracerProvider } = require( '@opentelemetry/sdk-trace-web'); | ||
const { | ||
LoggerProvider, | ||
SimpleLogRecordProcessor, | ||
ConsoleLogRecordExporter, | ||
} = require('@opentelemetry/sdk-logs'); | ||
const { | ||
createSessionSpanProcessor, | ||
createSessionLogRecordProcessor, | ||
Session, | ||
SessionManager, | ||
DefaultIdGenerator, | ||
LocalStorageSessionStore | ||
} = require('@opentelemetry/web-common'); | ||
|
||
// session manager | ||
const sessionManager = new SessionManager({ | ||
sessionIdGenerator: new DefaultIdGenerator(), | ||
sessionStore: new LocalStorageSessionStore(), | ||
maxDuration: 20, | ||
inactivityTimeout: 10 | ||
}); | ||
|
||
sessionManager.addObserver({ | ||
onSessionStarted: (newSession, previousSession) => { | ||
console.log('Session started', newSession, previousSession); | ||
}, | ||
onSessionEnded: (session) => { | ||
console.log('Session ended', session); | ||
} | ||
}); | ||
|
||
// configure tracer | ||
const provider = new WebTracerProvider(); | ||
provider.addSpanProcessor(createSessionSpanProcessor(sessionManager)); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); | ||
provider.register(); | ||
const tracer = provider.getTracer('example'); | ||
|
||
// configure logger | ||
const loggerProvider = new LoggerProvider(); | ||
loggerProvider.addLogRecordProcessor(createSessionLogRecordProcessor(sessionManager)); | ||
loggerProvider.addLogRecordProcessor( | ||
new SimpleLogRecordProcessor(new ConsoleLogRecordExporter()) | ||
); | ||
const logger = loggerProvider.getLogger('example'); | ||
|
||
window.addEventListener('load', () => { | ||
document.getElementById('button1').addEventListener('click', generateSpan); | ||
document.getElementById('button2').addEventListener('click', generateLog); | ||
}); | ||
|
||
function generateSpan() { | ||
const span = tracer.startSpan('foo'); | ||
span.setAttribute('key', 'value'); | ||
span.end(); | ||
} | ||
|
||
function generateLog() { | ||
logger.emit({ | ||
attributes: { | ||
name: 'my-event', | ||
}, | ||
body: { | ||
key1: 'val1' | ||
} | ||
}); | ||
} |
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
38 changes: 38 additions & 0 deletions
38
experimental/packages/web-common/src/DefaultIdGenerator.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,38 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { SessionIdGenerator } from './types/SessionIdGenerator'; | ||
|
||
export class DefaultIdGenerator implements SessionIdGenerator { | ||
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. same question here - if users can use composition instead, I'd advocate against exporting the class to make future changes easier. 🙂 |
||
generateSessionId = getIdGenerator(16); | ||
} | ||
|
||
const SHARED_CHAR_CODES_ARRAY = Array(32); | ||
function getIdGenerator(bytes: number): () => string { | ||
return function generateId() { | ||
for (let i = 0; i < bytes * 2; i++) { | ||
SHARED_CHAR_CODES_ARRAY[i] = Math.floor(Math.random() * 16) + 48; | ||
// valid hex characters in the range 48-57 and 97-102 | ||
if (SHARED_CHAR_CODES_ARRAY[i] >= 58) { | ||
SHARED_CHAR_CODES_ARRAY[i] += 39; | ||
} | ||
} | ||
return String.fromCharCode.apply( | ||
null, | ||
SHARED_CHAR_CODES_ARRAY.slice(0, bytes * 2) | ||
); | ||
}; | ||
} |
34 changes: 34 additions & 0 deletions
34
experimental/packages/web-common/src/LocalStorageSessionStore.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,34 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Session } from './types/Session'; | ||
import { SessionStore } from './types/SessionStore'; | ||
|
||
const SESSION_STORAGE_KEY = 'opentelemetry-session'; | ||
|
||
export class LocalStorageSessionStore implements SessionStore { | ||
save(session: Session): void { | ||
localStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(session)); | ||
} | ||
|
||
get(): Session | null { | ||
const sessionData = localStorage.getItem(SESSION_STORAGE_KEY); | ||
if (sessionData) { | ||
return JSON.parse(sessionData) as Session; | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.