Skip to content

Commit

Permalink
javadocs, removed sanitization, lazy initialization
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Wert <alexander.wert@elastic.co>
  • Loading branch information
AlexanderWert committed Nov 12, 2023
1 parent e58f9eb commit 84be830
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/java/io/opentelemetry/semconv/AttributeKeyTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

public final class AttributeKeyTemplate<T> {

private final ConcurrentMap<String, AttributeKey<T>> keysCache = new ConcurrentHashMap<>();
private final String prefix;
private final Function<String, AttributeKey<T>> keyBuilder;
private ConcurrentMap<String, AttributeKey<T>> keysCache;

AttributeKeyTemplate(String prefix, Function<String, AttributeKey<T>> keyBuilder) {
this.prefix = prefix;
Expand Down Expand Up @@ -56,11 +56,23 @@ static AttributeKeyTemplate<List<Double>> doubleArrayKeyTemplate(String prefix)
}

private AttributeKey<T> createAttributeKey(String keyName) {
String key = prefix + "." + keyName.toLowerCase(Locale.ROOT).replace('-', '_');
String key = prefix + "." + keyName.toLowerCase(Locale.ROOT);
return keyBuilder.apply(key);
}

/**
* Returns an {@link AttributeKey} object for the given attribute key whereby the key is the variable part of the full
* attribute name in a template-typed attribute, for example `http.request.header.<key>`.
* {@link AttributeKey} objets are being created and cached on the first invocation of this method for a certain key.
* Subsequent invocations of this method with the same key return the cached object.
*
* @param key The variable part of the template-typed attribute name.
* @return An {@link AttributeKey} object for the given key.
*/
public AttributeKey<T> getAttributeKey(String key) {
if (keysCache == null) {
keysCache = new ConcurrentHashMap<>(1);
}
return keysCache.computeIfAbsent(key, this::createAttributeKey);
}
}

0 comments on commit 84be830

Please sign in to comment.