Skip to content

Commit

Permalink
Merge pull request #163 from solarwinds/NH-61324
Browse files Browse the repository at this point in the history
Fix various issues with serverless code
  • Loading branch information
raphael-theriault-swi authored Nov 20, 2023
2 parents 19134a8 + 6dbe287 commit 1a41387
Show file tree
Hide file tree
Showing 20 changed files with 197 additions and 106 deletions.
1 change: 1 addition & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
rm yarn.lock
git grep -lz '@solarwinds-apm/' | xargs -0 sed -ie 's:@solarwinds-apm/:@${{ github.repository_owner }}/solarwinds-apm-:g'
git grep -lz -G 'solarwinds-apm[^-]' | xargs -0 sed -rie 's:solarwinds-apm([^-]):@${{ github.repository_owner }}/solarwinds-apm\1:g'
yarn install
yarn version:testing
yarn install
yarn lint:fix
Expand Down
9 changes: 9 additions & 0 deletions .yarn/versions/0d3fa678.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
releases:
"@solarwinds-apm/bindings": patch
"@solarwinds-apm/sdk": minor
"@solarwinds-apm/test": minor
solarwinds-apm: patch

declined:
- "@solarwinds-apm/compat"
- "@solarwinds-apm/dependencies"
4 changes: 4 additions & 0 deletions docker/otel-collector/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ exporters:

