Skip to content

Commit 217a8d3

Browse files
committed
move from prom-client to OpenTelemetry metrics
Signed-off-by: Jessica He <[email protected]>
1 parent bffcb88 commit 217a8d3

File tree

10 files changed

+1399
-86
lines changed

10 files changed

+1399
-86
lines changed

.rhdh/docker/Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ ENV GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE=''
374374
# The `--no-node-snapshot` node option enables the usage of the backstage scaffolder on nodejs 20
375375
# https://github.com/backstage/backstage/issues/20661
376376

377-
ENTRYPOINT ["node", "packages/backend", "--no-node-snapshot", "--config", "app-config.yaml", "--config", "app-config.example.yaml", "--config", "app-config.example.production.yaml"]
377+
# Copy instrumentation needed for surfacing metrics
378+
COPY --chown=1001:1001 $EXTERNAL_SOURCE_NESTED/packages/backend/src/instrumentation.js ./
379+
# Remove write and execute permissions
380+
RUN chmod a=r ./instrumentation.js
381+
382+
ENTRYPOINT ["node", "--require", "./instrumentation.js", "packages/backend", "--no-node-snapshot", "--config", "app-config.yaml", "--config", "app-config.example.yaml", "--config", "app-config.example.production.yaml"]
378383

379384
# append Brew metadata here

docker/Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ ENV GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE=''
291291
# The `--no-node-snapshot` node option enables the usage of the backstage scaffolder on nodejs 20
292292
# https://github.com/backstage/backstage/issues/20661
293293

294-
ENTRYPOINT ["node", "packages/backend", "--no-node-snapshot", "--config", "app-config.yaml", "--config", "app-config.example.yaml", "--config", "app-config.example.production.yaml"]
294+
# Copy instrumentation needed for surfacing metrics
295+
COPY --chown=1001:1001 $EXTERNAL_SOURCE_NESTED/packages/backend/src/instrumentation.js ./
296+
# Remove write and execute permissions
297+
RUN chmod a=r ./instrumentation.js
298+
299+
ENTRYPOINT ["node", "--require", "./instrumentation.js", "packages/backend", "--no-node-snapshot", "--config", "app-config.yaml", "--config", "app-config.example.yaml", "--config", "app-config.example.production.yaml"]
295300

296301
# append Brew metadata here

