@embrace-io/react-native-tracer-provider
Version:
A React Native for the Embrace SDK that conforms to the OpenTelemetry TracerProvider interface
64 lines (63 loc) • 3.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmbraceNativeTracer = void 0;
const api_1 = require("@opentelemetry/api");
const util_1 = require("./util");
const TracerProviderModule_1 = require("./TracerProviderModule");
const EmbraceNativeSpan_1 = require("./EmbraceNativeSpan");
/**
* EmbraceNativeTracer implements a Tracer over the native Embrace Android and iOS SDKs.
*
* Communication with the native modules is asynchronous while the @opentelemetry/api interfaces are synchronous so spans
* are returned immediately as simple objects that contain an ID for further communication with the native side.
*
* Since the notion of active context differs on the native side this tracer has its own context manager to handle the
* active context on the JS side which can optionally be configured as the global context manager.
*
* 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 EmbraceNativeTracer {
constructor(contextManager, spanContextSyncBehaviour, name, version, schemaUrl) {
this.name = name;
this.version = version;
this.schemaUrl = schemaUrl;
this.contextManager = contextManager;
this.spanContextSyncBehaviour = spanContextSyncBehaviour;
}
startSpan(name, options = {}, context) {
const { kind, attributes, links, startTime, root } = options;
const parentSpan = api_1.trace.getSpan(context || this.contextManager.active());
const parentNativeID = (!root && parentSpan && parentSpan.nativeID()) || "";
const nativeSpan = new EmbraceNativeSpan_1.EmbraceNativeSpan(this.name, this.version, this.schemaUrl, this.spanContextSyncBehaviour);
if (links && links.length) {
(0, util_1.logWarning)("Adding span links is not currently supported by the Embrace SDK");
}
nativeSpan.creatingNativeSide(TracerProviderModule_1.TracerProviderModule.startSpan(this.name, this.version, this.schemaUrl, nativeSpan.nativeID(), name, kind ? api_1.SpanKind[kind] : "", (0, util_1.normalizeTime)(startTime), (0, util_1.normalizeAttributes)(attributes), (0, util_1.normalizeLinks)(links), parentNativeID));
return nativeSpan;
}
startActiveSpan(name, arg2, arg3, arg4) {
let opts;
let ctx;
let fn;
if (arguments.length < 2) {
return;
}
else if (arguments.length === 2) {
fn = arg2;
}
else if (arguments.length === 3) {
opts = arg2;
fn = arg3;
}
else {
opts = arg2;
ctx = arg3;
fn = arg4;
}
const parentContext = ctx !== null && ctx !== void 0 ? ctx : this.contextManager.active();
const span = this.startSpan(name, opts, parentContext);
const contextWithSpanSet = api_1.trace.setSpan(parentContext, span);
return this.contextManager.with(contextWithSpanSet, fn, undefined, span);
}
}
exports.EmbraceNativeTracer = EmbraceNativeTracer;