Skip to content

Commit 33435ce

Browse files
committed
Finalized the Java example
1 parent 54d99ae commit 33435ce

5 files changed

Lines changed: 84 additions & 53 deletions

File tree

metrics-examples/java/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ WORKDIR /src
66
RUN mvn package -Dmaven.test.skip=true
77

88
# Runtime stage using distroless
9-
FROM gcr.io/distroless/java21-debian12:nonroot
9+
FROM gcr.io/distroless/java21-debian13:nonroot
1010

1111
# Copy the JAR from builder
1212
COPY --chown=1001:0 --from=builder /src/target/*.jar /app/app.jar

metrics-examples/java/README.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ java -jar target/metrics-example-1.0.0.jar
6767

6868
The application exposes two servers:
6969
- Main application: `http://localhost:8080`
70-
- Metrics endpoint: `http://localhost:2112/prometheus` (or `/metrics`)
70+
- Metrics endpoint: `http://localhost:2112/metrics` (Prometheus format)
7171
- Health check: `http://localhost:2112/health`
7272

7373
## Configuration
@@ -94,15 +94,54 @@ For database connectivity, create a Code Engine service binding between your pro
9494
- `GET /outbound/status/{code}` - Request specific HTTP status code
9595

9696
Management endpoints (port 2112):
97-
- `GET /prometheus` - Prometheus metrics endpoint
98-
- `GET /metrics` - Alternative metrics endpoint
97+
- `GET /metrics` - Prometheus metrics endpoint (Prometheus text format)
9998
- `GET /health` - Health check endpoint
10099

101100
All outbound endpoints include simulated compute-intensive data processing (0-3s duration, 40-80% CPU intensity).
102101

103102
## Metrics
104103

105-
The application exposes Prometheus metrics at `/prometheus` (port 2112). All metric names are prefixed with a configurable value set via the `METRICS_NAME_PREFIX` environment variable (default: `mymetrics_`).
104+
The application exposes Prometheus metrics at `/metrics` (port 2112) in Prometheus text format. All metric names are prefixed with a configurable value set via the `METRICS_NAME_PREFIX` environment variable (default: `mymetrics_`).
105+
106+
### Filtering Metrics
107+
108+
By default, Spring Boot exports many JVM, system, and application metrics. You can control which metrics are exported using the following approaches:
109+
110+
**Option 1: Disable entire metric categories**
111+
112+
Set these properties to `false` to exclude entire categories of metrics:
113+
114+
```properties
115+
management.metrics.enable.jvm=false # Disable all JVM metrics
116+
management.metrics.enable.process=false # Disable process metrics
117+
management.metrics.enable.system=false # Disable system metrics
118+
management.metrics.enable.tomcat=false # Disable Tomcat metrics
119+
management.metrics.enable.logback=false # Disable logback metrics
120+
management.metrics.enable.executor=false # Disable executor metrics
121+
management.metrics.enable.disk=false # Disable disk metrics
122+
management.metrics.enable.application=false # Disable application startup metrics
123+
```
124+
125+
**Option 2: Keep only custom metrics**
126+
127+
To export only your custom metrics (those with the `mymetrics_` prefix), disable all default categories:
128+
129+
```bash
130+
ibmcloud ce application update \
131+
--name metrics-example-app-java \
132+
--env management.metrics.enable.jvm=false \
133+
--env management.metrics.enable.process=false \
134+
--env management.metrics.enable.system=false \
135+
--env management.metrics.enable.tomcat=false \
136+
--env management.metrics.enable.logback=false \
137+
--env management.metrics.enable.executor=false \
138+
--env management.metrics.enable.disk=false \
139+
--env management.metrics.enable.application=false
140+
```
141+
142+
**Option 3: Use a MeterFilter for fine-grained control**
143+
144+
For more advanced filtering, you can create a custom `MeterFilter` bean in your configuration. See the [Micrometer documentation](https://micrometer.io/docs/concepts#_meter_filters){: external} for details.
106145

107146
**Request Metrics**
108147
- `mymetrics_requests_total`: Total requests by method and path

metrics-examples/java/src/main/resources/application.properties

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ management.metrics.export.prometheus.enabled=true
1717
# Metrics Configuration
1818
metrics.name.prefix=${METRICS_NAME_PREFIX:mymetrics_}
1919

20+
# Metrics Filtering - Control which metrics are exported
21+
# Option 1: Disable specific metric categories (set to false to exclude)
22+
management.metrics.enable.jvm=false
23+
management.metrics.enable.process=false
24+
management.metrics.enable.system=false
25+
management.metrics.enable.tomcat=false
26+
management.metrics.enable.logback=false
27+
management.metrics.enable.executor=false
28+
management.metrics.enable.disk=false
29+
management.metrics.enable.application=false
30+
31+
# Option 2: Exclude specific metrics by name pattern (comma-separated list)
32+
# Uncomment and customize to blocklist specific metrics
33+
#management.metrics.export.prometheus.descriptions=false
34+
2035
# HTTP Client Configuration
2136
httpbin.base.url=${HTTPBIN_BASE_URL:https://httpbin.org}
2237

metrics-examples/java/target/classes/application.properties

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

metrics-examples/run

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,32 @@ for i in "${languages[@]}"; do
4848
--cpu 0.25 \
4949
--env HTTPBIN_BASE_URL=https://httpbin.284uby30ujzw.ca-tor.codeengine.appdomain.cloud
5050

51-
# Give the Code Engine app some time to reconcile and move the traffic target to the latest revision
52-
sleep 10
51+
# Wait for the Kubernetes service resource to be ready before patching
52+
echo "Waiting for ksvc '$app_name' to be ready..."
53+
kubectl wait --for=condition=Ready ksvc/"$app_name" --timeout=300s
5354

54-
# Annotate the app
55-
kubectl patch kservice "$app_name" --type=json -p='[
56-
{"op":"add","path":"/spec/template/metadata/annotations/codeengine.cloud.ibm.com~1userMetricsScrape","value":"true"},
57-
{"op":"add","path":"/spec/template/metadata/annotations/codeengine.cloud.ibm.com~1userMetricsPath","value":"/metrics"},
58-
{"op":"add","path":"/spec/template/metadata/annotations/codeengine.cloud.ibm.com~1userMetricsPort","value":"2112"}
59-
]'
55+
# Remove the pause-build label if it exists to allow the build to proceed
56+
if kubectl get ksvc "$app_name" -o jsonpath='{.spec.template.metadata.labels.codeengine\.cloud\.ibm\.com/pause-build}' 2>/dev/null | grep -q .; then
57+
echo "Removing pause-build label from '$app_name'..."
58+
kubectl patch kservice "$app_name" --type=json -p='[
59+
{"op":"remove","path":"/spec/template/metadata/labels/codeengine.cloud.ibm.com~1pause-build"}
60+
]'
61+
fi
62+
63+
# Annotate the app with metrics configuration
64+
if [[ "$LANGUAGE" == "java" ]];then
65+
kubectl patch kservice "$app_name" --type=json -p='[
66+
{"op":"add","path":"/spec/template/metadata/annotations/codeengine.cloud.ibm.com~1userMetricsScrape","value":"true"},
67+
{"op":"add","path":"/spec/template/metadata/annotations/codeengine.cloud.ibm.com~1userMetricsPath","value":"/prometheus"},
68+
{"op":"add","path":"/spec/template/metadata/annotations/codeengine.cloud.ibm.com~1userMetricsPort","value":"2112"}
69+
]'
70+
else
71+
kubectl patch kservice "$app_name" --type=json -p='[
72+
{"op":"add","path":"/spec/template/metadata/annotations/codeengine.cloud.ibm.com~1userMetricsScrape","value":"true"},
73+
{"op":"add","path":"/spec/template/metadata/annotations/codeengine.cloud.ibm.com~1userMetricsPath","value":"/metrics"},
74+
{"op":"add","path":"/spec/template/metadata/annotations/codeengine.cloud.ibm.com~1userMetricsPort","value":"2112"}
75+
]'
76+
fi
6077
else
6178
continue;
6279
fi

0 commit comments

Comments
 (0)