docs/monitoring-and-logging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ When deploying Backstage Showcase onto a kubernetes cluster with the [RHDH Helm
1616

1717
### Enabling Metrics Monitoring on Openshift
1818

19-
To enable metrics monitoring on OpenShift, we need to create a `ServiceMonitor` resource in the OpenShift cluster that will be used by Prometheus to scrape metrics from your Backstage instance. For the metrics to be ingested by the built-in Prometheus instances in Openshift, please ensure you enabled [monitoring for user-defined projects](https://docs.openshift.com/container-platform/latest/monitoring/enabling-monitoring-for-user-defined-projects.html).
19+
To enable metrics monitoring on OpenShift, we need to create a `ServiceMonitor` resource in the OpenShift cluster that will be used by Prometheus to scrape metrics from your Backstage instance. For the metrics to be ingested by the built-in Prometheus instances in Openshift, please ensure you enabled [monitoring for user-defined projects](https://docs.openshift.com/container-platform/4.16/observability/monitoring/enabling-monitoring-for-user-defined-projects.html#enabling-monitoring-for-user-defined-projects).
2020

2121
#### Helm deployment
2222

packages/backend/package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"role": "backend"
99
},
1010
"scripts": {
11-
"start": "backstage-cli package start",
11+
"start": "backstage-cli package start --require ./src/instrumentation.js",
12+
"start:prometheus": "docker run --mount type=bind,source=./prometheus.yml,destination=/etc/prometheus/prometheus.yml --publish published=9090,target=9090,protocol=tcp prom/prometheus",
1213
"build": "backstage-cli package build",
1314
"lint:check": "backstage-cli package lint",
1415
"lint:fix": "backstage-cli package lint --fix",
@@ -49,10 +50,14 @@
4950
"@janus-idp/backstage-plugin-rbac-backend": "5.2.1",
5051
"@janus-idp/backstage-plugin-rbac-node": "1.8.0",
5152
"@janus-idp/backstage-scaffolder-backend-module-annotator": "2.2.0",
53+
"@opentelemetry/auto-instrumentations-node": "^0.50.2",
54+
"@opentelemetry/exporter-prometheus": "^0.53.0",
55+
"@opentelemetry/host-metrics": "^0.35.4",
56+
"@opentelemetry/instrumentation-runtime-node": "^0.9.0",
57+
"@opentelemetry/sdk-metrics": "^1.27.0",
58+
"@opentelemetry/sdk-node": "^0.53.0",
5259
"app": "*",
53-
"express-prom-bundle": "6.6.0",
5460
"global-agent": "3.0.0",
55-
"prom-client": "15.1.3",
5661
"undici": "6.19.8",
5762
"winston": "3.14.2",
5863
"winston-daily-rotate-file": "5.0.0"

packages/backend/prometheus.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This is an example configuration file for Prometheus, that is set to scrape
2+
# the metrics endpoint exposed by the example backend.
3+
#
4+
# If you want to run a docker based Prometheus instance locally to try it out,
5+
# run the following command in the Backstage root:
6+
#
7+
# docker run --mount type=bind,source=./packages/backend/prometheus.yml,destination=/etc/prometheus/prometheus.yml --publish published=9090,target=9090,protocol=tcp prom/prometheus
8+
#
9+
# After this, you have a Prometheus instance on http://localhost:9090
10+
11+
global:
12+
scrape_interval: 15s
13+
evaluation_interval: 15s
14+
15+
scrape_configs:
16+
- job_name: 'prometheus'
17+
static_configs:
18+
# This target address specifically works on Docker for MacOS - you may
19+
# need to change it to another host depending on your OS and Docker
20+
# engine.
21+
- targets: ['host.docker.internal:9464']

packages/backend/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { CommonJSModuleLoader } from './loader';
99
import { transports } from './logger';
1010
import {
1111
healthCheckPlugin,
12-
metricsPlugin,
1312
pluginIDProviderService,
1413
rbacDynamicPluginsProvider,
1514
} from './modules';
@@ -44,7 +43,6 @@ backend.add(
4443
}),
4544
);
4645

47-
backend.add(metricsPlugin);
4846
backend.add(healthCheckPlugin);
4947

5048
backend.add(import('@backstage/plugin-app-backend/alpha'));
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const { NodeSDK } = require('@opentelemetry/sdk-node');
2+
const {
3+
getNodeAutoInstrumentations,
4+
} = require('@opentelemetry/auto-instrumentations-node');
5+
const {
6+
RuntimeNodeInstrumentation,
7+
} = require('@opentelemetry/instrumentation-runtime-node');
8+
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
9+
const { HostMetrics } = require('@opentelemetry/host-metrics');
10+
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
11+
12+
// Application metrics will be exported to localhost:9464/metrics
13+
const prometheusApplication = new PrometheusExporter();
14+
const sdk = new NodeSDK({
15+
metricReader: prometheusApplication,
16+
instrumentations: [
17+
getNodeAutoInstrumentations(),
18+
new RuntimeNodeInstrumentation(),
19+
],
20+
});
21+
22+
sdk.start();
23+
24+
// Host/process metrics will be exported to localhost:9463/metrics
25+
const prometheusHost = new PrometheusExporter({ port: 9463 });
26+
const meterProvider = new MeterProvider({
27+
readers: [prometheusHost],
28+
});
29+
const hostMetrics = new HostMetrics({ meterProvider });
30+
hostMetrics.start();
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from './authProvidersModule';
22
export * from './rbacDynamicPluginsModule';
33
export * from './healthcheck';
4-
export * from './metrics';

packages/backend/src/modules/metrics.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)