Skip to content

Commit

Permalink
Merge pull request #527 from solarwinds/NH-98076
Browse files Browse the repository at this point in the history
Use HTTP GetSettings
  • Loading branch information
raphael-theriault-swi authored Dec 17, 2024
2 parents bf29621 + 624b40b commit b5bb329
Show file tree
Hide file tree
Showing 30 changed files with 1,262 additions and 2,052 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

3 changes: 3 additions & 0 deletions .yarn/versions/2e6d709d.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
releases:
"@solarwinds-apm/sampling": minor
solarwinds-apm: minor
1 change: 0 additions & 1 deletion packages/proto/.gitignore

This file was deleted.

2 changes: 0 additions & 2 deletions packages/proto/.prettierignore

This file was deleted.

3 changes: 0 additions & 3 deletions packages/proto/README.md

This file was deleted.

45 changes: 0 additions & 45 deletions packages/proto/build.js

This file was deleted.

19 changes: 0 additions & 19 deletions packages/proto/eslint.config.js

This file was deleted.

50 changes: 0 additions & 50 deletions packages/proto/package.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/proto/src
Submodule src deleted from c7318a
11 changes: 0 additions & 11 deletions packages/proto/wrapper

This file was deleted.

1 change: 1 addition & 0 deletions packages/sampling/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"test": "swtest -p test/tsconfig.json -c src"
},
"dependencies": {
"@noble/hashes": "^1.6.1",
"@opentelemetry/sdk-trace-base": "~1.29.0"
},
"peerDependencies": {
Expand Down
11 changes: 7 additions & 4 deletions packages/sampling/src/trace-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { hmac } from "@noble/hashes/hmac"
import { sha1 } from "@noble/hashes/sha1"
import { diag, type DiagLogger } from "@opentelemetry/api"
import { createHmac } from "crypto"

const TRIGGER_TRACE_KEY = "trigger-trace"
const TIMESTAMP_KEY = "ts"
Expand Down Expand Up @@ -174,10 +175,12 @@ export function validateSignature(
return Auth.BAD_TIMESTAMP
}

const hmac = createHmac("sha1", key)
const digest = hmac.update(header).digest()
const digest = hmac(sha1, key, header).reduce(
(hex, byte) => hex + byte.toString(16).padStart(2, "0"),
"",
)

if (signature === digest.toString("hex")) {
if (signature === digest) {
return Auth.OK
} else {
return Auth.BAD_SIGNATURE
Expand Down
2 changes: 0 additions & 2 deletions packages/solarwinds-apm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"test": "swtest -p test/tsconfig.json -c src"
},
"dependencies": {
"@grpc/grpc-js": "^1.12.2",
"@opentelemetry/api-logs": "~0.56.0",
"@opentelemetry/core": "~1.29.0",
"@opentelemetry/exporter-logs-otlp-proto": "~0.56.0",
Expand All @@ -74,7 +73,6 @@
"@solarwinds-apm/histogram": "workspace:^",
"@solarwinds-apm/instrumentations": "workspace:^",
"@solarwinds-apm/module": "workspace:^",
"@solarwinds-apm/proto": "workspace:^",
"@solarwinds-apm/sampling": "workspace:^",
"json-stringify-safe": "^5.0.1",
"node-releases": "^2.0.18",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class AppopticsInboundMetricsProcessor
method: meta.method,
status: meta.status,
url: meta.url,
domain: meta.hostname,
domain: null,
})
} else {
transaction = oboe.Span.createSpan({
Expand Down
8 changes: 3 additions & 5 deletions packages/solarwinds-apm/src/appoptics/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ export async function reporter(
): Promise<oboe.Reporter> {
const reporter = new oboe.Reporter({
service_key: `${config.serviceKey?.token}:${config.service}`,
host: config.collector,
certificates:
config.trustedpath ??
(config.collector.includes("appoptics.com") ? certificate : ""),
host: config.collector.hostname,
certificates: config.trustedpath ?? (config.appoptics ? certificate : ""),
grpc_proxy: config.proxy ?? "",
reporter: "ssl",
metric_format: 1,
metric_format: config.appoptics ? 1 : 2,
trace_metrics: 1,

log_level: otelLevelToOboeLevel(config.logLevel),
Expand Down
65 changes: 43 additions & 22 deletions packages/solarwinds-apm/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ const boolean = z.union([

const regex = z.union([
z.instanceof(RegExp),
z.string().transform((s, ctx) => {
z.string().transform((string, ctx) => {
try {
return new RegExp(s)
return new RegExp(string)
} catch (err) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
Expand All @@ -64,17 +64,32 @@ const regex = z.union([
const serviceKey = z
.string()
.includes(":")
.transform((k) => {
const [token, ...name] = k.split(":")
.transform((string) => {
const [token, ...name] = string.split(":")
return {
token: token!,
name: name.join(":"),
}
})

const trustedpath = z.string().transform((p, ctx) => {
const collector = z.string().transform((string, ctx) => {
if (!/^https?:/.test(string)) {
string = `https://${string}`
}
try {
return new URL(string)
} catch (err) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: (err as Error).message,
})
return z.NEVER
}
})

const trustedpath = z.string().transform((tp, ctx) => {
try {
return fs.readFile(p, "utf-8")
return fs.readFile(tp, "utf-8")
} catch (err) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
Expand Down Expand Up @@ -149,7 +164,7 @@ const schema = z.object({
serviceKey: serviceKey.optional(),
enabled: boolean.default(true),
legacy: boolean.optional(),
collector: z.string().default("apm.collector.na-01.cloud.solarwinds.com"),
collector: collector.default("apm.collector.na-01.cloud.solarwinds.com"),
trustedpath: trustedpath.optional(),
proxy: z.string().optional(),
logLevel: logLevel.default("warn"),
Expand Down Expand Up @@ -188,13 +203,14 @@ export interface Config extends z.input<typeof schema> {
/** Processed configuration for solarwinds-apm */
export interface Configuration extends z.output<typeof schema> {
service: string
appoptics: boolean
legacy: boolean

headers: Record<string, string>
otlp: {
tracesEndpoint?: string
metricsEndpoint?: string
logsEndpoint?: string
headers: Record<string, string>
}

source?: string
Expand Down Expand Up @@ -264,7 +280,8 @@ export async function read(): Promise<Configuration> {
])
}

const legacy = raw.legacy ?? raw.collector.includes("appoptics")
const appoptics = raw.collector.hostname.includes("appoptics")
const legacy = raw.legacy ?? appoptics
if (legacy && raw.exportLogsEnabled) {
console.warn("Logs export is not supported when exporting to AppOptics.")
raw.exportLogsEnabled = false
Expand All @@ -274,31 +291,25 @@ export async function read(): Promise<Configuration> {
...raw,

service,
appoptics,
legacy,

headers: raw.serviceKey?.token
? { authorization: `Bearer ${raw.serviceKey.token}` }
: {},
otlp: {
tracesEndpoint:
otel.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ??
otel.OTEL_EXPORTER_OTLP_ENDPOINT?.concat(ENDPOINTS.traces) ??
raw.collector
.replace(/^apm\.collector\./, "https://otel.collector.")
.concat(ENDPOINTS.traces),
otelUrl(raw.collector, ENDPOINTS.traces),
metricsEndpoint:
otel.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT ??
otel.OTEL_EXPORTER_OTLP_ENDPOINT?.concat(ENDPOINTS.metrics) ??
raw.collector
.replace(/^apm\.collector\./, "https://otel.collector.")
.concat(ENDPOINTS.metrics),
otelUrl(raw.collector, ENDPOINTS.metrics),
logsEndpoint:
otel.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT ??
otel.OTEL_EXPORTER_OTLP_ENDPOINT?.concat(ENDPOINTS.logs) ??
raw.collector
.replace(/^apm\.collector\./, "https://otel.collector.")
.concat(ENDPOINTS.logs),

headers: raw.serviceKey?.token
? { authorization: `Bearer ${raw.serviceKey.token}` }
: {},
otelUrl(raw.collector, ENDPOINTS.logs),
},

source,
Expand Down Expand Up @@ -362,3 +373,13 @@ function envObject(prefix = PREFIX) {
.map(([k, v]) => [fromEnvKey(k, prefix), v]),
)
}

function otelUrl(apmUrl: URL, endpoint: string) {
const otelUrl = new URL(apmUrl)
otelUrl.hostname = apmUrl.hostname.replace(
/^apm\.collector\./,
"otel.collector.",
)
otelUrl.pathname = endpoint
return otelUrl.href
}
2 changes: 1 addition & 1 deletion packages/solarwinds-apm/src/exporters/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class LogExporter extends OTLPLogExporter {
constructor(config: Configuration) {
super({
url: config.otlp.logsEndpoint,
headers: config.otlp.headers,
headers: config.headers,
httpAgentOptions: {
ca: config.trustedpath,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/solarwinds-apm/src/exporters/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class MetricExporter extends OTLPMetricExporter {
constructor(config: Configuration) {
super({
url: config.otlp.metricsEndpoint,
headers: config.otlp.headers,
headers: config.headers,
httpAgentOptions: {
ca: config.trustedpath,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/solarwinds-apm/src/exporters/traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class TraceExporter extends OTLPTraceExporter {
constructor(config: Configuration) {
super({
url: config.otlp.tracesEndpoint,
headers: config.otlp.headers,
headers: config.headers,
httpAgentOptions: {
ca: config.trustedpath,
},
Expand Down
Loading

0 comments on commit b5bb329

Please sign in to comment.