-
Notifications
You must be signed in to change notification settings - Fork 26
feat(otel-web): emit browser.language and browser.timezone resource attributes #267
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
Open
teeohhem
wants to merge
3
commits into
main
Choose a base branch
from
teeohhem/rum-browser-context
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.
+132
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
5e9916a
feat(otel-web): emit browser.language and browser.timezone resource a…
teeohhem 67f7e07
test(otel-web): cover navigator.language and empty-string language br…
teeohhem 7c0b582
refactor(otel-web): namespace the custom timezone attr as hyperdx.bro…
teeohhem 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 hidden or 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,12 @@ | ||
| --- | ||
| '@hyperdx/otel-web': minor | ||
| '@hyperdx/browser': minor | ||
| --- | ||
|
|
||
| Emit approximate locale/region resource attributes from the browser RUM | ||
| SDK: `browser.language` (OTel semconv, from `navigator.language`) and | ||
| `hyperdx.browser.timezone` (IANA zone from `Intl`). These are honest proxies for | ||
| where a user is — they are NOT IP geolocation (the browser can't | ||
| determine country without a permission prompt; true geo is derived in the | ||
| collector from the client IP). Added before user-provided | ||
| `resourceAttributes` so callers can override them. |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "extension": ["ts"], | ||
| "spec": "test/nodejs.test.ts", | ||
| "spec": ["test/nodejs.test.ts", "test/browserContext.test.ts"], | ||
| "require": "ts-node/register" | ||
| } |
This file contains hidden or 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,55 @@ | ||
| import type { Attributes } from '@opentelemetry/api'; | ||
|
|
||
| /** | ||
| * Approximate locale/region signals read from the browser environment. | ||
| * These are HONEST PROXIES for where a user is — they are NOT IP | ||
| * geolocation (the browser cannot determine country without a permission | ||
| * prompt). True geo (`geo.country.name`, …) is derived in the OTel | ||
| * collector from the client IP. See the geoip processor. | ||
| */ | ||
| export interface BrowserContext { | ||
| /** navigator.language, e.g. "en-US" (OTel semconv `browser.language`). */ | ||
| language?: string; | ||
| /** IANA time zone, e.g. "America/New_York" (emitted as `hyperdx.browser.timezone`). */ | ||
| timeZone?: string; | ||
| } | ||
|
|
||
| /** Read the browser context from the current environment (best-effort). */ | ||
| export function resolveBrowserContext(): BrowserContext { | ||
| const language = | ||
| typeof navigator !== 'undefined' ? navigator.language : undefined; | ||
|
|
||
| let timeZone: string | undefined; | ||
| try { | ||
| timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone || undefined; | ||
| } catch { | ||
| // Intl or the resolved time zone is unavailable in this environment. | ||
| timeZone = undefined; | ||
| } | ||
|
|
||
| return { language, timeZone }; | ||
| } | ||
|
|
||
| /** | ||
| * Map a {@link BrowserContext} to resource attributes. `browser.language` | ||
| * is an OpenTelemetry semantic-convention attribute; `hyperdx.browser.timezone` | ||
| * is a vendor-namespaced custom attribute — OTel semconv has no timezone | ||
| * resource attribute, so it is kept out of the reserved `browser.` namespace | ||
| * to avoid a future collision. Absent values are omitted so they never | ||
| * overwrite a user-provided attribute with an empty string. | ||
| * | ||
| * The context is injectable so the mapping can be unit-tested without real | ||
| * browser globals. | ||
| */ | ||
| export function getBrowserContextResourceAttributes( | ||
| context: BrowserContext = resolveBrowserContext(), | ||
| ): Attributes { | ||
| const attrs: Attributes = {}; | ||
| if (context.language) { | ||
| attrs['browser.language'] = context.language; | ||
| } | ||
| if (context.timeZone) { | ||
| attrs['hyperdx.browser.timezone'] = context.timeZone; | ||
| } | ||
| return attrs; | ||
| } | ||
This file contains hidden or 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 hidden or 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,59 @@ | ||
| import * as assert from 'assert'; | ||
|
|
||
| import { | ||
| getBrowserContextResourceAttributes, | ||
| resolveBrowserContext, | ||
| } from '../src/browserContext'; | ||
|
|
||
| describe('browserContext', () => { | ||
| it('maps language and timeZone to resource attributes', () => { | ||
| assert.deepStrictEqual( | ||
| getBrowserContextResourceAttributes({ | ||
| language: 'en-US', | ||
| timeZone: 'America/New_York', | ||
| }), | ||
| { | ||
| 'browser.language': 'en-US', | ||
| 'hyperdx.browser.timezone': 'America/New_York', | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| it('omits absent values so it never overwrites with empty attributes', () => { | ||
| assert.deepStrictEqual(getBrowserContextResourceAttributes({}), {}); | ||
| assert.deepStrictEqual( | ||
| getBrowserContextResourceAttributes({ timeZone: 'UTC' }), | ||
| { 'hyperdx.browser.timezone': 'UTC' }, | ||
| ); | ||
| }); | ||
|
|
||
| it('treats an empty-string language as absent', () => { | ||
| assert.deepStrictEqual( | ||
| getBrowserContextResourceAttributes({ language: '' }), | ||
| {}, | ||
| ); | ||
| }); | ||
|
|
||
| it('resolveBrowserContext reads a non-empty IANA time zone from the environment', () => { | ||
| const { timeZone } = resolveBrowserContext(); | ||
| assert.strictEqual(typeof timeZone, 'string'); | ||
| assert.ok((timeZone as string).length > 0); | ||
| }); | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
| it('resolveBrowserContext reads navigator.language when available', () => { | ||
| const original = Object.getOwnPropertyDescriptor(globalThis, 'navigator'); | ||
| Object.defineProperty(globalThis, 'navigator', { | ||
| value: { language: 'fr-FR' }, | ||
| configurable: true, | ||
| }); | ||
| try { | ||
| assert.strictEqual(resolveBrowserContext().language, 'fr-FR'); | ||
| } finally { | ||
| if (original) { | ||
| Object.defineProperty(globalThis, 'navigator', original); | ||
| } else { | ||
| delete (globalThis as any).navigator; | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
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.
Is this customized? I don't see that in the spec https://opentelemetry.io/docs/specs/semconv/resource/browser/
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.
Good catch — this was custom. The OTel browser resource semconv only defines
browser.brands/browser.platform/browser.mobile/browser.language/browser.user_agent; there's no timezone attribute. To avoid squatting in the OTel-reservedbrowser.namespace (and a possible future collision), I've renamed it tohyperdx.browser.timezonein 7c0b582.browser.languagestays as-is since it's the spec attribute.