|
| 1 | +const { |
| 2 | + NodeTracerConfig, |
| 3 | + NodeTracerProvider, |
| 4 | +} = require("@opentelemetry/sdk-trace-node"); |
| 5 | +const { |
| 6 | + BatchSpanProcessor, |
| 7 | + ConsoleSpanExporter, |
| 8 | + SDKRegistrationConfig, |
| 9 | + SimpleSpanProcessor, |
| 10 | +} = require("@opentelemetry/sdk-trace-base"); |
| 11 | +const { |
| 12 | + Instrumentation, |
| 13 | + registerInstrumentations, |
| 14 | +} = require("@opentelemetry/instrumentation"); |
| 15 | +const { awsLambdaDetector } = require("@opentelemetry/resource-detector-aws"); |
| 16 | +const { |
| 17 | + detectResourcesSync, |
| 18 | + envDetector, |
| 19 | + processDetector, |
| 20 | +} = require("@opentelemetry/resources"); |
| 21 | +const { |
| 22 | + AwsInstrumentation, |
| 23 | +} = require("@opentelemetry/instrumentation-aws-sdk"); |
| 24 | +const { |
| 25 | + AwsLambdaInstrumentation, |
| 26 | +} = require("@opentelemetry/instrumentation-aws-lambda"); |
| 27 | +const { diag, DiagConsoleLogger, DiagLogLevel } = require("@opentelemetry/api"); |
| 28 | +const { getEnv } = require("@opentelemetry/core"); |
| 29 | +const { |
| 30 | + OTLPTraceExporter, |
| 31 | +} = require("@opentelemetry/exporter-trace-otlp-proto"); |
| 32 | +const { |
| 33 | + MeterProvider, |
| 34 | + MeterProviderOptions, |
| 35 | +} = require("@opentelemetry/sdk-metrics"); |
| 36 | + |
| 37 | +function defaultConfigureInstrumentations() { |
| 38 | + // Use require statements for instrumentation to avoid having to have transitive dependencies on all the typescript |
| 39 | + // definitions. |
| 40 | + const { DnsInstrumentation } = require("@opentelemetry/instrumentation-dns"); |
| 41 | + const { |
| 42 | + ExpressInstrumentation, |
| 43 | + } = require("@opentelemetry/instrumentation-express"); |
| 44 | + const { |
| 45 | + GraphQLInstrumentation, |
| 46 | + } = require("@opentelemetry/instrumentation-graphql"); |
| 47 | + const { |
| 48 | + GrpcInstrumentation, |
| 49 | + } = require("@opentelemetry/instrumentation-grpc"); |
| 50 | + const { |
| 51 | + HapiInstrumentation, |
| 52 | + } = require("@opentelemetry/instrumentation-hapi"); |
| 53 | + const { |
| 54 | + HttpInstrumentation, |
| 55 | + } = require("@opentelemetry/instrumentation-http"); |
| 56 | + const { |
| 57 | + IORedisInstrumentation, |
| 58 | + } = require("@opentelemetry/instrumentation-ioredis"); |
| 59 | + const { KoaInstrumentation } = require("@opentelemetry/instrumentation-koa"); |
| 60 | + const { |
| 61 | + MongoDBInstrumentation, |
| 62 | + } = require("@opentelemetry/instrumentation-mongodb"); |
| 63 | + const { |
| 64 | + MySQLInstrumentation, |
| 65 | + } = require("@opentelemetry/instrumentation-mysql"); |
| 66 | + const { NetInstrumentation } = require("@opentelemetry/instrumentation-net"); |
| 67 | + const { PgInstrumentation } = require("@opentelemetry/instrumentation-pg"); |
| 68 | + const { |
| 69 | + RedisInstrumentation, |
| 70 | + } = require("@opentelemetry/instrumentation-redis"); |
| 71 | + return [ |
| 72 | + new DnsInstrumentation(), |
| 73 | + new ExpressInstrumentation(), |
| 74 | + new GraphQLInstrumentation(), |
| 75 | + new GrpcInstrumentation(), |
| 76 | + new HapiInstrumentation(), |
| 77 | + new HttpInstrumentation(), |
| 78 | + new IORedisInstrumentation(), |
| 79 | + new KoaInstrumentation(), |
| 80 | + new MongoDBInstrumentation(), |
| 81 | + new MySQLInstrumentation(), |
| 82 | + new NetInstrumentation(), |
| 83 | + new PgInstrumentation(), |
| 84 | + new RedisInstrumentation(), |
| 85 | + ]; |
| 86 | +} |
| 87 | + |
| 88 | +global.configureTracerProvider = function (tracerProvider) {}; |
| 89 | +global.configureTracer = function (defaultConfig) { |
| 90 | + return defaultConfig; |
| 91 | +}; |
| 92 | +global.configureSdkRegistration = function (defaultSdkRegistration) { |
| 93 | + return defaultSdkRegistration; |
| 94 | +}; |
| 95 | +global.configureMeter = function (defaultConfig) { |
| 96 | + return defaultConfig; |
| 97 | +}; |
| 98 | +global.configureMeterProvider = function (meterProvider) {}; |
| 99 | +global.configureInstrumentations = function () { |
| 100 | + return []; |
| 101 | +}; |
| 102 | + |
| 103 | +// configure lambda logging |
| 104 | +const logLevel = getEnv().OTEL_LOG_LEVEL; |
| 105 | +diag.setLogger(new DiagConsoleLogger(), logLevel); |
| 106 | + |
| 107 | +console.log("Registering OpenTelemetry"); |
| 108 | + |
| 109 | +// By default use OpenTelemetry context propagation |
| 110 | +let disableAwsContextPropagation = true; |
| 111 | +const sumoOtelDisableAwsContextPropagationVal = |
| 112 | + process.env.SUMO_OTEL_DISABLE_AWS_CONTEXT_PROPAGATION; |
| 113 | +if ( |
| 114 | + sumoOtelDisableAwsContextPropagationVal === "false" || |
| 115 | + sumoOtelDisableAwsContextPropagationVal === "False" |
| 116 | +) { |
| 117 | + disableAwsContextPropagation = false; |
| 118 | +} |
| 119 | + |
| 120 | +// For debug purposes only |
| 121 | +if (logLevel === DiagLogLevel.DEBUG) { |
| 122 | + console.log("Debug environment variables status"); |
| 123 | + console.log( |
| 124 | + "AWS_LAMBDA_EXEC_WRAPPER value ", |
| 125 | + process.env.AWS_LAMBDA_EXEC_WRAPPER |
| 126 | + ); |
| 127 | + console.log( |
| 128 | + "OTEL_RESOURCE_ATTRIBUTES value", |
| 129 | + process.env.OTEL_RESOURCE_ATTRIBUTES |
| 130 | + ); |
| 131 | + console.log("OTEL_SERVICE_NAME value", process.env.OTEL_SERVICE_NAME); |
| 132 | + console.log("OTEL_TRACES_SAMPLER value", process.env.OTEL_TRACES_SAMPLER); |
| 133 | + console.log( |
| 134 | + "SUMOLOGIC_HTTP_TRACES_ENDPOINT_URL value", |
| 135 | + process.env.SUMOLOGIC_HTTP_TRACES_ENDPOINT_URL |
| 136 | + ); |
| 137 | + console.log( |
| 138 | + "SUMO_OTEL_DISABLE_AWS_CONTEXT_PROPAGATION value", |
| 139 | + process.env.SUMO_OTEL_DISABLE_AWS_CONTEXT_PROPAGATION |
| 140 | + ); |
| 141 | +} |
| 142 | + |
| 143 | +const instrumentations = [ |
| 144 | + new AwsInstrumentation({ |
| 145 | + suppressInternalInstrumentation: true, |
| 146 | + }), |
| 147 | + new AwsLambdaInstrumentation({ |
| 148 | + disableAwsContextPropagation: disableAwsContextPropagation, |
| 149 | + }), |
| 150 | + ...(typeof configureInstrumentations === "function" |
| 151 | + ? configureInstrumentations |
| 152 | + : defaultConfigureInstrumentations)(), |
| 153 | +]; |
| 154 | + |
| 155 | +// Register instrumentations synchronously to ensure code is patched even before provider is ready. |
| 156 | +registerInstrumentations({ |
| 157 | + instrumentations, |
| 158 | +}); |
| 159 | + |
| 160 | +async function initializeProvider() { |
| 161 | + const resource = detectResourcesSync({ |
| 162 | + detectors: [awsLambdaDetector, envDetector, processDetector], |
| 163 | + }); |
| 164 | + |
| 165 | + let config = { |
| 166 | + resource, |
| 167 | + }; |
| 168 | + if (typeof configureTracer === "function") { |
| 169 | + config = configureTracer(config); |
| 170 | + } |
| 171 | + |
| 172 | + const tracerProvider = new NodeTracerProvider(config); |
| 173 | + if (typeof configureTracerProvider === "function") { |
| 174 | + configureTracerProvider(tracerProvider); |
| 175 | + } else { |
| 176 | + // defaults |
| 177 | + tracerProvider.addSpanProcessor( |
| 178 | + new BatchSpanProcessor(new OTLPTraceExporter()) |
| 179 | + ); |
| 180 | + } |
| 181 | + // logging for debug |
| 182 | + if (logLevel === DiagLogLevel.DEBUG) { |
| 183 | + tracerProvider.addSpanProcessor( |
| 184 | + new SimpleSpanProcessor(new ConsoleSpanExporter()) |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + let sdkRegistrationConfig = {}; |
| 189 | + if (typeof configureSdkRegistration === "function") { |
| 190 | + sdkRegistrationConfig = configureSdkRegistration(sdkRegistrationConfig); |
| 191 | + } |
| 192 | + tracerProvider.register(sdkRegistrationConfig); |
| 193 | + |
| 194 | + // Configure default meter provider (do not export metrics) |
| 195 | + let meterConfig = { |
| 196 | + resource, |
| 197 | + }; |
| 198 | + if (typeof configureMeter === "function") { |
| 199 | + meterConfig = configureMeter(meterConfig); |
| 200 | + } |
| 201 | + |
| 202 | + const meterProvider = new MeterProvider(meterConfig); |
| 203 | + if (typeof configureMeterProvider === "function") { |
| 204 | + configureMeterProvider(meterProvider); |
| 205 | + } |
| 206 | + |
| 207 | + // Re-register instrumentation with initialized provider. Patched code will see the update. |
| 208 | + registerInstrumentations({ |
| 209 | + instrumentations, |
| 210 | + tracerProvider, |
| 211 | + meterProvider, |
| 212 | + }); |
| 213 | +} |
| 214 | +initializeProvider(); |
0 commit comments