Skip to content

Commit

Permalink
Extended jinja template to generate template-type semantic attributes.
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 Sep 21, 2023
1 parent 520c5ef commit 360ece4
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ dependencies {
}

// start - define tasks to download, unzip, and generate from opentelemetry/semantic-conventions
var generatorVersion = "0.18.0"
var generatorVersion = "0.21.0"
val semanticConventionsRepoZip = "https://github.com/open-telemetry/semantic-conventions/archive/v$semanticConventionsVersion.zip"
val schemaUrl = "https://opentelemetry.io/schemas/$semanticConventionsVersion"

Expand Down
25 changes: 25 additions & 0 deletions buildscripts/templates/SemanticAttributes.java.j2
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ import static io.opentelemetry.api.common.AttributeKey.longKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.api.common.AttributeKey.stringArrayKey;

import static io.opentelemetry.semconv.AttributeKeyTemplate.stringArrayKeyTemplate;
import static io.opentelemetry.semconv.AttributeKeyTemplate.stringKeyTemplate;

import io.opentelemetry.api.common.AttributeKey;
import java.util.List;

Expand Down Expand Up @@ -84,6 +87,28 @@ public final class {{class}} {
{%- endif %}
public static final AttributeKey<{{upFirst(to_java_return_type(attribute.attr_type | string))}}> {{attribute.fqn | to_const_name}} = {{to_java_key_type(attribute.attr_type | string)}}("{{attribute.fqn}}");
{%- endfor %}
{%- for attribute_template in attribute_templates if attribute_template.is_local and not attribute_template.ref %}

/**
* {{attribute_template.brief | render_markdown(code="{{@code {0}}}", paragraph="{0}")}}
{%- if attribute_template.note %}
*
* <p>Notes:
{# NOTE: replace("> ", "") removes the following problematic characters which produce mangled javadoc: #}
{# https://github.com/open-telemetry/semantic-conventions/blob/c83a10a9c33c18a769835e959200d0e24dc708fe/model/resource/k8s.yaml#L34-L38 #}
<ul> {{attribute_template.note | replace("> ", "") | render_markdown(code="{{@code {0}}}", paragraph="<li>{0}</li>", list="{0}")}} </ul>

{%- endif %}
{%- if (attribute_template.stability | string()) == "StabilityLevel.DEPRECATED" %}
*
* @deprecated {{attribute_template.brief | to_doc_brief}}.
{%- endif %}
*/
{%- if (attribute_template.stability | string()) == "StabilityLevel.DEPRECATED" %}
@Deprecated
{%- endif %}
public static final AttributeKeyTemplate<{{upFirst(to_java_return_type(attribute_template.instantiated_type | string))}}> {{attribute_template.fqn | to_const_name}} = {{to_java_key_type(attribute_template.instantiated_type | string)}}Template("{{attribute_template.fqn}}");
{%- endfor %}

// Enum definitions
{%- for attribute in attributes if attribute.is_local and not attribute.ref %}
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/io/opentelemetry/semconv/AttributeKeyTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.opentelemetry.semconv;

import io.opentelemetry.api.common.AttributeKey;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;

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;

AttributeKeyTemplate(String prefix, Function<String, AttributeKey<T>> keyBuilder) {
this.prefix = prefix;
this.keyBuilder = keyBuilder;
}

static AttributeKeyTemplate<String> stringKeyTemplate(String prefix) {
return new AttributeKeyTemplate<String>(prefix, AttributeKey::stringKey);
}

static AttributeKeyTemplate<List<String>> stringArrayKeyTemplate(String prefix) {
return new AttributeKeyTemplate<List<String>>(prefix, AttributeKey::stringArrayKey);
}

static AttributeKeyTemplate<Boolean> booleanKeyTemplate(String prefix) {
return new AttributeKeyTemplate<Boolean>(prefix, AttributeKey::booleanKey);
}

static AttributeKeyTemplate<List<Boolean>> booleanArrayKeyTemplate(String prefix) {
return new AttributeKeyTemplate<List<Boolean>>(prefix, AttributeKey::booleanArrayKey);
}

static AttributeKeyTemplate<Long> longKeyTemplate(String prefix) {
return new AttributeKeyTemplate<Long>(prefix, AttributeKey::longKey);
}

static AttributeKeyTemplate<List<Long>> longArrayKeyTemplate(String prefix) {
return new AttributeKeyTemplate<List<Long>>(prefix, AttributeKey::longArrayKey);
}

static AttributeKeyTemplate<Double> doubleKeyTemplate(String prefix) {
return new AttributeKeyTemplate<Double>(prefix, AttributeKey::doubleKey);
}

static AttributeKeyTemplate<List<Double>> doubleArrayKeyTemplate(String prefix) {
return new AttributeKeyTemplate<List<Double>>(prefix, AttributeKey::doubleArrayKey);
}

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

public AttributeKey<T> getAttributeKey(String key) {
return keysCache.computeIfAbsent(key, this::createAttributeKey);
}
}

0 comments on commit 360ece4

Please sign in to comment.