Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow custom prometheus info metric #83

Merged
merged 7 commits into from
Mar 14, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/main/java/com/ibm/watson/modelmesh/Metrics.java
Original file line number Diff line number Diff line change
@@ -155,7 +155,7 @@ final class PrometheusMetrics implements Metrics {
private final boolean shortNames;
private final EnumMap<Metric, Collector> metricsMap = new EnumMap<>(Metric.class);

public PrometheusMetrics(Map<String, String> params) throws Exception {
public PrometheusMetrics(Map<String, String> params, Map<String, String> customParams) throws Exception {
int port = 2112;
boolean shortNames = true;
boolean https = true;
@@ -230,6 +230,17 @@ public PrometheusMetrics(Map<String, String> params) throws Exception {
}
}

// Custom info metrics
if (!customParams.isEmpty()){
Gauge customMetrics = Gauge.build()
.name(customParams.get("metric_name"))
.help("Info Metrics")
.labelNames("deployment", "slot", "component", "group")
.create();
customMetrics.labels(customParams.get("deployment"), customParams.get("slot"), customParams.get("component"), customParams.get("group"));
registry.register(customMetrics);
}

this.metricServer = new NettyServer(registry, port, https);
this.shortNames = shortNames;

28 changes: 26 additions & 2 deletions src/main/java/com/ibm/watson/modelmesh/ModelMesh.java
Original file line number Diff line number Diff line change
@@ -883,7 +883,9 @@ protected final TProcessor initialize() throws Exception {
// }

// "type" or "type:p1=v1;p2=v2;...;pn=vn"
private static final Pattern METRICS_CONFIG_PATT = Pattern.compile("([a-z]+)(:\\w+=[^;]+(?:;\\w+=[^;]+)*)?");
private static final Pattern METRICS_CONFIG_PATT = Pattern.compile("([a-z;]+)(:\\w+=[^;]+(?:;\\w+=[^;]+)*)?");
// "metric_name" or "metric:name;l1=v1,l2=v2,...,ln=vn,"
private static final Pattern CUSTOM_METRIC_CONFIG_PATT = Pattern.compile("([a-z_:]+);(\\w+=[^;]+(?:;\\w+=[^,]+)*)?");

private static Metrics setUpMetrics() throws Exception {
if (System.getenv("MM_METRICS_STATSD_PORT") != null || System.getenv("MM_METRICS_PROMETHEUS_PORT") != null) {
@@ -916,12 +918,34 @@ private static Metrics setUpMetrics() throws Exception {
params.put(kv[0], kv[1]);
}
}
String customMetricConfig = getStringParameter(MMESH_CUSTOM_ENV_VAR, null);
Map<String, String> customParams;
if (customMetricConfig == null) {
logger.info("{} returned null", MMESH_CUSTOM_ENV_VAR);
customParams = Collections.emptyMap();
} else {
logger.info("{} set to \"{}\"", MMESH_CUSTOM_ENV_VAR, customMetricConfig);
Matcher customMetricMatcher = CUSTOM_METRIC_CONFIG_PATT.matcher(customMetricConfig);
if (!customMetricMatcher.matches()) {
throw new Exception("Invalid metrics configuration provided in env var " + MMESH_CUSTOM_ENV_VAR + ": \""
+ customMetricConfig + "\"");
}
String name = customMetricMatcher.group(1);
String customParamString = customMetricMatcher.group(2);

customParams = new HashMap<>();
customParams.put("metric_name", name);
for (String customParm : customParamString.substring(0).split(",")) {
String[] kv = customParm.split("=");
customParams.put(kv[0], kv[1]);
}
}
try {
switch (type.toLowerCase()) {
case "statsd":
return new Metrics.StatsDMetrics(params);
case "prometheus":
return new Metrics.PrometheusMetrics(params);
return new Metrics.PrometheusMetrics(params, customParams);
case "disabled":
logger.info("Metrics publishing is disabled (env var {}={})", MMESH_METRICS_ENV_VAR, metricsConfig);
return Metrics.NO_OP_METRICS;
Original file line number Diff line number Diff line change
@@ -46,6 +46,7 @@ private ModelMeshEnvVars() {}
public static final String LOAD_FAILURE_EXPIRY_ENV_VAR = "MM_LOAD_FAILURE_EXPIRY_TIME_MS";

public static final String MMESH_METRICS_ENV_VAR = "MM_METRICS";
public static final String MMESH_CUSTOM_ENV_VAR = "MM_INFO_METRICS";

public static final String LOG_EACH_INVOKE_ENV_VAR = "MM_LOG_EACH_INVOKE";
public static final String SEND_DEST_ID_ENV_VAR = "MM_SEND_DEST_ID";
10 changes: 8 additions & 2 deletions src/test/java/com/ibm/watson/modelmesh/ModelMeshMetricsTest.java
Original file line number Diff line number Diff line change
@@ -70,7 +70,9 @@ protected int requestCount() {

@Override
protected Map<String, String> extraEnvVars() {
return ImmutableMap.of("MM_METRICS", "prometheus:port=" + METRICS_PORT + ";scheme=" + SCHEME);
return ImmutableMap.of(
"MM_METRICS", "prometheus:port=" + METRICS_PORT + ";scheme=" + SCHEME,
"MM_INFO_METRICS", "assistant_deployment_info:relabel;deployment=DEPLOYMENT_NAME,slot=SLOT_NAME,component=COMPONENT_NAME,group=GROUP_NAME");
}

@Test
@@ -84,7 +86,7 @@ public void metricsTest() throws Exception {

// verify not found status
ModelStatusInfo status = manageModels.getModelStatus(GetStatusRequest.newBuilder()
.setModelId("i don't exist").build());
.setModelId("I don't exist").build());

assertEquals(ModelStatus.NOT_FOUND, status.getStatus());
assertEquals(0, status.getErrorsCount());
@@ -198,5 +200,9 @@ public void verifyMetrics() throws Exception {
assertEquals(0.0, metrics.get("jvm_buffer_pool_used_buffers{pool=\"mapped\",}")); // mmapped memory not used
assertTrue(metrics.containsKey("jvm_gc_collection_seconds_sum{gc=\"G1 Young Generation\",}"));
assertTrue(metrics.containsKey("jvm_memory_bytes_committed{area=\"heap\",}"));

// Info metrics
assertTrue(metrics.containsKey("assistant_deployment_info:relabel{deployment=\"DEPLOYMENT_NAME\",slot=\"SLOT_NAME\",component=\"COMPONENT_NAME\",group=\"GROUP_NAME\",}"));
assertEquals(0.0, metrics.get("assistant_deployment_info:relabel{deployment=\"DEPLOYMENT_NAME\",slot=\"SLOT_NAME\",component=\"COMPONENT_NAME\",group=\"GROUP_NAME\",}"));
}
}