Skip to content

Commit

Permalink
auto-instrument OpenTelemetry in the GB Proxy app (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryce-fitzsimons authored Jul 26, 2024
1 parent 628f0ff commit a15f514
Show file tree
Hide file tree
Showing 5 changed files with 1,149 additions and 5 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ The GrowthBook Proxy repository is a mono-repo containing the following packages

### What's new

**Version 1.1.8**
- Auto-instrument OpenTelemetry when using `yarn start:with-tracing`

**Version 1.1.4**
- Support Redis-based sticky bucketing for remote evaluation
- Update remote evaluation to allow for buffered sticky bucket writes
Expand Down Expand Up @@ -110,7 +113,7 @@ The GrowthBook Proxy supports a number of configuration options available via en

### Caching

By default, features are cached in memory in GrowthBook Proxy; you may provide your own cache service via Redis or Mongo. To fully utilize the GrowthBook Proxy, we highly recommend using Redis, which is a prerequisite for real-time updates when your proxy is horizontally scaled (as proxy instances are kept in-sync using Redis pub/sub).
By default, features are cached in memory in the GrowthBook Proxy; you may provide your own cache service via Redis or Mongo. To fully utilize the GrowthBook Proxy, we highly recommend using Redis, which is a prerequisite for real-time updates when your proxy is horizontally scaled (as proxy instances are kept in-sync using Redis pub/sub).

- `CACHE_ENGINE` - One of: `memory`, `redis`, or `mongo` (default: `memory`)
- `CACHE_CONNECTION_URL` - The URL of your Redis or Mongo Database
Expand Down Expand Up @@ -149,6 +152,14 @@ Although we recommend terminating SSL using your load balancer, you can also con

If the GrowthBook app your proxy is connecting to is using a self-signed certificate, you can disable certificate verification by setting `NODE_TLS_REJECT_UNAUTHORIZED` to "0".

### Observability (OpenTelemetry)

The GrowthBook Proxy is instrumented with OpenTelemetry to publish observability metrics, traces, and logs.

To enable, you must change the Docker CMD from the default `yarn start` to `yarn start:with-tracing`.

The standard [OTEL\_\* Environment Variables](https://opentelemetry.io/docs/concepts/sdk-configuration/) are supported, such as `OTEL_SERVICE_NAME` and `OTEL_EXPORTER_OTLP_ENDPOINT`.

### Other common configuration options

**Streaming**
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"build:proxy": "yarn build:deps:proxy && wsrun -p @growthbook/proxy -c build",
"type-check": "wsrun -r type-check",
"start": "wsrun -p @growthbook/proxy -c start",
"start:with-tracing": "wsrun -p @growthbook/proxy -c start:with-tracing",
"dev": "wsrun -r dev",
"dev:proxy": "wsrun -p @growthbook/proxy @growthbook/proxy-eval -c dev",
"dev:edge": "wsrun -p @growthbook/edge-utils @growthbook/edge-express -c dev"
Expand Down
9 changes: 7 additions & 2 deletions packages/apps/proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"node": ">=18"
},
"description": "GrowthBook proxy server for caching, realtime updates, telemetry, etc",
"version": "1.1.6",
"version": "1.1.8",
"main": "dist/app.js",
"license": "MIT",
"repository": {
Expand All @@ -19,6 +19,7 @@
"build": "yarn build:clean && yarn build:typescript",
"type-check": "tsc --pretty --noEmit",
"start": "node dist/index.js",
"start:with-tracing": "node --require ./dist/tracing.js dist/index.js",
"dev": "concurrently 'tsc --watch' 'nodemon -q dist/index.js | yarn pino-pretty'"
},
"dependencies": {
Expand All @@ -32,7 +33,11 @@
"spdy": "^4.0.2",
"uuid": "^9.0.0",
"@growthbook/growthbook": "^1.1.0",
"@growthbook/proxy-eval": "^1.0.3"
"@growthbook/proxy-eval": "^1.0.3",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/auto-instrumentations-node": "^0.48.0",
"@opentelemetry/exporter-metrics-otlp-proto": "^0.52.1",
"@opentelemetry/sdk-metrics": "^1.25.1"
},
"devDependencies": {
"@types/cors": "^2.8.14",
Expand Down
56 changes: 56 additions & 0 deletions packages/apps/proxy/src/tracing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright The OpenTelemetry Authors
*
* 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
*
* https://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 * as opentelemetry from "@opentelemetry/sdk-node";
import { diag, DiagConsoleLogger } from "@opentelemetry/api";
import {
getNodeAutoInstrumentations,
getResourceDetectors,
} from "@opentelemetry/auto-instrumentations-node";
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-proto";
import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";

diag.setLogger(
new DiagConsoleLogger(),
opentelemetry.core.getEnv().OTEL_LOG_LEVEL,
);

const metricReader = new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter(),
exportIntervalMillis: 10000,
});

const sdk = new opentelemetry.NodeSDK({
instrumentations: getNodeAutoInstrumentations(),
resourceDetectors: getResourceDetectors(),
metricReader,
});

try {
sdk.start();
diag.info("OpenTelemetry automatic instrumentation started successfully");
} catch (error) {
diag.error(
"Error initializing OpenTelemetry SDK. Your application is not instrumented and will not produce telemetry",
error,
);
}

process.on("SIGTERM", () => {
sdk
.shutdown()
.then(() => diag.debug("OpenTelemetry SDK terminated"))
.catch((error) => diag.error("Error terminating OpenTelemetry SDK", error));
});
Loading

0 comments on commit a15f514

Please sign in to comment.