Skip to content
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
1 change: 1 addition & 0 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export {
setHttpStatus,
makeMultiplexedTransport,
MULTIPLEXED_TRANSPORT_EXTRA_KEY,
MULTIPLEXED_METRIC_ROUTING_KEY,
moduleMetadataIntegration,
supabaseIntegration,
instrumentSupabaseClient,
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ export { ServerRuntimeClient } from './server-runtime-client';
export { initAndBind, setCurrentClient } from './sdk';
export { createTransport } from './transports/base';
export { makeOfflineTransport } from './transports/offline';
export { makeMultiplexedTransport, MULTIPLEXED_TRANSPORT_EXTRA_KEY } from './transports/multiplexed';
export {
makeMultiplexedTransport,
MULTIPLEXED_TRANSPORT_EXTRA_KEY,
MULTIPLEXED_METRIC_ROUTING_KEY,
} from './transports/multiplexed';
export { getIntegrationsToSetup, addIntegration, defineIntegration, installedIntegrations } from './integration';
export {
_INTERNAL_skipAiProviderWrapping,
Expand Down
17 changes: 12 additions & 5 deletions packages/core/src/metrics/public-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Scope } from '../scope';
import type { Metric, MetricType } from '../types-hoist/metric';
import type { Metric, MetricRoutingInfo, MetricType } from '../types-hoist/metric';
import { _INTERNAL_captureMetric } from './internal';
import { MULTIPLEXED_METRIC_ROUTING_KEY } from '../transports/multiplexed';

/**
* Options for capturing a metric.
Expand All @@ -20,6 +21,12 @@ export interface MetricOptions {
* The scope to capture the metric with.
*/
scope?: Scope;

/**
* The routing information for multiplexed transport.
* Each metric can be sent to multiple DSNs.
*/
routing?: Array<MetricRoutingInfo>;
}

/**
Expand All @@ -31,10 +38,10 @@ export interface MetricOptions {
* @param options - Options for capturing the metric.
*/
function captureMetric(type: MetricType, name: string, value: number, options?: MetricOptions): void {
_INTERNAL_captureMetric(
{ type, name, value, unit: options?.unit, attributes: options?.attributes },
{ scope: options?.scope },
);
const attributes = options?.routing
? { ...options.attributes, [MULTIPLEXED_METRIC_ROUTING_KEY]: options.routing }
: options?.attributes;
_INTERNAL_captureMetric({ type, name, value, unit: options?.unit, attributes }, { scope: options?.scope });
}

/**
Expand Down
13 changes: 12 additions & 1 deletion packages/core/src/transports/multiplexed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Event } from '../types-hoist/event';
import type { BaseTransportOptions, Transport, TransportMakeRequestResponse } from '../types-hoist/transport';
import { dsnFromString } from '../utils/dsn';
import { createEnvelope, forEachEnvelopeItem } from '../utils/envelope';
import type { SerializedMetric } from '../types-hoist/metric';

interface MatchParam {
/** The envelope to be sent */
Expand All @@ -16,6 +17,7 @@ interface MatchParam {
* @param types Defaults to ['event']
*/
getEvent(types?: EnvelopeItemType[]): Event | undefined;
getMetric(): SerializedMetric | undefined;
}

type RouteTo = { dsn: string; release: string };
Expand All @@ -27,6 +29,8 @@ type Matcher = (param: MatchParam) => (string | RouteTo)[];
*/
export const MULTIPLEXED_TRANSPORT_EXTRA_KEY = 'MULTIPLEXED_TRANSPORT_EXTRA_KEY';

export const MULTIPLEXED_METRIC_ROUTING_KEY = 'sentry.routing';

/**
* Gets an event from an envelope.
*
Expand Down Expand Up @@ -109,6 +113,13 @@ export function makeMultiplexedTransport<TO extends BaseTransportOptions>(
) {
return event.extra[MULTIPLEXED_TRANSPORT_EXTRA_KEY];
}
const metric = args.getMetric();
if (
metric?.attributes?.[MULTIPLEXED_METRIC_ROUTING_KEY] &&
Array.isArray(metric.attributes[MULTIPLEXED_METRIC_ROUTING_KEY])
) {
return metric.attributes[MULTIPLEXED_METRIC_ROUTING_KEY] as RouteTo[];
}
return [];
});

Expand Down Expand Up @@ -142,7 +153,7 @@ export function makeMultiplexedTransport<TO extends BaseTransportOptions>(
return eventFromEnvelope(envelope, eventTypes);
}

const transports = actualMatcher({ envelope, getEvent })
const transports = actualMatcher({ envelope, getEvent, getMetric: () => undefined })
.map(result => {
if (typeof result === 'string') {
return getTransport(result, undefined);
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/types-hoist/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,23 @@ export interface SerializedMetric {

/**
* Arbitrary structured data that stores information about the metric.
* This can contain routing information via the `MULTIPLEXED_METRIC_ROUTING_KEY` key.
*/
attributes?: Attributes;
}

export type SerializedMetricContainer = {
items: Array<SerializedMetric>;
};

export interface MetricRoutingInfo {
/**
* The DSN of the Sentry project to send the metric to.
*/
dsn: string;

/**
* The release of the Sentry project to send the metric to.
*/
release?: string;
}
Loading