diff --git a/sdk/core/core-tracing/.tshy/browser.json b/sdk/core/core-tracing/.tshy/browser.json index 32e74e04ec62..58163fb8398e 100644 --- a/sdk/core/core-tracing/.tshy/browser.json +++ b/sdk/core/core-tracing/.tshy/browser.json @@ -5,7 +5,9 @@ "../src/**/*.mts", "../src/**/*.tsx" ], - "exclude": [], + "exclude": [ + ".././src/state-cjs.cts" + ], "compilerOptions": { "outDir": "../.tshy-build/browser" } diff --git a/sdk/core/core-tracing/.tshy/commonjs.json b/sdk/core/core-tracing/.tshy/commonjs.json index 5ace94d041f3..82ad9f8e077e 100644 --- a/sdk/core/core-tracing/.tshy/commonjs.json +++ b/sdk/core/core-tracing/.tshy/commonjs.json @@ -6,7 +6,8 @@ "../src/**/*.tsx" ], "exclude": [ - "../src/**/*.mts" + "../src/**/*.mts", + "../src/state-browser.mts" ], "compilerOptions": { "outDir": "../.tshy-build/commonjs" diff --git a/sdk/core/core-tracing/.tshy/esm.json b/sdk/core/core-tracing/.tshy/esm.json index ff5264e692d1..3fe24fbc7772 100644 --- a/sdk/core/core-tracing/.tshy/esm.json +++ b/sdk/core/core-tracing/.tshy/esm.json @@ -5,7 +5,10 @@ "../src/**/*.mts", "../src/**/*.tsx" ], - "exclude": [], + "exclude": [ + ".././src/state-cjs.cts", + ".././src/state-browser.mts" + ], "compilerOptions": { "outDir": "../.tshy-build/esm" } diff --git a/sdk/core/core-tracing/.tshy/react-native.json b/sdk/core/core-tracing/.tshy/react-native.json index f431a06985d8..289dea78b362 100644 --- a/sdk/core/core-tracing/.tshy/react-native.json +++ b/sdk/core/core-tracing/.tshy/react-native.json @@ -5,7 +5,10 @@ "../src/**/*.mts", "../src/**/*.tsx" ], - "exclude": [], + "exclude": [ + ".././src/state-cjs.cts", + ".././src/state-browser.mts" + ], "compilerOptions": { "outDir": "../.tshy-build/react-native" } diff --git a/sdk/core/core-tracing/src/instrumenter.ts b/sdk/core/core-tracing/src/instrumenter.ts index f591634bf520..265b2cb4ce49 100644 --- a/sdk/core/core-tracing/src/instrumenter.ts +++ b/sdk/core/core-tracing/src/instrumenter.ts @@ -7,7 +7,9 @@ import { TracingContext, TracingSpan, } from "./interfaces.js"; + import { createTracingContext } from "./tracingContext.js"; +import { state } from "./state.js"; export function createDefaultTracingSpan(): TracingSpan { return { @@ -57,16 +59,13 @@ export function createDefaultInstrumenter(): Instrumenter { }; } -/** @internal */ -let instrumenterImplementation: Instrumenter | undefined; - /** * Extends the Azure SDK with support for a given instrumenter implementation. * * @param instrumenter - The instrumenter implementation to use. */ export function useInstrumenter(instrumenter: Instrumenter): void { - instrumenterImplementation = instrumenter; + state.instrumenterImplementation = instrumenter; } /** @@ -75,8 +74,8 @@ export function useInstrumenter(instrumenter: Instrumenter): void { * @returns The currently set instrumenter */ export function getInstrumenter(): Instrumenter { - if (!instrumenterImplementation) { - instrumenterImplementation = createDefaultInstrumenter(); + if (!state.instrumenterImplementation) { + state.instrumenterImplementation = createDefaultInstrumenter(); } - return instrumenterImplementation; + return state.instrumenterImplementation; } diff --git a/sdk/core/core-tracing/src/state-browser.mts b/sdk/core/core-tracing/src/state-browser.mts new file mode 100644 index 000000000000..4210e537a181 --- /dev/null +++ b/sdk/core/core-tracing/src/state-browser.mts @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import { Instrumenter } from "./interfaces.js"; + +/** + * Browser-only implementation of the module's state. The browser esm variant will not load the commonjs state, so we do not need to share state between the two. + */ +export const state = { + instrumenterImplementation: undefined as Instrumenter | undefined, +}; diff --git a/sdk/core/core-tracing/src/state-cjs.cts b/sdk/core/core-tracing/src/state-cjs.cts new file mode 100644 index 000000000000..634a71849fdf --- /dev/null +++ b/sdk/core/core-tracing/src/state-cjs.cts @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +/** + * @internal + * + * Holds the singleton instrumenter, to be shared across CJS and ESM imports. + */ +export const state = { + instrumenterImplementation: undefined, +}; diff --git a/sdk/core/core-tracing/src/state.ts b/sdk/core/core-tracing/src/state.ts new file mode 100644 index 000000000000..a5c509621d0f --- /dev/null +++ b/sdk/core/core-tracing/src/state.ts @@ -0,0 +1,14 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import { Instrumenter } from "./interfaces.js"; +// @ts-expect-error The recommended approach to sharing module state between ESM and CJS. +// See https://github.com/isaacs/tshy/blob/main/README.md#module-local-state for additional information. +import { state as cjsState } from "../commonjs/state.js"; + +/** + * Defines the shared state between CJS and ESM by re-exporting the CJS state. + */ +export const state = cjsState as { + instrumenterImplementation: Instrumenter | undefined; +}; diff --git a/sdk/core/core-tracing/vitest.config.ts b/sdk/core/core-tracing/vitest.config.ts index f46a08861fab..05261d85a6eb 100644 --- a/sdk/core/core-tracing/vitest.config.ts +++ b/sdk/core/core-tracing/vitest.config.ts @@ -2,6 +2,7 @@ // Licensed under the MIT license. import { defineConfig } from "vitest/config"; +import { resolve } from "node:path"; export default defineConfig({ test: { @@ -13,6 +14,9 @@ export default defineConfig({ toFake: ["setTimeout", "Date"], }, watch: false, + alias: { + "../commonjs/state.js": resolve("./src/state-cjs.cts"), + }, include: ["test/**/*.spec.ts"], exclude: ["test/**/browser/*.spec.ts"], coverage: {