Skip to content

Commit

Permalink
remove remote metrics from context, move to stats (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky authored Jan 3, 2023
1 parent fed489c commit 88c91c8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-insects-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': patch
---

Remove remote metrics from context and move to stats
3 changes: 2 additions & 1 deletion packages/browser/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { popSnippetWindowBuffer } from '../core/buffer/snippet'
import { ClassicIntegrationSource } from '../plugins/ajs-destination/types'
import { attachInspector } from '../core/inspector'
import { Stats } from '../core/stats'

export interface LegacyIntegrationConfiguration {
/* @deprecated - This does not indicate browser types anymore */
Expand Down Expand Up @@ -279,7 +280,7 @@ async function loadAnalytics(

const plugins = settings.plugins ?? []
const classicIntegrations = settings.classicIntegrations ?? []
Context.initRemoteMetrics(legacySettings.metrics)
Stats.initRemoteMetrics(legacySettings.metrics)

// needs to be flushed before plugins are registered
flushPreBuffer(analytics, preInitBuffer)
Expand Down
9 changes: 2 additions & 7 deletions packages/browser/src/core/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@ import {
} from '@segment/analytics-core'
import { SegmentEvent } from '../events/interfaces'
import { Stats } from '../stats'
import { MetricsOptions, RemoteMetrics } from '../stats/remote-metrics'

let _remoteMetrics: RemoteMetrics

export class Context extends CoreContext<SegmentEvent> {
static override system() {
return new this({ type: 'track', event: 'system' })
}
static initRemoteMetrics(options?: MetricsOptions) {
_remoteMetrics = new RemoteMetrics(options)
}
constructor(event: SegmentEvent, id?: string) {
super(event, id, new Stats(_remoteMetrics))
super(event, id, new Stats())
}
}

export { ContextCancelation }
export type { ContextFailedDelivery, SerializedContext, CancelationOptions }
9 changes: 5 additions & 4 deletions packages/browser/src/core/stats/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Stats } from '..'
import { RemoteMetrics } from '../remote-metrics'
import { Stats } from '..'

const spy = jest.spyOn(RemoteMetrics.prototype, 'increment')

describe(Stats, () => {
test('forwards increments to remote metrics endpoint', () => {
const remote = new RemoteMetrics()
const spy = jest.spyOn(remote, 'increment')
Stats.initRemoteMetrics()

const stats = new Stats(remote)
const stats = new Stats()
stats.increment('banana', 1, ['phone:1'])

expect(spy).toHaveBeenCalledWith('banana', ['phone:1'])
Expand Down
11 changes: 7 additions & 4 deletions packages/browser/src/core/stats/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { CoreStats } from '@segment/analytics-core'
import type { RemoteMetrics } from './remote-metrics'
import { MetricsOptions, RemoteMetrics } from './remote-metrics'

let remoteMetrics: RemoteMetrics | undefined

export class Stats extends CoreStats {
constructor(private _remoteMetrics?: RemoteMetrics) {
super()
static initRemoteMetrics(options?: MetricsOptions) {
remoteMetrics = new RemoteMetrics(options)
}

override increment(metric: string, by?: number, tags?: string[]): void {
super.increment(metric, by, tags)
this._remoteMetrics?.increment(metric, tags ?? [])
remoteMetrics?.increment(metric, tags ?? [])
}
}

0 comments on commit 88c91c8

Please sign in to comment.