Skip to content

Commit

Permalink
Allow custom metrics endpoint (#1037)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Jackins <djackins@twilio.com>
  • Loading branch information
danieljackins and danieljackins authored Mar 5, 2024
1 parent a9b99ad commit e435279
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-pets-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': minor
---

Allow custom metrics endpoint on load
38 changes: 38 additions & 0 deletions packages/browser/src/browser/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
lowEntropyTestData,
} from '../../test-helpers/fixtures/client-hints'
import { getGlobalAnalytics, NullAnalytics } from '../..'
import { recordIntegrationMetric } from '../../core/stats/metric-helpers'

let fetchCalls: ReturnType<typeof parseFetchCall>[] = []

Expand Down Expand Up @@ -655,6 +656,43 @@ describe('Dispatch', () => {
]
`)
})

it('respects api and protocol overrides for metrics endpoint', async () => {
const [ajs] = await AnalyticsBrowser.load(
{
writeKey,
cdnSettings: {
integrations: {
'Segment.io': {
apiHost: 'cdnSettings.api.io',
},
},
metrics: {
flushTimer: 0,
},
},
},
{
integrations: {
'Segment.io': {
apiHost: 'new.api.io',
protocol: 'http',
},
},
}
)

const event = await ajs.track('foo')

recordIntegrationMetric(event, {
integrationName: 'foo',
methodName: 'bar',
type: 'action',
})

await sleep(10)
expect(fetchCalls[1].url).toBe('http://new.api.io/m')
})
})

describe('Group', () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/browser/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,16 @@ async function loadAnalytics(
const plugins = settings.plugins ?? []

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

const segmentLoadOptions = options.integrations?.['Segment.io'] as
| SegmentioSettings
| undefined

Stats.initRemoteMetrics({
...legacySettings.metrics,
host: segmentLoadOptions?.apiHost ?? legacySettings.metrics?.host,
protocol: segmentLoadOptions?.protocol,
})

// needs to be flushed before plugins are registered
flushPreBuffer(analytics, preInitBuffer)
Expand Down
5 changes: 4 additions & 1 deletion packages/browser/src/core/stats/remote-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface MetricsOptions {
sampleRate?: number
flushTimer?: number
maxQueueSize?: number
protocol?: 'http' | 'https'
}

/**
Expand Down Expand Up @@ -56,6 +57,7 @@ export class RemoteMetrics {
private host: string
private flushTimer: number
private maxQueueSize: number
private protocol: string

sampleRate: number
queue: RemoteMetric[]
Expand All @@ -65,6 +67,7 @@ export class RemoteMetrics {
this.sampleRate = options?.sampleRate ?? 1
this.flushTimer = options?.flushTimer ?? 30 * 1000 /* 30s */
this.maxQueueSize = options?.maxQueueSize ?? 20
this.protocol = options?.protocol ?? 'https'

this.queue = []

Expand Down Expand Up @@ -130,7 +133,7 @@ export class RemoteMetrics {
this.queue = []

const headers = { 'Content-Type': 'text/plain' }
const url = `https://${this.host}/m`
const url = `${this.protocol}://${this.host}/m`

return fetch(url, {
headers,
Expand Down

0 comments on commit e435279

Please sign in to comment.