aboutsummaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/main/java/ai/vespa/metricsproxy/metric/model/MetricsPacket.java
blob: 35761ebcf1835ae8a8ea67cba7d79e9e3bad5ca6 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metricsproxy.metric.model;

import ai.vespa.metricsproxy.metric.Metric;

import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;

import static java.util.Collections.unmodifiableMap;
import static java.util.stream.Collectors.joining;

/**
 * Represents a packet of metrics (with meta information) that belong together because they:
 * <ul>
 *     <li>share both the same dimensions and consumers, AND</li>
 *     <li>represent the same source, e.g. a vespa service or the system hardware.</li>
 * </ul>
 *
 * @author gjoranv
 */
public class MetricsPacket {

    public final int statusCode;
    public final String statusMessage;
    public final long timestamp;
    public final ServiceId service;
    private final Map<MetricId, Number> metrics;
    private final Map<DimensionId, String> dimensions;
    private final Set<ConsumerId> consumers;

    private MetricsPacket(int statusCode, String statusMessage, long timestamp, ServiceId service,
                          Map<MetricId, Number> metrics, Map<DimensionId, String> dimensions, Set<ConsumerId> consumers ) {
        this.statusCode = statusCode;
        this.statusMessage = statusMessage;
        this.timestamp = timestamp;
        this.service = service;
        this.metrics = metrics;
        this.dimensions = dimensions;
        this.consumers = Set.copyOf(consumers);
    }

    public Map<MetricId, Number> metrics() { return unmodifiableMap(metrics); }
    public Map<DimensionId, String> dimensions() { return unmodifiableMap(dimensions); }
    public Set<ConsumerId> consumers() { return consumers;}

    @Override
    public String toString() {
        return "MetricsPacket{" +
                "statusCode=" + statusCode +
                ", statusMessage='" + statusMessage + '\'' +
                ", timestamp=" + timestamp +
                ", service=" + service.id +
                ", metrics=" + idMapToString(metrics, id -> id.id) +
                ", dimensions=" + idMapToString(dimensions, id -> id.id) +
                ", consumers=" + consumers.stream().map(id -> id.id).collect(joining(",", "[", "]")) +
                '}';
    }

    private static <K,V> String idMapToString(Map<K,V> map, Function<K, String> idMapper) {
        return map.entrySet().stream()
                .map(entry -> idMapper.apply(entry.getKey()) + "=" + entry.getValue())
                .collect(joining(",", "{", "}"));
    }

    public static class Builder {

        // Set defaults here, and use null guard in all setters.
        // Except for 'service' for which we require an explicit non-null value.
        private ServiceId service;
        private int statusCode = 0;
        private String statusMessage = "";
        private long timestamp = 0L;
        private Map<MetricId, Number> metrics = new LinkedHashMap<>();
        private final Map<DimensionId, String> dimensions = new LinkedHashMap<>();
        private Set<ConsumerId> consumers = Collections.emptySet();

        public Builder(ServiceId service) {
            Objects.requireNonNull(service, "Service cannot be null.");
            this.service = service;
        }

        public Builder service(ServiceId service) {
            if (service == null) throw new IllegalArgumentException("Service cannot be null.");
            this.service = service;
            return this;
        }

        public Builder statusCode(Integer statusCode) {
            if (statusCode != null) this.statusCode = statusCode;
            return this;
        }

        public Builder statusMessage(String statusMessage) {
            if (statusMessage != null) this.statusMessage = statusMessage;
            return this;
        }

        public Builder timestamp(Long timestamp) {
            if (timestamp != null) this.timestamp = timestamp;
            return this;
        }

        public Builder putMetrics(Collection<Metric> extraMetrics) {
            if (extraMetrics != null)
                extraMetrics.forEach(metric -> metrics.put(metric.getName(), metric.getValue()));
            return this;
        }

        public Builder putMetric(MetricId id, Number value) {
            metrics.put(id, value);
            return this;
        }

        public Builder retainMetrics(Set<MetricId> idsToRetain) {
            metrics.keySet().retainAll(idsToRetain);
            return this;
        }

        public Builder applyOutputNames(Map<MetricId, List<MetricId>> outputNamesById) {
            Map<MetricId, Number> newMetrics = new LinkedHashMap<>();
            outputNamesById.forEach((id, outputNames) -> {
                if (metrics.containsKey(id))
                    outputNames.forEach(outputName -> newMetrics.put(outputName, metrics.get(id)));
            });
            metrics = newMetrics;
            return this;
        }

        public Builder putDimension(DimensionId id, String value) {
            dimensions.put(id, value);
            return this;
        }

        public Builder putDimensions(Map<DimensionId, String> extraDimensions) {
            if (extraDimensions != null) dimensions.putAll(extraDimensions);
            return this;
        }

        public Builder putDimensionsIfAbsent(Map<DimensionId, String> extraDimensions) {
            if (extraDimensions != null) extraDimensions.forEach(dimensions::putIfAbsent);
            return this;
        }

        /**
         * Returns a modifiable copy of the dimension IDs of this builder, usually for use with {@link #retainDimensions(Collection)}.
         */
        public Set<DimensionId> getDimensionIds() {
            return new LinkedHashSet<>(dimensions.keySet());
        }

        public String getDimensionValue(DimensionId id) {
            return dimensions.get(id);
        }

        public Builder retainDimensions(Collection<DimensionId> idsToRetain) {
            dimensions.keySet().retainAll(idsToRetain);
            return this;
        }

        public Builder addConsumers(Set<ConsumerId> extraConsumers) {
            if ((extraConsumers != null) && !extraConsumers.isEmpty()) {
                if (consumers.isEmpty()) {
                    if (extraConsumers.size() == 1) {
                        consumers = Collections.singleton(extraConsumers.iterator().next());
                        return this;
                    }
                    consumers = new LinkedHashSet<>(extraConsumers.size());
                } else if (consumers.size() == 1) {
                    var copy = new LinkedHashSet<ConsumerId>(extraConsumers.size() + 1);
                    copy.addAll(consumers);
                    consumers = copy;
                }
                consumers.addAll(extraConsumers);
            }
            return this;
        }

        public boolean hasConsumer(ConsumerId id) {
            return consumers.contains(id);
        }

        public MetricsPacket build() {
            return new MetricsPacket(statusCode, statusMessage, timestamp, service, metrics, dimensions, consumers);
        }

        public boolean hasMetrics() {
            return ! metrics.isEmpty();
        }

        public Instant getTimestamp() { return Instant.ofEpochSecond(timestamp); }

    }

}