Skip to content
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

chore: refactor session handling #902

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .size-limit.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ module.exports = [

{
name: 'artifacts/splunk-otel-web.js',
limit: '40 kB',
limit: '41 kB',
path: './packages/web/dist/artifacts/splunk-otel-web.js',
},

{
name: 'artifacts/splunk-otel-web.js',
limit: '72 kB',
limit: '73 kB',
path: './packages/web/dist/artifacts/splunk-otel-web-legacy.js',
},

Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/SessionBasedSampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { Context, Link, Sampler, SamplingResult, SpanAttributes, SpanKind } from '@opentelemetry/api'
import { AlwaysOffSampler, AlwaysOnSampler } from '@opentelemetry/core'
import { getRumSessionId } from './session'
import { SessionProvider } from './services/session-service'

export interface SessionBasedSamplerConfig {
/**
Expand Down Expand Up @@ -75,7 +75,7 @@ export class SessionBasedSampler implements Sampler {
// Implementation based on @opentelemetry/core TraceIdRatioBasedSampler
// but replacing deciding based on traceId with sessionId
// (not extended from due to private methods)
const currentSession = getRumSessionId()
const currentSession = SessionProvider.sessionId
if (this._currentSession !== currentSession) {
this._currentSessionSampled = this._accumulate(currentSession) < this._upperBound
this._currentSession = currentSession
Expand Down
8 changes: 1 addition & 7 deletions packages/web/src/SplunkContextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@
import { Context, ContextManager, ROOT_CONTEXT } from '@opentelemetry/api'
import { unwrap } from 'shimmer'
import { getOriginalFunction, isFunction, wrapNatively } from './utils'

export interface ContextManagerConfig {
/** Enable async tracking of span parents */
async?: boolean
onBeforeContextEnd?: () => void
onBeforeContextStart?: () => void
}
import { ContextManagerConfig } from './types'

type EventListenerWithOrig = EventListener & { _orig?: EventListener }

Expand Down
13 changes: 12 additions & 1 deletion packages/web/src/SplunkLongTaskInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@
import { InstrumentationBase, InstrumentationConfig } from '@opentelemetry/instrumentation'

import { VERSION } from './version'
import { SplunkOtelWebConfig } from './types'
import { SessionProvider } from './services/session-service'

const LONGTASK_PERFORMANCE_TYPE = 'longtask'
const MODULE_NAME = 'splunk-longtask'

export class SplunkLongTaskInstrumentation extends InstrumentationBase {
private _longtaskObserver: PerformanceObserver | undefined

constructor(config: InstrumentationConfig = {}) {
private initOptions: SplunkOtelWebConfig

constructor(config: InstrumentationConfig = {}, initOptions: SplunkOtelWebConfig) {
super(MODULE_NAME, VERSION, Object.assign({}, config))

this.initOptions = initOptions
}

disable(): void {
Expand All @@ -52,6 +58,11 @@ export class SplunkLongTaskInstrumentation extends InstrumentationBase {
init(): void {}

private _createSpanFromEntry(entry: PerformanceEntry) {
if (!!this.initOptions._experimental_longtaskNoStartSession && !SessionProvider.sessionId) {
// session expired, we do not want to spawn new session from long tasks
return
}

const span = this.tracer.startSpan(LONGTASK_PERFORMANCE_TYPE, {
startTime: entry.startTime,
})
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/SplunkSpanAttributesProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { Attributes } from '@opentelemetry/api'
import { Span, SpanProcessor } from '@opentelemetry/sdk-trace-base'
import { getRumSessionId } from './session'
import { SessionProvider } from './services/session-service'

export class SplunkSpanAttributesProcessor implements SpanProcessor {
private readonly _globalAttributes: Attributes
Expand All @@ -42,7 +42,7 @@ export class SplunkSpanAttributesProcessor implements SpanProcessor {
onStart(span: Span): void {
span.setAttribute('location.href', location.href)
span.setAttributes(this._globalAttributes)
span.setAttribute('splunk.rumSessionId', getRumSessionId())
span.setAttribute('splunk.rumSessionId', SessionProvider.sessionId)
span.setAttribute('browser.instance.visibility_state', document.visibilityState)
}

Expand Down
124 changes: 0 additions & 124 deletions packages/web/src/cookie-session.ts

This file was deleted.

Loading
Loading