|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.telemetry.metrics.tags; |
| 10 | + |
| 11 | +import org.opensearch.common.annotation.ExperimentalApi; |
| 12 | + |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Objects; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class to create tags for a meter. |
| 20 | + * |
| 21 | + * @opensearch.experimental |
| 22 | + */ |
| 23 | +@ExperimentalApi |
| 24 | +public class Tags { |
| 25 | + private final Map<String, Object> tagsMap; |
| 26 | + /** |
| 27 | + * Empty value. |
| 28 | + */ |
| 29 | + public final static Tags EMPTY = new Tags(Collections.emptyMap()); |
| 30 | + |
| 31 | + /** |
| 32 | + * Factory method. |
| 33 | + * @return tags. |
| 34 | + */ |
| 35 | + public static Tags create() { |
| 36 | + return new Tags(new HashMap<>()); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Constructor. |
| 41 | + */ |
| 42 | + private Tags(Map<String, Object> tagsMap) { |
| 43 | + this.tagsMap = tagsMap; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Add String attribute. |
| 48 | + * @param key key |
| 49 | + * @param value value |
| 50 | + * @return Same instance. |
| 51 | + */ |
| 52 | + public Tags addTag(String key, String value) { |
| 53 | + Objects.requireNonNull(value, "value cannot be null"); |
| 54 | + tagsMap.put(key, value); |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Add long attribute. |
| 60 | + * @param key key |
| 61 | + * @param value value |
| 62 | + * @return Same instance. |
| 63 | + */ |
| 64 | + public Tags addTag(String key, long value) { |
| 65 | + tagsMap.put(key, value); |
| 66 | + return this; |
| 67 | + }; |
| 68 | + |
| 69 | + /** |
| 70 | + * Add double attribute. |
| 71 | + * @param key key |
| 72 | + * @param value value |
| 73 | + * @return Same instance. |
| 74 | + */ |
| 75 | + public Tags addTag(String key, double value) { |
| 76 | + tagsMap.put(key, value); |
| 77 | + return this; |
| 78 | + }; |
| 79 | + |
| 80 | + /** |
| 81 | + * Add boolean attribute. |
| 82 | + * @param key key |
| 83 | + * @param value value |
| 84 | + * @return Same instance. |
| 85 | + */ |
| 86 | + public Tags addTag(String key, boolean value) { |
| 87 | + tagsMap.put(key, value); |
| 88 | + return this; |
| 89 | + }; |
| 90 | + |
| 91 | + /** |
| 92 | + * Returns the attribute map. |
| 93 | + * @return tags map |
| 94 | + */ |
| 95 | + public Map<String, ?> getTagsMap() { |
| 96 | + return Collections.unmodifiableMap(tagsMap); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments