Skip to content

Commit

Permalink
feat: Allow custom prometheus info metric
Browse files Browse the repository at this point in the history
Signed-off-by: Rafael Vasquez <[email protected]>
  • Loading branch information
rafvasq committed Feb 8, 2023
1 parent 38c1385 commit b8d0567
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/main/java/com/ibm/watson/modelmesh/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public PrometheusMetrics(Map<String, String> params) throws Exception {
int port = 2112;
boolean shortNames = true;
boolean https = true;
boolean customMetrics = false;
String memMetrics = "all"; // default to all
for (Entry<String, String> ent : params.entrySet()) {
switch (ent.getKey()) {
Expand All @@ -183,6 +184,9 @@ public PrometheusMetrics(Map<String, String> params) throws Exception {
case "mem_detail":
memMetrics = ent.getValue();
break;
case "metric_name":
customMetrics = true;
break;
default:
throw new Exception("Unrecognized metrics config parameter: " + ent.getKey());
}
Expand Down Expand Up @@ -230,6 +234,21 @@ public PrometheusMetrics(Map<String, String> params) throws Exception {
}
}

// Custom info metrics
if (customMetrics){
@SuppressWarnings("rawtypes")
SimpleCollector.Builder builder;
builder = Gauge.build();
Collector collector = (
builder
.name(params.get("metric_name"))
.help("Custom Info Metrics")
.labelNames(params.get("deployment"), params.get("slot"), params.get("component"), params.get("group"))
.create()
);
registry.register(collector);
}

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

Expand Down
22 changes: 21 additions & 1 deletion src/main/java/com/ibm/watson/modelmesh/ModelMesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -916,6 +918,24 @@ private static Metrics setUpMetrics() throws Exception {
params.put(kv[0], kv[1]);
}
}
String customMetricConfig = getStringParameter(MMESH_CUSTOM_ENV_VAR, null);
if (customMetricConfig == null) {
logger.info("{} returned null", MMESH_CUSTOM_ENV_VAR);
} 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);
params.put("metric_name", name);
for (String customParm : customParamString.substring(1).split(",")) {
String[] kv = customParm.split("=");
params.put(kv[0], kv[1]);
}
}
try {
switch (type.toLowerCase()) {
case "statsd":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit b8d0567

Please sign in to comment.