All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.prometheus.metrics.model.snapshots.Labels Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
package io.prometheus.metrics.model.snapshots;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;

import static io.prometheus.metrics.model.snapshots.PrometheusNaming.isValidLabelName;
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.prometheusName;

/**
 * Immutable set of name/value pairs, sorted by name.
 */
public class Labels implements Comparable, Iterable

* However, for debugging it's better to show the original names rather than the Prometheus names. */ @Override public String toString() { StringBuilder b = new StringBuilder(); b.append("{"); for (int i = 0; i < names.length; i++) { if (i > 0) { b.append(","); } b.append(names[i]); b.append("=\""); appendEscapedLabelValue(b, values[i]); b.append("\""); } b.append("}"); return b.toString(); } private void appendEscapedLabelValue(StringBuilder b, String value) { for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); switch (c) { case '\\': b.append("\\\\"); break; case '\"': b.append("\\\""); break; case '\n': b.append("\\n"); break; default: b.append(c); } } } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Labels labels = (Labels) o; return labels.hasSameNames(this) && labels.hasSameValues(this); } @Override public int hashCode() { int result = Arrays.hashCode(prometheusNames); result = 31 * result + Arrays.hashCode(values); return result; } public static Builder builder() { return new Builder(); } public static class Builder { private final List names = new ArrayList<>(); private final List values = new ArrayList<>(); private Builder() { } /** * Add a label. Call multiple times to add multiple labels. */ public Builder label(String name, String value) { names.add(name); values.add(value); return this; } public Labels build() { return Labels.of(names, values); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy