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

org.restheart.metrics.MetricNameAndLabels Maven / Gradle / Ivy

There is a newer version: 8.1.7
Show newest version
/*-
 * ========================LICENSE_START=================================
 * restheart-commons
 * %%
 * Copyright (C) 2019 - 2024 SoftInstigate
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =========================LICENSE_END==================================
 */
package org.restheart.metrics;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * record for metric name and labels that can be serialized/deserialized to/from string
 *
 * @author Andrea Di Cesare {@literal }
 */
public record MetricNameAndLabels(String name, List labels) {
    public static String SEPARATOR = ".";
    private static String SEPARATOR_REGEX = "\\.";

    public MetricNameAndLabels(String name, List labels) {
        if (name == null) {
            throw new IllegalArgumentException("name cannot be null");
        }

        if (labels == null) {
            throw new IllegalArgumentException("value cannot be null");
        }


        this.name = name.replaceAll("SEPARATOR_REGEX", "_");
        this.labels = labels;
    }

    public static MetricNameAndLabels from(String raw) {
        var name = raw.substring(0, raw.indexOf("."));

        var labels = Arrays.stream(raw.split(SEPARATOR_REGEX))
            .skip(1)
            .map(l -> MetricLabel.from(l))
            .collect(Collectors.toList());

        return new MetricNameAndLabels(name, labels);
    }

    public String toString() {
        var sb = new StringBuilder();
        sb.append(name).append(SEPARATOR);

        sb.append(labels.stream().map(l -> l.toString()).collect(Collectors.joining(SEPARATOR)));
        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy