|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +/* |
| 5 | + * Copyright The OpenTelemetry Authors |
| 6 | + * SPDX-License-Identifier: Apache-2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +package com.azure.monitor.opentelemetry.exporter.implementation; |
| 10 | + |
| 11 | +import io.opentelemetry.api.common.AttributeKey; |
| 12 | +import io.opentelemetry.api.common.AttributeType; |
| 13 | +import java.util.List; |
| 14 | +import java.util.concurrent.ConcurrentHashMap; |
| 15 | +import java.util.concurrent.ConcurrentMap; |
| 16 | +import java.util.function.Function; |
| 17 | + |
| 18 | +// this is a copy of io.opentelemetry.semconv.AttributeKeyTemplate (1.23.1-alpha) |
| 19 | +// because the module that contains that class is not stable, |
| 20 | +// so don't want to take a dependency on it |
| 21 | + |
| 22 | +/** |
| 23 | + * This class provides a handle for creating and caching dynamic / template-type attributes of the |
| 24 | + * form <b><prefix>.<key></b>. The <prefix> is fixed for a template instance while |
| 25 | + * {@link AttributeKey}s can be created and are cached for different values of the <key> part. |
| 26 | + * |
| 27 | + * <p>An example template-type attribute is the set of attributes for HTTP headers: |
| 28 | + * <b>http.request.header.<key></b> |
| 29 | + * |
| 30 | + * @param <T> The type of the nested {@link AttributeKey}s. |
| 31 | + */ |
| 32 | +public final class AttributeKeyTemplate<T> { |
| 33 | + |
| 34 | + private final String prefix; |
| 35 | + private final Function<String, AttributeKey<T>> keyBuilder; |
| 36 | + private final ConcurrentMap<String, AttributeKey<T>> keysCache = new ConcurrentHashMap<>(1); |
| 37 | + |
| 38 | + private AttributeKeyTemplate(String prefix, Function<String, AttributeKey<T>> keyBuilder) { |
| 39 | + this.prefix = prefix; |
| 40 | + this.keyBuilder = keyBuilder; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Create an {@link AttributeKeyTemplate} with type {@link AttributeType#STRING} and the given |
| 45 | + * {@code prefix}. |
| 46 | + */ |
| 47 | + public static AttributeKeyTemplate<String> stringKeyTemplate(String prefix) { |
| 48 | + return new AttributeKeyTemplate<>(prefix, AttributeKey::stringKey); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Create an {@link AttributeKeyTemplate} with type {@link AttributeType#STRING_ARRAY} and the |
| 53 | + * given {@code prefix}. |
| 54 | + */ |
| 55 | + public static AttributeKeyTemplate<List<String>> stringArrayKeyTemplate(String prefix) { |
| 56 | + return new AttributeKeyTemplate<>(prefix, AttributeKey::stringArrayKey); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Create an {@link AttributeKeyTemplate} with type {@link AttributeType#BOOLEAN} and the given |
| 61 | + * {@code prefix}. |
| 62 | + */ |
| 63 | + public static AttributeKeyTemplate<Boolean> booleanKeyTemplate(String prefix) { |
| 64 | + return new AttributeKeyTemplate<>(prefix, AttributeKey::booleanKey); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Create an {@link AttributeKeyTemplate} with type {@link AttributeType#BOOLEAN_ARRAY} and the |
| 69 | + * given {@code prefix}. |
| 70 | + */ |
| 71 | + public static AttributeKeyTemplate<List<Boolean>> booleanArrayKeyTemplate(String prefix) { |
| 72 | + return new AttributeKeyTemplate<>(prefix, AttributeKey::booleanArrayKey); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Create an {@link AttributeKeyTemplate} with type {@link AttributeType#LONG} and the given |
| 77 | + * {@code prefix}. |
| 78 | + */ |
| 79 | + public static AttributeKeyTemplate<Long> longKeyTemplate(String prefix) { |
| 80 | + return new AttributeKeyTemplate<>(prefix, AttributeKey::longKey); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Create an {@link AttributeKeyTemplate} with type {@link AttributeType#LONG_ARRAY} and the given |
| 85 | + * {@code prefix}. |
| 86 | + */ |
| 87 | + public static AttributeKeyTemplate<List<Long>> longArrayKeyTemplate(String prefix) { |
| 88 | + return new AttributeKeyTemplate<>(prefix, AttributeKey::longArrayKey); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Create an {@link AttributeKeyTemplate} with type {@link AttributeType#DOUBLE} and the given |
| 93 | + * {@code prefix}. |
| 94 | + */ |
| 95 | + public static AttributeKeyTemplate<Double> doubleKeyTemplate(String prefix) { |
| 96 | + return new AttributeKeyTemplate<>(prefix, AttributeKey::doubleKey); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Create an {@link AttributeKeyTemplate} with type {@link AttributeType#DOUBLE_ARRAY} and the |
| 101 | + * given {@code prefix}. |
| 102 | + */ |
| 103 | + public static AttributeKeyTemplate<List<Double>> doubleArrayKeyTemplate(String prefix) { |
| 104 | + return new AttributeKeyTemplate<>(prefix, AttributeKey::doubleArrayKey); |
| 105 | + } |
| 106 | + |
| 107 | + private AttributeKey<T> createAttributeKey(String keyName) { |
| 108 | + String key = prefix + "." + keyName; |
| 109 | + return keyBuilder.apply(key); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Returns an {@link AttributeKey} object for the given attribute key whereby the key is the |
| 114 | + * variable part of the full attribute name in a template-typed attribute, for example |
| 115 | + * <b>http.request.header.<key></b>. |
| 116 | + * |
| 117 | + * <p>{@link AttributeKey} objets are being created and cached on the first invocation of this |
| 118 | + * method for a certain key. Subsequent invocations of this method with the same key return the |
| 119 | + * cached object. |
| 120 | + * |
| 121 | + * @param key The variable part of the template-typed attribute name. |
| 122 | + * @return An {@link AttributeKey} object for the given key. |
| 123 | + */ |
| 124 | + public AttributeKey<T> getAttributeKey(String key) { |
| 125 | + return keysCache.computeIfAbsent(key, this::createAttributeKey); |
| 126 | + } |
| 127 | +} |
| 128 | + |
0 commit comments