solarwinds-apm
Version:
OpenTelemetry-based SolarWinds APM library
87 lines (84 loc) • 3.31 kB
JavaScript
/*
Copyright 2023-2025 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;
}
function patcher(names, patch) {
return (configs, options) => {
for (const name of names) {
patch((configs[name] ??= {}), options);
}
};
}
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-fs"], (config) => {
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-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);
};
}),
];
//# sourceMappingURL=patches.js.map