Skip to content

Commit 2b8c285

Browse files
committed
Fix codegen template to handle all attribute types
1 parent d503f1b commit 2b8c285

3 files changed

Lines changed: 34 additions & 13 deletions

File tree

semconv/templates/registry/kotlin/kotlin_event_template.kt.j2

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
package io.opentelemetry.android.semconv.events
88

9+
import io.opentelemetry.api.common.AttributeKey
910
import io.opentelemetry.api.common.Attributes
1011
import io.opentelemetry.api.logs.Logger
1112

12-
// Autogenerated OpenTelemetry Android semantic convention event for '{{ctx.name}}'.
13+
// Autogenerated OpenTelemetry Android semantic convention event for '{{ ctx.name }}'.
1314
//
1415
// DO NOT EDIT - AUTOGENERATED FILE
1516
{{ [ctx.brief, concat_if("\n\nNotes:\n\n", ctx.note)] | comment }}
@@ -30,7 +31,14 @@ class {{ my_class_name }}(
3031
val attributes = Attributes.builder()
3132
{%- for attribute in ctx.attributes %}
3233
{%- set required = attribute.requirement_level == "required" %}
34+
{%- if attribute.type in ["string[]", "boolean[]", "int[]", "double[]"] %}
35+
{%- set key = 'AttributeKey.' ~ (attribute.type | map_text("kotlin_key_factory")) ~ '("' ~ attribute.name ~ '")' %}
3336
{%- if required %}
37+
attributes.put({{ key }}, {{ attribute.name | camel_case }})
38+
{%- else %}
39+
{{ attribute.name | camel_case }}?.let { attributes.put({{ key }}, it) }
40+
{%- endif %}
41+
{%- elif required %}
3442
attributes.put("{{ attribute.name }}", {{ attribute.name | camel_case }})
3543
{%- else %}
3644
{{ attribute.name | camel_case }}?.let { attributes.put("{{ attribute.name }}", it) }

semconv/templates/registry/kotlin/kotlin_semconv_template.kt.j2

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,13 @@ object {{ my_class_name }} {
2121
@Deprecated("{{ attribute.deprecated.note | replace('\n', ' ') | trim }}")
2222
{%- endif %}
2323
const val {{ attribute.name | screaming_snake_case }}: String = "{{ attribute.name }}"
24+
{%- if attribute.type != "any" %}
25+
{%- set itype = attribute.type | instantiated_type %}
26+
{%- if itype in ["string", "boolean", "int", "double", "string[]", "boolean[]", "int[]", "double[]"] %}
2427
@JvmField
25-
{%- if attribute.type == "string" %}
26-
val {{ attribute.name | screaming_snake_case }}_KEY: AttributeKey<String> =
27-
AttributeKey.stringKey({{ attribute.name | screaming_snake_case }})
28-
{%- elif attribute.type == "boolean" %}
29-
val {{ attribute.name | screaming_snake_case }}_KEY: AttributeKey<Boolean> =
30-
AttributeKey.booleanKey({{ attribute.name | screaming_snake_case }})
31-
{%- elif attribute.type == "int" %}
32-
val {{ attribute.name | screaming_snake_case }}_KEY: AttributeKey<Long> =
33-
AttributeKey.longKey({{ attribute.name | screaming_snake_case }})
34-
{%- elif attribute.type == "double" %}
35-
val {{ attribute.name | screaming_snake_case }}_KEY: AttributeKey<Double> =
36-
AttributeKey.doubleKey({{ attribute.name | screaming_snake_case }})
28+
val {{ attribute.name | screaming_snake_case }}_KEY: AttributeKey<{{ itype | map_text("kotlin_key_generic") }}> =
29+
AttributeKey.{{ itype | map_text("kotlin_key_factory") }}({{ attribute.name | screaming_snake_case }})
30+
{%- endif %}
3731
{%- endif %}
3832
{%- endfor %}
3933
}

semconv/templates/registry/kotlin/weaver.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ templates:
4242
note: .note,
4343
deprecated: .deprecated,
4444
attributes: ((.attributes // [])
45+
| map(select(.type != "any"))
4546
| unique_by(.name))
4647
})
4748
application_mode: each
@@ -50,6 +51,24 @@ text_maps:
5051
kotlin_enum_type:
5152
int: Long
5253
string: String
54+
kotlin_key_generic:
55+
string: String
56+
boolean: Boolean
57+
int: Long
58+
double: Double
59+
"string[]": List<String>
60+
"boolean[]": List<Boolean>
61+
"int[]": List<Long>
62+
"double[]": List<Double>
63+
kotlin_key_factory:
64+
string: stringKey
65+
boolean: booleanKey
66+
int: longKey
67+
double: doubleKey
68+
"string[]": stringArrayKey
69+
"boolean[]": booleanArrayKey
70+
"int[]": longArrayKey
71+
"double[]": doubleArrayKey
5372
kotlin_event_type:
5473
int: Long
5574
double: Double

0 commit comments

Comments
 (0)