const { Resource } = require("@opentelemetry/resources");
const { SimpleSpanProcessor } = require("@opentelemetry/sdk-trace-base");
const {
SemanticResourceAttributes,
} = require("@opentelemetry/semantic-conventions");
const { trace } = require("@opentelemetry/api");
const {
OTLPTraceExporter,
} = require("@opentelemetry/exporter-trace-otlp-http");
const { NodeSDK } = require("@opentelemetry/sdk-node");
const { HttpInstrumentation } = require("@opentelemetry/instrumentation-http");
const {
getNodeAutoInstrumentations,
} = require("@opentelemetry/auto-instrumentations-node");
const { B3Propagator } = require("@opentelemetry/propagator-b3");
const constant = require("./utilityConstant")
function createExporter(env) {
console.log("jeager starting on server env " + env);
let jaegerUrl = constant.jaegerUrl[env];
console.log("url " + jaegerUrl)
return new OTLPTraceExporter({
url: jaegerUrl,
});
}
const getTracer = (service_name) => {
console.log("This is my service " + service_name);
return trace.getTracer(service_name);
};
const startTracing = (serviceName, version, config) => {
console.log("starting tracing for service" + serviceName);
console.log(config.env);
exporter = createExporter(config.env);
sdk = new NodeSDK({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
[SemanticResourceAttributes.SERVICE_VERSION]: version,
}),
spanProcessor: new SimpleSpanProcessor(exporter),
traceExporter: exporter,
instrumentations: [new HttpInstrumentation()],
textMapPropagator: new B3Propagator(),
});
sdk.start();
};
I used this code without the startTracing function it works fine and returns traces but inside the function it doesn't work. The value of serviceName and version can not be accessed outside the function as the function will be executed in the end and SDK will be initialized earlier with no values and will not able to access the value of config.env.