@embrace-io/react-native-tracer-provider
Version:
A React Native for the Embrace SDK that conforms to the OpenTelemetry TracerProvider interface
45 lines (44 loc) • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmbraceNativeTracerProvider = void 0;
const react_native_1 = require("react-native");
const api_1 = require("@opentelemetry/api");
const util_1 = require("./util");
const TracerProviderModule_1 = require("./TracerProviderModule");
const StackContextManager_1 = require("./StackContextManager");
const EmbraceNativeTracer_1 = require("./EmbraceNativeTracer");
/**
* EmbraceNativeTracerProvider implements a TracerProvider over the native Embrace Android and iOS SDKs.
* Thin wrapped objects representing Tracers and Spans are maintained at the JS level and use Native Modules to
* call down to the SDKs to perform the actual operations on them.
*
* The JS side of this implementation is modelled after [opentelemetry-sdk-trace-base](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-sdk-trace-base)
*/
class EmbraceNativeTracerProvider {
constructor(config = {
setGlobalContextManager: true,
}) {
this.contextManager = new StackContextManager_1.StackContextManager();
this.contextManager.enable();
if (config.setGlobalContextManager) {
api_1.context.setGlobalContextManager(this.contextManager);
}
this.spanContextSyncBehaviour =
config.spanContextSyncBehaviour || "return_empty";
react_native_1.AppState.addEventListener("change", () => {
// Embrace ends the current session when the app switches between foreground and background, at that point
// we can clear any completed spans from memory as they won't be valid to reference anymore
TracerProviderModule_1.TracerProviderModule.clearCompletedSpans();
});
}
getTracer(name, version, options) {
const schemaUrl = (options === null || options === void 0 ? void 0 : options.schemaUrl) || "";
const tracerVersion = version || "";
if (schemaUrl && react_native_1.Platform.OS === "ios") {
(0, util_1.logWarning)("`schemaUrl` is ignored when running on iOS");
}
TracerProviderModule_1.TracerProviderModule.setupTracer(name, tracerVersion, schemaUrl);
return new EmbraceNativeTracer_1.EmbraceNativeTracer(this.contextManager, this.spanContextSyncBehaviour, name, tracerVersion, schemaUrl);
}
}
exports.EmbraceNativeTracerProvider = EmbraceNativeTracerProvider;