solarwinds-apm
Version:
OpenTelemetry-based SolarWinds APM library
134 lines (131 loc) • 4.8 kB
JavaScript
/*
Copyright 2023-2026 SolarWinds Worldwide, LLC.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { context, trace, } from "@opentelemetry/api";
import { environment } from "./env.js";
export function patch(configs, options, logger) {
for (const patcher of PATCHERS) {
patcher(configs, options);
}
logger.debug("patched instrumentation configs", configs);
return configs;
}
export function patchEnv(config, env = process.env) {
for (const patcher of ENV_PATCHERS) {
patcher(env, config);
}
}
function patcher(names, patch) {
return (configs, options) => {
for (const name of names) {
patch((configs[name] ??= {}), options);
}
};
}
function envPatcher(names, patch) {
return (env, config) => {
for (const name of names) {
env[name] = patch(env[name], config);
}
};
}
const PATCHERS = [
patcher(["@fastify/otel"], (config) => {
config.registerOnInitialization ??= true;
}),
patcher(["@opentelemetry/instrumentation-aws-lambda"], (config) => {
config.enabled ??= environment.IS_AWS_LAMBDA;
}),
patcher(["@opentelemetry/instrumentation-aws-sdk"], (config) => {
if (environment.IS_AWS_LAMBDA) {
config.enabled ??= true;
}
}),
patcher(["@opentelemetry/instrumentation-cassandra-driver"], (config) => {
config.enhancedDatabaseReporting ??= true;
}),
patcher(["@opentelemetry/instrumentation-fs"], (config) => {
config.enabled ??= false;
config.requireParentSpan ??= true;
}),
patcher(["@opentelemetry/instrumentation-http"], (config, options) => {
const original = config.responseHook;
config.responseHook = (span, response) => {
// only for server responses originating from the instrumented app
if ("setHeader" in response) {
const ctx = trace.setSpan(context.active(), span);
options.responsePropagator.inject(ctx, response, {
set: (res, k, v) => {
if (!res.hasHeader(k)) {
res.setHeader(k, v);
}
},
});
}
original?.(span, response);
};
}),
patcher(["@opentelemetry/instrumentation-mysql2"], (config, options) => {
config.addSqlCommenterCommentToQueries ??=
options.insertTraceContextIntoQueries;
}),
patcher(["@opentelemetry/instrumentation-pg"], (config, options) => {
config.requireParentSpan ??= true;
config.addSqlCommenterCommentToQueries ??=
options.insertTraceContextIntoQueries;
}),
patcher(["@opentelemetry/instrumentation-runtime-node"], (config, options) => {
config.enabled ??= options.runtimeMetrics;
}),
patcher([
"@opentelemetry/instrumentation-dns",
"@opentelemetry/instrumentation-net",
], (config) => {
config.enabled ??= false;
}),
patcher([
"@opentelemetry/instrumentation-bunyan",
"@opentelemetry/instrumentation-pino",
"@opentelemetry/instrumentation-winston",
], (config, options) => {
config.disableLogCorrelation ??= !options.insertTraceContextIntoLogs;
config.disableLogSending ??= !options.exportLogsEnabled;
const original = config.logHook;
config.logHook = (span, record) => {
record["resource.service.name"] ??= options.service;
original?.(span, record);
};
}),
];
const ENV_PATCHERS = [
envPatcher(["OTEL_SEMCONV_STABILITY_OPT_IN"], (value) => {
const STABLE = ["http"];
const DUPLICATE = ["database", "messaging", "k8s"];
const flags = value
?.split(",")
.map((f) => f.trim())
.filter((f) => f !== "") ?? [];
const specified = new Set(flags.map((f) => f.split("/")[0]));
for (const flag of STABLE) {
if (!specified.has(flag)) {
flags.push(flag);
}
}
for (const flag of DUPLICATE) {
if (!specified.has(flag)) {
flags.push(`${flag}/dup`);
}
}
return flags.join(",");
}),
];
//# sourceMappingURL=patches.js.map