Skip to content

Commit 794fd97

Browse files
authored
Merge pull request #1167 from brendandburns/prom
Add utilities for instrumentation.
2 parents cadfe0a + 62c14d5 commit 794fd97

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

examples/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
<relativePath>../pom.xml</relativePath>
1313
</parent>
1414
<dependencies>
15+
<dependency>
16+
<groupId>io.prometheus</groupId>
17+
<artifactId>simpleclient</artifactId>
18+
<version>0.9.0</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>io.prometheus</groupId>
22+
<artifactId>simpleclient_httpserver</artifactId>
23+
<version>0.9.0</version>
24+
</dependency>
1525
<dependency>
1626
<groupId>io.kubernetes</groupId>
1727
<artifactId>client-java-api</artifactId>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.examples;
14+
15+
import io.kubernetes.client.monitoring.Monitoring;
16+
import io.kubernetes.client.openapi.ApiClient;
17+
import io.kubernetes.client.openapi.ApiException;
18+
import io.kubernetes.client.openapi.Configuration;
19+
import io.kubernetes.client.openapi.apis.CoreV1Api;
20+
import io.kubernetes.client.openapi.models.V1Pod;
21+
import io.kubernetes.client.openapi.models.V1PodList;
22+
import io.kubernetes.client.util.Config;
23+
import java.io.IOException;
24+
25+
/**
26+
* A simple example of how to use the Java API with Prometheus metrics
27+
*
28+
* <p>Easiest way to run this: mvn exec:java
29+
* -Dexec.mainClass="io.kubernetes.client.examples.PrometheusExample"
30+
*
31+
* <p>From inside $REPO_DIR/examples
32+
*/
33+
public class PrometheusExample {
34+
public static void main(String[] args) throws IOException, ApiException {
35+
ApiClient client = Config.defaultClient();
36+
Configuration.setDefaultApiClient(client);
37+
38+
// Install an HTTP Interceptor that adds metrics
39+
Monitoring.installMetrics(client);
40+
41+
// Install a simple HTTP server to serve prometheus metrics. If you already are serving
42+
// metrics elsewhere, this is unnecessary.
43+
Monitoring.startMetricsServer("localhost", 8080);
44+
45+
CoreV1Api api = new CoreV1Api();
46+
47+
while (true) {
48+
// A request that should return 200
49+
V1PodList list =
50+
api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
51+
// A request that should return 404
52+
try {
53+
V1Pod pod = api.readNamespacedPod("foo", "bar", null, null, null);
54+
} catch (ApiException ex) {
55+
if (ex.getCode() != 404) {
56+
throw ex;
57+
}
58+
}
59+
try {
60+
Thread.sleep(10000);
61+
} catch (InterruptedException ex) {
62+
ex.printStackTrace();
63+
}
64+
}
65+
}
66+
}

util/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
<relativePath>../pom.xml</relativePath>
1313
</parent>
1414
<dependencies>
15+
<dependency>
16+
<groupId>io.prometheus</groupId>
17+
<artifactId>simpleclient</artifactId>
18+
<version>0.9.0</version>
19+
<optional>true</optional>
20+
</dependency>
21+
<dependency>
22+
<groupId>io.prometheus</groupId>
23+
<artifactId>simpleclient_httpserver</artifactId>
24+
<version>0.9.0</version>
25+
<optional>true</optional>
26+
</dependency>
1527
<dependency>
1628
<groupId>io.kubernetes</groupId>
1729
<artifactId>client-java-api</artifactId>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.monitoring;
14+
15+
import io.kubernetes.client.openapi.ApiClient;
16+
import io.prometheus.client.exporter.HTTPServer;
17+
import java.io.IOException;
18+
19+
public class Monitoring {
20+
public static void startMetricsServer(int port) throws IOException {
21+
new HTTPServer(port);
22+
}
23+
24+
public static void startMetricsServer(String host, int port) throws IOException {
25+
new HTTPServer(host, port);
26+
}
27+
28+
public static void installMetrics(ApiClient client) throws IOException {
29+
client.setHttpClient(
30+
client.getHttpClient().newBuilder().addInterceptor(new PrometheusInterceptor()).build());
31+
}
32+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.monitoring;
14+
15+
import io.prometheus.client.Counter;
16+
import io.prometheus.client.Histogram;
17+
import java.io.IOException;
18+
import okhttp3.Interceptor;
19+
import okhttp3.Response;
20+
21+
public class PrometheusInterceptor implements Interceptor {
22+
static final String PREFIX = "k8s_java";
23+
static final Counter requests =
24+
Counter.build().name(PREFIX + "_requests_total").help("Total requests").register();
25+
static final Counter codes =
26+
Counter.build()
27+
.name(PREFIX + "_response_code_total")
28+
.help("Response code total")
29+
.labelNames("code")
30+
.register();
31+
static final Histogram latency =
32+
Histogram.build()
33+
.name(PREFIX + "_request_latency_seconds")
34+
.help("Request latency (seconds)")
35+
.register();
36+
37+
@Override
38+
public Response intercept(Interceptor.Chain chain) throws IOException {
39+
requests.inc();
40+
Histogram.Timer timer = latency.startTimer();
41+
42+
try {
43+
Response response = chain.proceed(chain.request());
44+
codes.labels(Integer.toString(response.code())).inc();
45+
return response;
46+
} finally {
47+
timer.observeDuration();
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)