2023-11-06 09:35:17 +00:00
|
|
|
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
|
|
|
import { WebTracerProvider } from "@opentelemetry/sdk-trace-web";
|
|
|
|
|
import { registerInstrumentations } from "@opentelemetry/instrumentation";
|
|
|
|
|
import { ZoneContextManager } from "@opentelemetry/context-zone";
|
|
|
|
|
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
|
|
|
|
|
import { Resource } from "@opentelemetry/resources";
|
|
|
|
|
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
|
|
|
|
|
import { getAppsmithConfigs } from "@appsmith/configs";
|
2023-11-24 07:39:02 +00:00
|
|
|
import { W3CTraceContextPropagator } from "@opentelemetry/core";
|
2023-11-06 09:35:17 +00:00
|
|
|
|
|
|
|
|
const { newRelic } = getAppsmithConfigs();
|
2023-11-24 07:39:02 +00:00
|
|
|
const { applicationId, otlpEndpoint, otlpLicenseKey, otlpServiceName } =
|
|
|
|
|
newRelic;
|
2023-11-06 09:35:17 +00:00
|
|
|
|
|
|
|
|
const provider = new WebTracerProvider({
|
|
|
|
|
resource: new Resource({
|
2023-11-24 07:39:02 +00:00
|
|
|
[SemanticResourceAttributes.SERVICE_NAME]: otlpServiceName,
|
2023-11-06 09:35:17 +00:00
|
|
|
[SemanticResourceAttributes.SERVICE_INSTANCE_ID]: applicationId,
|
|
|
|
|
[SemanticResourceAttributes.SERVICE_VERSION]: "1.0.0",
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const newRelicExporter = new OTLPTraceExporter({
|
2023-11-24 07:39:02 +00:00
|
|
|
url: `${otlpEndpoint}/v1/traces`,
|
2023-11-06 09:35:17 +00:00
|
|
|
headers: {
|
|
|
|
|
"api-key": otlpLicenseKey,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const processor = new BatchSpanProcessor(
|
|
|
|
|
newRelicExporter,
|
|
|
|
|
//Optional BatchSpanProcessor Configurations
|
|
|
|
|
{
|
|
|
|
|
// The maximum queue size. After the size is reached spans are dropped.
|
|
|
|
|
maxQueueSize: 100,
|
|
|
|
|
// The maximum batch size of every export. It must be smaller or equal to maxQueueSize.
|
|
|
|
|
maxExportBatchSize: 50,
|
|
|
|
|
// The interval between two consecutive exports
|
|
|
|
|
scheduledDelayMillis: 500,
|
|
|
|
|
// How long the export can run before it is cancelled
|
|
|
|
|
exportTimeoutMillis: 30000,
|
|
|
|
|
},
|
|
|
|
|
);
|
2023-11-24 07:39:02 +00:00
|
|
|
|
|
|
|
|
const W3C_OTLP_TRACE_HEADER = "traceparent";
|
|
|
|
|
const CUSTOM_OTLP_TRACE_HEADER = "traceparent-otlp";
|
|
|
|
|
//We are overriding the default header "traceparent" used for trace context because the browser
|
|
|
|
|
// agent shares the same header's distributed tracing
|
|
|
|
|
class CustomW3CTraceContextPropagator extends W3CTraceContextPropagator {
|
|
|
|
|
inject(context, carrier, setter) {
|
|
|
|
|
// Call the original inject method to get the default traceparent header
|
|
|
|
|
super.inject(context, carrier, setter);
|
|
|
|
|
|
|
|
|
|
// Modify the carrier to use a different header
|
|
|
|
|
if (carrier[W3C_OTLP_TRACE_HEADER]) {
|
|
|
|
|
carrier[CUSTOM_OTLP_TRACE_HEADER] = carrier[W3C_OTLP_TRACE_HEADER];
|
|
|
|
|
delete carrier[W3C_OTLP_TRACE_HEADER]; // Remove the original traceparent header
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 09:35:17 +00:00
|
|
|
provider.addSpanProcessor(processor);
|
|
|
|
|
provider.register({
|
|
|
|
|
contextManager: new ZoneContextManager(),
|
2023-11-24 07:39:02 +00:00
|
|
|
propagator: new CustomW3CTraceContextPropagator(),
|
2023-11-06 09:35:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
registerInstrumentations({
|
2024-04-09 08:55:46 +00:00
|
|
|
instrumentations: [],
|
2023-11-06 09:35:17 +00:00
|
|
|
});
|