Skip to content

Commit

Permalink
Merge pull request #249 from solarwinds/NH-72500
Browse files Browse the repository at this point in the history
Disable most instrumentations by default in Lambda environments
  • Loading branch information
raphael-theriault-swi authored Feb 12, 2024
2 parents a717ede + 86e1874 commit 18680f5
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .yarn/versions/4c56dfd7.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
releases:
"@solarwinds-apm/instrumentations": major
"@solarwinds-apm/sdk": minor
solarwinds-apm: major
2 changes: 1 addition & 1 deletion packages/instrumentations/src/compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { type Comparator, compare, minVersion, Range } from "semver"

import { getInstrumentations } from "./index.js"

const instrumentations = getInstrumentations() as InstrumentationBase[]
const instrumentations = getInstrumentations({}, false) as InstrumentationBase[]

const init = "init" as const
const definitions = instrumentations
Expand Down
7 changes: 5 additions & 2 deletions packages/instrumentations/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,18 @@ export type InstrumentationConfigMap = {
}

export function getInstrumentations(
configs: InstrumentationConfigMap = {},
configs: InstrumentationConfigMap,
defaultDisabled: boolean,
): Instrumentation[] {
const instrumentations: Instrumentation[] = []
const c: Record<string, InstrumentationConfig> = configs

for (const [name, Class] of Object.entries<
new (config?: InstrumentationConfig) => Instrumentation
>(INSTRUMENTATIONS)) {
const config = c[name]
let config = c[name]

if (defaultDisabled) (config ??= {}).enabled ??= false
if (config?.enabled === false) continue

instrumentations.push(new Class(config))
Expand Down
File renamed without changes.
25 changes: 25 additions & 0 deletions packages/sdk/src/patches/aws-sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright 2023-2024 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 { type InstrumentationConfig } from "@opentelemetry/instrumentation"
import { IS_AWS_LAMBDA } from "@solarwinds-apm/module"

import { type Patch } from "."

export const patch: Patch<InstrumentationConfig> = (config) => ({
...config,
enabled: config.enabled ?? (IS_AWS_LAMBDA || undefined),
})
6 changes: 4 additions & 2 deletions packages/sdk/src/patches/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import { type TextMapPropagator } from "@opentelemetry/api"
import { type InstrumentationConfig } from "@opentelemetry/instrumentation"

import { type SwConfiguration } from "../config"
import * as awsLambda from "./aws-lambda"
import * as awsSdk from "./aws-sdk"
import * as bunyan from "./bunyan"
import * as fs from "./fs"
import * as http from "./http"
import * as lambda from "./lambda"
import * as mysql2 from "./mysql2"
import * as pg from "./pg"
import * as pino from "./pino"
Expand All @@ -48,7 +49,8 @@ export type Patch<Config extends InstrumentationConfig> = (
export const RESOURCE_SERVICE_NAME = "resource.service.name" as const

const patches = {
"aws-lambda": lambda,
"aws-lambda": awsLambda,
"aws-sdk": awsSdk,
bunyan,
fs,
http,
Expand Down
1 change: 1 addition & 0 deletions packages/solarwinds-apm/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ const schema = z.object({
swMetrics: boolean.default(!IS_SERVERLESS),
initMessage: boolean.default(!IS_SERVERLESS),
resourceDetection: boolean.default(true),
instrumentationsDefaultDisabled: boolean.default(IS_SERVERLESS),
})
.default({}),
})
Expand Down
1 change: 1 addition & 0 deletions packages/solarwinds-apm/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ function initInstrumentations(config: ExtendedSwConfiguration) {
...config,
responsePropagator: traceOptionsResponsePropagator,
}),
config.dev.instrumentationsDefaultDisabled,
),
...(config.instrumentations.extra ?? []),
]
Expand Down
1 change: 1 addition & 0 deletions packages/solarwinds-apm/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe("readConfig", () => {
swMetrics: true,
initMessage: true,
resourceDetection: true,
instrumentationsDefaultDisabled: false,
},
}

Expand Down

0 comments on commit 18680f5

Please sign in to comment.