## Description This PR fixes logic to capture traces only when both end time and start time are available for a span. The hypothesis is that if a span is captured but its end time is missing, new relic will add current time as end time. This end time may be added after a few minutes. This is skewing the metrics for evaluation traces. The PR also does refactor of the traces code for general better readability. Fixes #`Issue Number` _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="@tag.Sanity" ### 🔍 Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!IMPORTANT] > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/8612102498> > Commit: `64b79261b44cc96c16429bf043565cc92d9d7992` > Cypress dashboard url: <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=8612102498&attempt=1" target="_blank">Click here!</a> > All cypress tests have passed 🎉🎉🎉 <!-- end of auto-generated comment: Cypress test results --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Unified span creation for telemetry with improved parameter handling and attribute management. - Enhanced span timing and nesting logic for better performance tracking. - **Chores** - Simplified telemetry tracing logic across various modules by standardizing the use of new span management functions. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
71 lines
2.6 KiB
JavaScript
71 lines
2.6 KiB
JavaScript
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";
|
|
import { W3CTraceContextPropagator } from "@opentelemetry/core";
|
|
|
|
const { newRelic } = getAppsmithConfigs();
|
|
const { applicationId, otlpEndpoint, otlpLicenseKey, otlpServiceName } =
|
|
newRelic;
|
|
|
|
const provider = new WebTracerProvider({
|
|
resource: new Resource({
|
|
[SemanticResourceAttributes.SERVICE_NAME]: otlpServiceName,
|
|
[SemanticResourceAttributes.SERVICE_INSTANCE_ID]: applicationId,
|
|
[SemanticResourceAttributes.SERVICE_VERSION]: "1.0.0",
|
|
}),
|
|
});
|
|
|
|
const newRelicExporter = new OTLPTraceExporter({
|
|
url: `${otlpEndpoint}/v1/traces`,
|
|
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,
|
|
},
|
|
);
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|
|
|
|
provider.addSpanProcessor(processor);
|
|
provider.register({
|
|
contextManager: new ZoneContextManager(),
|
|
propagator: new CustomW3CTraceContextPropagator(),
|
|
});
|
|
|
|
registerInstrumentations({
|
|
instrumentations: [],
|
|
});
|