From bf2f2cf44eb98d5b70cb87fab5516695c31cc978 Mon Sep 17 00:00:00 2001 From: Daniel Jackins Date: Tue, 13 Feb 2024 01:56:26 -0700 Subject: [PATCH 1/4] allow custom metrics endpoint --- packages/browser/src/browser/index.ts | 9 ++++++++- packages/browser/src/core/analytics/index.ts | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/browser/src/browser/index.ts b/packages/browser/src/browser/index.ts index 0c2186e9c..95aab7061 100644 --- a/packages/browser/src/browser/index.ts +++ b/packages/browser/src/browser/index.ts @@ -353,7 +353,14 @@ async function loadAnalytics( const plugins = settings.plugins ?? [] const classicIntegrations = settings.classicIntegrations ?? [] - Stats.initRemoteMetrics(legacySettings.metrics) + + if (options.metricsEndpoint && legacySettings.metrics) { + legacySettings.metrics.host = options.metricsEndpoint + } + + Stats.initRemoteMetrics( + legacySettings.metrics ?? { host: options.metricsEndpoint } + ) // needs to be flushed before plugins are registered flushPreBuffer(analytics, preInitBuffer) diff --git a/packages/browser/src/core/analytics/index.ts b/packages/browser/src/core/analytics/index.ts index b396a8423..aee180711 100644 --- a/packages/browser/src/core/analytics/index.ts +++ b/packages/browser/src/core/analytics/index.ts @@ -106,6 +106,10 @@ export interface InitOptions { plan?: Plan retryQueue?: boolean obfuscate?: boolean + /** + * Allows you to change the metrics endpoint used + */ + metricsEndpoint?: string /** * This callback allows you to update/mutate CDN Settings. * This is called directly after settings are fetched from the CDN. From 2f803241339263a9abd19a2c4892ca8b12b0e4d5 Mon Sep 17 00:00:00 2001 From: Daniel Jackins Date: Tue, 13 Feb 2024 09:51:39 -0700 Subject: [PATCH 2/4] changeset --- .changeset/few-pets-tell.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/few-pets-tell.md diff --git a/.changeset/few-pets-tell.md b/.changeset/few-pets-tell.md new file mode 100644 index 000000000..6fb136b2d --- /dev/null +++ b/.changeset/few-pets-tell.md @@ -0,0 +1,5 @@ +--- +'@segment/analytics-next': minor +--- + +Allow custom metrics endpoint From 29846464c57cbc36186d7cf057d571dd006d8e3a Mon Sep 17 00:00:00 2001 From: Daniel Jackins Date: Tue, 5 Mar 2024 01:09:23 -0700 Subject: [PATCH 3/4] set host with load options, respect protocol for metrics --- .changeset/few-pets-tell.md | 2 +- .../src/browser/__tests__/integration.test.ts | 38 +++++++++++++++++++ packages/browser/src/browser/index.ts | 16 ++++---- packages/browser/src/core/analytics/index.ts | 4 -- .../browser/src/core/stats/remote-metrics.ts | 5 ++- 5 files changed, 52 insertions(+), 13 deletions(-) diff --git a/.changeset/few-pets-tell.md b/.changeset/few-pets-tell.md index 6fb136b2d..38a9874a9 100644 --- a/.changeset/few-pets-tell.md +++ b/.changeset/few-pets-tell.md @@ -2,4 +2,4 @@ '@segment/analytics-next': minor --- -Allow custom metrics endpoint +Allow custom metrics endpoint on load diff --git a/packages/browser/src/browser/__tests__/integration.test.ts b/packages/browser/src/browser/__tests__/integration.test.ts index 5afc6d344..02ec2aee9 100644 --- a/packages/browser/src/browser/__tests__/integration.test.ts +++ b/packages/browser/src/browser/__tests__/integration.test.ts @@ -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[] = [] @@ -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', () => { diff --git a/packages/browser/src/browser/index.ts b/packages/browser/src/browser/index.ts index 95aab7061..774bb58ed 100644 --- a/packages/browser/src/browser/index.ts +++ b/packages/browser/src/browser/index.ts @@ -354,13 +354,15 @@ async function loadAnalytics( const classicIntegrations = settings.classicIntegrations ?? [] - if (options.metricsEndpoint && legacySettings.metrics) { - legacySettings.metrics.host = options.metricsEndpoint - } - - Stats.initRemoteMetrics( - legacySettings.metrics ?? { host: options.metricsEndpoint } - ) + 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) diff --git a/packages/browser/src/core/analytics/index.ts b/packages/browser/src/core/analytics/index.ts index aee180711..b396a8423 100644 --- a/packages/browser/src/core/analytics/index.ts +++ b/packages/browser/src/core/analytics/index.ts @@ -106,10 +106,6 @@ export interface InitOptions { plan?: Plan retryQueue?: boolean obfuscate?: boolean - /** - * Allows you to change the metrics endpoint used - */ - metricsEndpoint?: string /** * This callback allows you to update/mutate CDN Settings. * This is called directly after settings are fetched from the CDN. diff --git a/packages/browser/src/core/stats/remote-metrics.ts b/packages/browser/src/core/stats/remote-metrics.ts index 65c23aa1a..dcbac94a4 100644 --- a/packages/browser/src/core/stats/remote-metrics.ts +++ b/packages/browser/src/core/stats/remote-metrics.ts @@ -8,6 +8,7 @@ export interface MetricsOptions { sampleRate?: number flushTimer?: number maxQueueSize?: number + protocol?: string } /** @@ -56,6 +57,7 @@ export class RemoteMetrics { private host: string private flushTimer: number private maxQueueSize: number + private protocol: string sampleRate: number queue: RemoteMetric[] @@ -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 = [] @@ -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, From 9baea6dbb8768e6c46c17a4c60301e407782614a Mon Sep 17 00:00:00 2001 From: Daniel Jackins Date: Tue, 5 Mar 2024 11:48:03 -0700 Subject: [PATCH 4/4] narrow protocol type --- packages/browser/src/core/stats/remote-metrics.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/browser/src/core/stats/remote-metrics.ts b/packages/browser/src/core/stats/remote-metrics.ts index dcbac94a4..82f5bfacf 100644 --- a/packages/browser/src/core/stats/remote-metrics.ts +++ b/packages/browser/src/core/stats/remote-metrics.ts @@ -8,7 +8,7 @@ export interface MetricsOptions { sampleRate?: number flushTimer?: number maxQueueSize?: number - protocol?: string + protocol?: 'http' | 'https' } /**