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

Allow custom metrics endpoint #1037

Merged
merged 4 commits into from
Mar 5, 2024
Merged
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
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 @@ -353,7 +353,16 @@ async function loadAnalytics(
const plugins = settings.plugins ?? []

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

const segmentLoadOptions = options.integrations?.['Segment.io'] as
silesky marked this conversation as resolved.
Show resolved Hide resolved
| 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
Loading