Skip to content

Commit

Permalink
use http getsettings
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael-theriault-swi committed Dec 12, 2024
1 parent 0291583 commit ae72a05
Show file tree
Hide file tree
Showing 20 changed files with 567 additions and 866 deletions.
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
41 changes: 29 additions & 12 deletions packages/solarwinds-apm/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,24 @@ const serviceKey = z
}
})

const trustedpath = z.string().transform((p, ctx) => {
const collector = z.string().transform((c, ctx) => {
if (!/^https?:/.test(c)) {
c = `https://${c}`
}
try {
return fs.readFile(p, "utf-8")
return new URL(c)
} 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(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,31 @@ 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
raw.collector.hostname
.replace(/^apm\.collector\./, "https://otel.collector.")
.concat(ENDPOINTS.traces),
metricsEndpoint:
otel.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT ??
otel.OTEL_EXPORTER_OTLP_ENDPOINT?.concat(ENDPOINTS.metrics) ??
raw.collector
raw.collector.hostname
.replace(/^apm\.collector\./, "https://otel.collector.")
.concat(ENDPOINTS.metrics),
logsEndpoint:
otel.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT ??
otel.OTEL_EXPORTER_OTLP_ENDPOINT?.concat(ENDPOINTS.logs) ??
raw.collector
raw.collector.hostname
.replace(/^apm\.collector\./, "https://otel.collector.")
.concat(ENDPOINTS.logs),

headers: raw.serviceKey?.token
? { authorization: `Bearer ${raw.serviceKey.token}` }
: {},
},

source,
Expand Down
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
6 changes: 3 additions & 3 deletions packages/solarwinds-apm/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,12 @@ async function initTracing(
new ParentSpanProcessor(),
]
} else {
const [{ GrpcSampler }, { TraceExporter }] = await Promise.all([
import("./sampling/grpc.js"),
const [{ HttpSampler }, { TraceExporter }] = await Promise.all([
import("./sampling/http.js"),
import("./exporters/traces.js"),
])

sampler = new GrpcSampler(config)
sampler = new HttpSampler(config)
processors = [
new TransactionNameProcessor(config),
new ResponseTimeProcessor(),
Expand Down
Loading

0 comments on commit ae72a05

Please sign in to comment.