aboutsummaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/main/java/ai/vespa/metricsproxy/metric/model/prometheus/PrometheusModel.java
blob: 77db2389271b4ac9078acbda8fab26b60ee48c21 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metricsproxy.metric.model.prometheus;

import io.prometheus.client.Collector;
import io.prometheus.client.exporter.common.TextFormat;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author yj-jtakagi
 * @author gjoranv
 */
public class PrometheusModel implements Enumeration<Collector.MetricFamilySamples> {
    private static Logger log = Logger.getLogger(PrometheusModel.class.getName());

    private final Iterator<Collector.MetricFamilySamples> metricFamilySamplesIterator;

    PrometheusModel(List<Collector.MetricFamilySamples> metricFamilySamples) {
        this.metricFamilySamplesIterator = metricFamilySamples.iterator();
    }

    @Override
    public boolean hasMoreElements() {
        return metricFamilySamplesIterator.hasNext();
    }

    @Override
    public Collector.MetricFamilySamples nextElement() {
        return metricFamilySamplesIterator.next();
    }

    public String serialize() {
        var writer = new StringWriter();
        try {
            TextFormat.write004(writer, this);
        } catch (Exception e) {
            throw new PrometheusRenderingException("Could not render metrics. Check the log for details.", e);
        }
        return writer.toString();
    }

}