service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [logging]
metrics:
receivers: [otlp]
processors: [batch]
Expand Down
2 changes: 1 addition & 1 deletion examples/express-mysql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@solarwinds-apm/example-express-mysql",
"private": true,
"scripts": {
"start": "node --loader solarwinds-apm/loader index.js"
"start": "node --import solarwinds-apm index.js"
},
"dependencies": {
"@opentelemetry/api": "^1.3.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/fastify-postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"type": "module",
"scripts": {
"start": "node --loader solarwinds-apm/loader index.js"
"start": "node --import solarwinds-apm index.js"
},
"dependencies": {
"@fastify/postgres": "^5.2.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-distributed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"type": "module",
"scripts": {
"start": "concurrently \"node --loader solarwinds-apm/loader external.js\" \"node --loader solarwinds-apm/loader internal.js\""
"start": "concurrently \"node --import solarwinds-apm external.js\" \"node --import solarwinds-apm internal.js\""
},
"dependencies": {
"@opentelemetry/api": "^1.3.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-manual/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"type": "module",
"scripts": {
"start": "node --loader solarwinds-apm/loader index.js"
"start": "node --import solarwinds-apm index.js"
},
"dependencies": {
"@opentelemetry/api": "^1.3.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/hello/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"type": "module",
"scripts": {
"start": "node --loader solarwinds-apm/loader index.js"
"start": "node --import solarwinds-apm index.js"
},
"dependencies": {
"@opentelemetry/api": "^1.3.0",
Expand Down
4 changes: 3 additions & 1 deletion examples/next-prisma/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
module.exports = {
experimental: {
instrumentationHook: true,
serverActions: true,
serverComponentsExternalPackages: ["solarwinds-apm"],
},
// the following is only necessary due to a bug in how next.js handles monorepos
// it is not necessary to include in your own config
webpack: (config) => {
config.externals.push({
"solarwinds-apm": "commonjs solarwinds-apm",
Expand Down
4 changes: 2 additions & 2 deletions examples/next-prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@opentelemetry/api": "^1.3.0",
"@prisma/client": "^5.4.1",
"@prisma/instrumentation": "^5.4.1",
"next": "^13.5.4",
"next": "^14.0.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"solarwinds-apm": "workspace:^"
Expand All @@ -19,7 +19,7 @@
"@types/node": "^16.13.0",
"@types/react": "^18.2.25",
"eslint": "^8.50.0",
"eslint-config-next": "^13.5.4",
"eslint-config-next": "^14.0.3",
"prisma": "^5.4.1",
"typescript": "^5.2.2"
}
Expand Down
6 changes: 5 additions & 1 deletion packages/bindings/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ const configs = targets.flatMap(({ name, oboe, target, cpu, glibc }) => {

// only build serverless api for serverless
const oboeSources = name.includes("serverless")
? ["src/oboe/oboe.serverless.cc", "src/oboe/oboe_api.cc"]
? [
"src/oboe/oboe.serverless.cc",
"src/oboe/oboe_api.cc",
"oboe/include/oboe_api.cpp",
]
: [
"src/oboe/oboe.cc",
"src/oboe/config.cc",
Expand Down
9 changes: 8 additions & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"type": "commonjs",
"exports": {
".": "./dist/index.js",
"./otlp-exporter": "./dist/otlp-exporter.js"
"./otlp-exporter": "./dist/otlp-exporter.js",
"./otlp-metrics-exporter": "./dist/otlp-metrics-exporter.js"
},
"main": "./dist/index.js",
"files": [
Expand Down Expand Up @@ -47,18 +48,24 @@
},
"peerDependencies": {
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/exporter-metrics-otlp-grpc": ">=0.34.0",
"@opentelemetry/exporter-trace-otlp-grpc": ">=0.34.0"
},
"peerDependenciesMeta": {
"@opentelemetry/api": {
"optional": false
},
"@opentelemetry/exporter-metrics-otlp-grpc": {
"optional": true
},
"@opentelemetry/exporter-trace-otlp-grpc": {
"optional": true
}
},
"devDependencies": {
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/exporter-metrics-otlp-grpc": "~0.45.1",
"@opentelemetry/exporter-trace-otlp-grpc": "~0.45.1",
"@opentelemetry/instrumentation": "~0.45.1",
"@opentelemetry/resources": "~1.18.1",
"@opentelemetry/sdk-trace-node": "~1.18.1",
Expand Down
34 changes: 26 additions & 8 deletions packages/sdk/src/metrics/serverless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,42 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { metrics } from "@opentelemetry/api"
import { metrics, ValueType } from "@opentelemetry/api"
import { type oboe } from "@solarwinds-apm/bindings"
import { lazy } from "@solarwinds-apm/lazy"

const meter = metrics.getMeter("sw.apm.sampling.metrics")
const meter = lazy(() => metrics.getMeter("sw.apm.sampling.metrics"))

const counters = {
RequestCount: lazy(() => meter.createCounter("trace.service.request_count")),
RequestCount: lazy(() =>
meter.createCounter("trace.service.request_count", {
valueType: ValueType.INT,
}),
),
TokenBucketExhaustionCount: lazy(() =>
meter.createCounter("trace.service.tokenbucket_exhaustion_count"),
meter.createCounter("trace.service.tokenbucket_exhaustion_count", {
valueType: ValueType.INT,
}),
),
TraceCount: lazy(() =>
meter.createCounter("trace.service.tracecount", {
valueType: ValueType.INT,
}),
),
SampleCount: lazy(() =>
meter.createCounter("trace.service.samplecount", {
valueType: ValueType.INT,
}),
),
TraceCount: lazy(() => meter.createCounter("trace.service.tracecount")),
SampleCount: lazy(() => meter.createCounter("trace.service.samplecount")),
ThroughTraceCount: lazy(() =>
meter.createCounter("trace.service.through_trace_count"),
meter.createCounter("trace.service.through_trace_count", {
valueType: ValueType.INT,
}),
),
TriggeredTraceCount: lazy(() =>
meter.createCounter("trace.service.triggered_trace_count"),
meter.createCounter("trace.service.triggered_trace_count", {
valueType: ValueType.INT,
}),
),
}

Expand Down
50 changes: 50 additions & 0 deletions packages/sdk/src/otlp-metrics-exporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2023 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 { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-grpc"
import {
Aggregation,
AggregationTemporality,
ExponentialHistogramAggregation,
InstrumentType,
type PushMetricExporter,
} from "@opentelemetry/sdk-metrics"

export class SwOtlpMetricsExporter
extends OTLPMetricExporter
implements PushMetricExporter
{
override selectAggregationTemporality(
instrumentType: InstrumentType,
): AggregationTemporality {
switch (instrumentType) {
case InstrumentType.COUNTER:
case InstrumentType.OBSERVABLE_COUNTER:
return AggregationTemporality.DELTA
default:
return super.selectAggregationTemporality(instrumentType)
}
}

selectAggregation(instrumentType: InstrumentType): Aggregation {
switch (instrumentType) {
case InstrumentType.HISTOGRAM:
return new ExponentialHistogramAggregation(undefined, true)
default:
return Aggregation.Default()
}
}
}
9 changes: 6 additions & 3 deletions packages/sdk/src/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,8 @@ export class SwSampler implements Sampler {
traceparent = traceParent(parentSpanContext)
}

return this.oboeDecisionFunction({
const input: oboe.DecisionOptions = {
in_xtrace: traceparent,
custom_sample_rate: oboe.SETTINGS_UNSET,
custom_tracing_mode: tracingMode,
custom_trigger_mode: this.config.triggerTraceEnabled
? oboe.TRIGGER_ENABLED
Expand All @@ -176,7 +175,11 @@ export class SwSampler implements Sampler {
header_signature: traceOptions?.signature,
header_timestamp: traceOptions?.timestamp,
tracestate: parentSpanContext?.traceState?.get(TRACESTATE_SW_KEY),
})
}
this.logger.debug("tracing decision input", input)
const output = this.oboeDecisionFunction(input)
this.logger.debug("tracing decision output", output)
return output
}

private tracingMode(
Expand Down
12 changes: 1 addition & 11 deletions packages/solarwinds-apm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,15 @@
"zod": "^3.22.4"
},
"peerDependencies": {
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/exporter-metrics-otlp-grpc": ">=0.34.0",
"@opentelemetry/exporter-trace-otlp-grpc": ">=0.34.0"
"@opentelemetry/api": "^1.3.0"
},
"peerDependenciesMeta": {
"@opentelemetry/api": {
"optional": false
},
"@opentelemetry/exporter-metrics-otlp-grpc": {
"optional": true
},
"@opentelemetry/exporter-trace-otlp-grpc": {
"optional": true
}
},
"devDependencies": {
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/exporter-metrics-otlp-grpc": "~0.45.1",
"@opentelemetry/exporter-trace-otlp-grpc": "~0.45.1",
"@solarwinds-apm/eslint-config": "workspace:^",
"@solarwinds-apm/rollup-config": "workspace:^",
"@solarwinds-apm/test": "workspace:^",
Expand Down
7 changes: 4 additions & 3 deletions packages/solarwinds-apm/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export async function init() {
.merge(
new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: config.serviceName,
"sw.data.module": "apm",
}),
)

Expand Down Expand Up @@ -289,10 +290,10 @@ async function metricReaders(
}

if (config.dev.otlpMetrics) {
const { OTLPMetricExporter } = await import(
"@opentelemetry/exporter-metrics-otlp-grpc"
const { SwOtlpMetricsExporter } = await import(
"@solarwinds-apm/sdk/otlp-metrics-exporter"
)
const exporter = new OTLPMetricExporter()
const exporter = new SwOtlpMetricsExporter()
readers.push(
new PeriodicExportingMetricReader({
exporter,
Expand Down
2 changes: 1 addition & 1 deletion packages/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"chai-as-promised": "^7.1.1",
"globby": "^11.1.0",
"semver": "^7.5.4",
"tsx": "^3.13.0"
"tsx": "^4.1.3"
},
"devDependencies": {
"@solarwinds-apm/eslint-config": "workspace:^",
Expand Down
7 changes: 6 additions & 1 deletion scripts/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ exec(`turbo run build --filter=./examples/${example}...`)
// get env vars that will be passed to the container
const env = Object.fromEntries(
Object.entries(process.env).filter(
([key]) => key.startsWith("SW_APM_") || key.startsWith("OTEL_"),
([key]) =>
key.startsWith("SW_APM_") ||
key.startsWith("OTEL_") ||
key.startsWith("AWS_LAMBDA_"),
),
)
if (collector) {
env.SW_APM_COLLECTOR = "apm-collector:12224"
env.SW_APM_TRUSTED_PATH =
"/solarwinds-apm/docker/apm-collector/server-grpc.crt"
env.OTEL_EXPORTER_OTLP_PROTOCOL = "grpc"
env.OTEL_EXPORTER_OTLP_ENDPOINT = "http://otel-collector:4317"
}

// run example inside container
Expand Down
Loading

0 comments on commit 1a41387

Please sign in to comment.