Skip to content

Commit

Permalink
added prometheus endpoint and server metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
boglesby committed Jan 17, 2020
1 parent 5252f0c commit 17449b2
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.pivotal.test.client.metrics;

public class MetricsHandlerFunction {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.pivotal.test.client.metrics;

import io.pivotal.test.client.function.AdminFunctions;
import io.pivotal.test.client.service.TradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.TreeMap;

@Component
public class ServerMetricsProvider {

@Autowired
private TradeService service;

@Autowired
private AdminFunctions adminFunctions;

public Map getServerMetrics(String type) {
Map serverMetrics = null;
if (type.equals("all") || type.equals("server")) {
Iterable serverMetricsIter = (Iterable) this.adminFunctions.getMetrics();
serverMetrics = (Map) serverMetricsIter.iterator().next();
}
return serverMetrics;
}

private Map getClientMetrics(String type) {
Map clientMetrics = null;
if (type.equals("all") || type.equals("client")) {
clientMetrics = new TreeMap();
addOperationMetrics(clientMetrics);
MetricsHelper.addOSMetrics(clientMetrics);
MetricsHelper.addGCMetrics(clientMetrics);
MetricsHelper.addMemoryMetrics(clientMetrics);
}
return clientMetrics;
}

private void addOperationMetrics(Map clientMetrics) {
Map operationMetrics = new TreeMap();
clientMetrics.put("operations", operationMetrics);
operationMetrics.put("puts", this.service.getPutOperations());
operationMetrics.put("gets", this.service.getGetOperations());
operationMetrics.put("destroys", this.service.getDestroyOperations());
operationMetrics.put("cusipQueries", this.service.getQueryByCusipOperations());
operationMetrics.put("functionUpdates", this.service.getFunctionUpdateOperations());
}

public Object getMetrics(String type) {
return new ClientAndServerMetrics(getClientMetrics(type), getServerMetrics(type));
}

public int getMetric(String metricName) {
System.out.println(Thread.currentThread().getName() + ": ServerMetricsProvider.getMetric metricName=" + metricName);
return 100;
//return this.serverMetrics.get(metricName).get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.pivotal.test.client.metrics;

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.pivotal.test.client.service.TradeService;
import org.springframework.beans.factory.annotation.Autowired;

public class OperationMetricsBinder implements MeterBinder {

@Autowired
private TradeService service;

@Override
public void bindTo(MeterRegistry registry) {
Gauge
.builder("client.puts", service, TradeService::getPutOperations)
.description("Number of put operations")
.register(registry);

Gauge
.builder("client.gets", service, TradeService::getGetOperations)
.description("Number of get operations")
.register(registry);

Gauge
.builder("client.destroys", service, TradeService::getDestroyOperations)
.description("Number of destroy operations")
.register(registry);

Gauge
.builder("client.queries.by.cusip", service, TradeService::getQueryByCusipOperations)
.description("Number of queries by cusip operations")
.register(registry);

Gauge
.builder("client.function.updates", service, TradeService::getFunctionUpdateOperations)
.description("Number of function update operations")
.register(registry);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.pivotal.test.client.metrics;

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.MeterBinder;
import org.springframework.beans.factory.annotation.Autowired;

public class ServerMetricsBuilder implements MeterBinder {

@Autowired
private MetricsProvider provider;

@Override
public void bindTo(MeterRegistry registry) {
MetricsHandlerFunction function = (serverName, metric) -> {
//Gauge gauge = Gauge.builder(metricName, provider, provider -> provider.getServerMetric(metricName)).register(registry);
System.out.println(Thread.currentThread().getName() + ": XXX ServerMetricsBuilder.bindTo serverName=" + serverName + "; metric=" + metric);
};
this.provider.processServerMetrics(function);
}
}
32 changes: 32 additions & 0 deletions client/src/main/java/io/pivotal/test/metrics/Metric.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.pivotal.test.metrics;

import java.io.Serializable;

public class Metric implements Serializable {

private String name;

private Number value;

private boolean isCounter;

private static final long serialVersionUID = 1L;

public Metric(String name, Number value, boolean isCounter) {
this.name = name;
this.value = value;
this.isCounter = isCounter;
}

public String getName() {
return this.name;
}

public Number getValue() {
return this.value;
}

public boolean isCounter() {
return this.isCounter;
}
}
4 changes: 4 additions & 0 deletions server/bin/exportstacktraces.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gfsh \
-e "connect --url=$URL --user=$USERNAME --password=$PASSWORD --skip-ssl-validation" \
-e "set variable --name=APP_RESULT_VIEWER --value=any" \
-e "export stack-traces"
32 changes: 32 additions & 0 deletions server/src/main/java/io/pivotal/test/metrics/Metric.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.pivotal.test.server.metrics;

import java.io.Serializable;

public class Metric implements Serializable {

private String name;

private Number value;

private boolean isCounter;

private static final long serialVersionUID = 1L;

public Metric(String name, Number value, boolean isCounter) {
this.name = name;
this.value = value;
this.isCounter = isCounter;
}

public String getName() {
return this.name;
}

public Number getValue() {
return this.value;
}

public boolean isCounter() {
return this.isCounter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public static void addGCMetrics(Map allMetrics) {
}
}

public static void addGCMetrics(List<Metric> allMetrics) {
List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean gcBean : gcBeans) {
allMetrics.add(new Metric(gcBean.getName() + "_collections", gcBean.getCollectionCount(), true));
allMetrics.add(new Metric(gcBean.getName() + "_collectionTime", gcBean.getCollectionTime(), true));
}
}

public static void addMemoryMetrics(Map allMetrics) {
Map memoryMetrics = new TreeMap();
allMetrics.put("memory", memoryMetrics);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.pivotal.test.server.function;

public class GetMetricsListFunction {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.pivotal.test.server.function;

import io.pivotal.test.server.metrics.MetricsHelper;
import io.pivotal.test.metrics.MetricsHelper;
import org.apache.geode.cache.execute.Function;
import org.apache.geode.cache.execute.FunctionContext;

Expand All @@ -13,6 +13,7 @@ public class GetMetricsFunction implements Function {
public void execute(FunctionContext context) {
Map allMetrics = new TreeMap();
MetricsHelper.addOSMetrics(allMetrics);
MetricsHelper.addVMMetrics(allMetrics);
MetricsHelper.addGCMetrics(allMetrics);
MetricsHelper.addMemoryMetrics(allMetrics);
context.getResultSender().lastResult(allMetrics);
Expand Down

0 comments on commit 17449b2

Please sign in to comment.