Skip to content

Commit 0273c20

Browse files
committed
report some basic information about itself using the prometheus client
1 parent d5bf9d6 commit 0273c20

File tree

1 file changed

+61
-12
lines changed

1 file changed

+61
-12
lines changed

src/main/java/io/radanalytics/operator/Entrypoint.java

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import io.fabric8.kubernetes.client.KubernetesClient;
77
import io.fabric8.kubernetes.client.Watch;
88
import io.fabric8.kubernetes.client.utils.HttpClientUtils;
9+
import io.prometheus.client.Gauge;
910
import io.prometheus.client.exporter.HTTPServer;
1011
import io.prometheus.client.hotspot.DefaultExports;
12+
import io.prometheus.client.log4j.InstrumentedAppender;
1113
import io.radanalytics.operator.common.AbstractOperator;
1214
import io.radanalytics.operator.common.AnsiColors;
1315
import io.radanalytics.operator.common.OperatorConfig;
@@ -45,23 +47,25 @@ public class Entrypoint {
4547

4648
public static ExecutorService EXECUTORS = Executors.newFixedThreadPool(10);
4749

50+
private static OperatorConfig config;
51+
private static KubernetesClient client;
52+
4853
public static void main(String[] args) {
4954
log.info("Starting..");
50-
OperatorConfig config = OperatorConfig.fromMap(System.getenv());
51-
KubernetesClient client = new DefaultKubernetesClient();
52-
boolean isOpenshift = isOnOpenShift(client);
53-
CompletableFuture<Void> future = run(client, isOpenshift, config).exceptionally(ex -> {
55+
config = OperatorConfig.fromMap(System.getenv());
56+
client = new DefaultKubernetesClient();
57+
boolean isOpenshift = isOnOpenShift();
58+
CompletableFuture<Void> future = run(isOpenshift).exceptionally(ex -> {
5459
log.error("Unable to start operator for one or more namespaces", ex);
5560
System.exit(1);
5661
return null;
5762
});
5863
if (config.isMetrics()) {
59-
CompletableFuture<Optional<HTTPServer>> maybeMetricServer = future.thenCompose(s -> runMetrics(isOpenshift, config));
60-
// todo: shutdown hook and top it if necessary
64+
CompletableFuture<Optional<HTTPServer>> maybeMetricServer = future.thenCompose(s -> runMetrics(isOpenshift));
6165
}
6266
}
6367

64-
private static CompletableFuture<Void> run(KubernetesClient client, boolean isOpenShift, OperatorConfig config) {
68+
private static CompletableFuture<Void> run(boolean isOpenShift) {
6569
printInfo();
6670

6771
if (isOpenShift) {
@@ -73,20 +77,20 @@ private static CompletableFuture<Void> run(KubernetesClient client, boolean isOp
7377
List<CompletableFuture> futures = new ArrayList<>();
7478
if (null == config.getNamespaces()) { // get the current namespace
7579
String namespace = client.getNamespace();
76-
CompletableFuture future = runForNamespace(client, isOpenShift, namespace, config.getReconciliationIntervalS(), 0);
80+
CompletableFuture future = runForNamespace(isOpenShift, namespace, config.getReconciliationIntervalS(), 0);
7781
futures.add(future);
7882
} else {
7983
Iterator<String> ns;
8084
int i;
8185
for (ns = config.getNamespaces().iterator(), i = 0; i < config.getNamespaces().size(); i++) {
82-
CompletableFuture future = runForNamespace(client, isOpenShift, ns.next(), config.getReconciliationIntervalS(), i);
86+
CompletableFuture future = runForNamespace(isOpenShift, ns.next(), config.getReconciliationIntervalS(), i);
8387
futures.add(future);
8488
}
8589
}
8690
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[]{}));
8791
}
8892

89-
private static CompletableFuture<Optional<HTTPServer>> runMetrics(boolean isOpenShift, OperatorConfig config) {
93+
private static CompletableFuture<Optional<HTTPServer>> runMetrics(boolean isOpenShift) {
9094
HTTPServer httpServer = null;
9195
try {
9296
log.info("Starting a simple HTTP server for exposing internal metrics..");
@@ -104,7 +108,7 @@ private static CompletableFuture<Optional<HTTPServer>> runMetrics(boolean isOpen
104108
return CompletableFuture.supplyAsync(() -> maybeServer);
105109
}
106110

107-
private static CompletableFuture<Void> runForNamespace(KubernetesClient client, boolean isOpenShift, String namespace, long reconInterval, int delay) {
111+
private static CompletableFuture<Void> runForNamespace(boolean isOpenShift, String namespace, long reconInterval, int delay) {
108112
List<ClassLoader> classLoadersList = new LinkedList<>();
109113
classLoadersList.add(ClasspathHelper.contextClassLoader());
110114
classLoadersList.add(ClasspathHelper.staticClassLoader());
@@ -172,7 +176,7 @@ private static CompletableFuture<Void> runForNamespace(KubernetesClient client,
172176
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[]{}));
173177
}
174178

175-
private static boolean isOnOpenShift(KubernetesClient client) {
179+
private static boolean isOnOpenShift() {
176180
URL kubernetesApi = client.getMasterUrl();
177181

178182
HttpUrl.Builder urlBuilder = new HttpUrl.Builder();
@@ -217,11 +221,56 @@ private static void printInfo() {
217221
} catch (Exception e) {
218222
// ignore, not critical
219223
}
224+
225+
if(config.isMetrics()) {
226+
registerMetrics(gitSha, version);
227+
}
228+
220229
log.info("\n{}Operator{} has started in version {}{}{}. {}\n", re(), xx(), gr(),
221230
version, xx(), FOO);
222231
if (!gitSha.isEmpty()) {
223232
log.info("Git sha: {}{}{}", ye(), gitSha, xx());
224233
}
225234
log.info("==================\n");
226235
}
236+
237+
private static void registerMetrics(String gitSha, String version) {
238+
List<String> labels = new ArrayList<>();
239+
List<String> values = new ArrayList<>();
240+
241+
labels.addAll(Arrays.asList("gitSha", "version",
242+
"CRD",
243+
"COLORS",
244+
OperatorConfig.WATCHED_NAMESPACE,
245+
OperatorConfig.METRICS,
246+
OperatorConfig.METRICS_JVM,
247+
OperatorConfig.METRICS_PORT,
248+
OperatorConfig.FULL_RECONCILIATION_INTERVAL_S,
249+
OperatorConfig.OPERATOR_OPERATION_TIMEOUT_MS
250+
));
251+
values.addAll(Arrays.asList(gitSha, version,
252+
Optional.ofNullable(System.getenv().get("CRD")).orElse("false"),
253+
Optional.ofNullable(System.getenv().get("COLORS")).orElse("true"),
254+
null == config.getNamespaces() ? client.getNamespace() : config.getNamespaces().toString(),
255+
String.valueOf(config.isMetrics()),
256+
String.valueOf(config.isMetricsJvm()),
257+
String.valueOf(config.getMetricsPort()),
258+
String.valueOf(config.getReconciliationIntervalS()),
259+
String.valueOf(config.getOperationTimeoutMs())
260+
));
261+
262+
Gauge.build()
263+
.name("operator_info")
264+
.help("Basic information about the abstract operator library.")
265+
.labelNames(labels.toArray(new String[]{}))
266+
.register()
267+
.labels(values.toArray(new String[]{}))
268+
.set(1);
269+
270+
// add log appender for metrics
271+
final org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
272+
InstrumentedAppender metricsLogAppender = new InstrumentedAppender();
273+
metricsLogAppender.setName("metrics");
274+
rootLogger.addAppender(metricsLogAppender);
275+
}
227276
}

0 commit comments

Comments
 (0)