aboutsummaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/main/java/ai/vespa/metricsproxy/metric/MetricsFormatter.java
blob: dd249c4a21fcf9948270e592cfcf802188128e22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metricsproxy.metric;

import ai.vespa.metricsproxy.service.VespaService;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

/**
 * Format metrics as required by users of the "getMetricsById" rpc method.
 *
 * @author Unknown
 */
public class MetricsFormatter {

    private final boolean includeServiceName;
    private final boolean isSystemMetric;
    private final DecimalFormat df = new DecimalFormat("0.000", new DecimalFormatSymbols(Locale.ENGLISH));

    public MetricsFormatter(boolean includeServiceName, boolean isSystemMetric) {
        this.includeServiceName = includeServiceName;
        this.isSystemMetric = isSystemMetric;
    }

    public String format(VespaService service, String name, Number value) {
        StringBuilder sb = new StringBuilder();

        if (includeServiceName) {
            sb.append(service.getServiceName()).append(".");
        }

        if (isSystemMetric)
            sb.append(toSystemServiceId(service.getConfigId()));
        else
            sb.append(toServiceId(service.getConfigId()));

        sb.append(".")
                .append(formatMetricName(name))
                .append("=");

        if (value instanceof Double) {
            sb.append(df.format(value.doubleValue()));
        } else {
            sb.append(value.toString());
        }

        return sb.toString();
    }

    private static String formatMetricName(String name) {
        name = name.replaceAll("\"", "");
        name = name.replaceAll("\\.", "_");
        return name;
    }

    // E.g. container/default.1 ->  'container.default.1'
    private static String toServiceId(String configId) {
        return "'" + configId.replace("/", ".") + "'";
    }

    // E.g. container/default.1 ->  container.'default.1'
    private static String toSystemServiceId(String configId) {
        String name = configId.replace("/", ".");
        name = name.replaceFirst("\\.", ".'") + "'";
        return name;
    }

}