Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,35 @@ object KnownTagsEmitter {
return u
}

val nameOfConst = HashMap<String, String>()
val idOfConst = HashMap<String, String>()
val serialOfConst = HashMap<String, String>()
val otelNameOfConst = HashMap<String, String>()
val nameOfConst = HashMap<TagConventions.TagIdentity, String>()
val idOfConst = HashMap<TagConventions.TagIdentity, String>()
val serialOfConst = HashMap<TagConventions.TagIdentity, String>()
val otelNameOfConst = HashMap<TagConventions.TagIdentity, String>()
for (t in reg.tags) {
val base = sanitize(t.name)
nameOfConst[t.name] = unique(withSuffix(base, "_NAME"))
idOfConst[t.name] = unique(withSuffix(base, "_ID"))
serialOfConst[t.name] = unique(withSuffix(base, "_SERIAL_NUM"))
// Shared Datadog names get no NAME constant because they identify several tags.
// Callers use a direction-specific ID (e.g. PEER_PORT_OUTBOUND_ID); nameOf emits the
// shared Datadog name as a literal.
if (t.sharedNameDirection == null) nameOfConst[t.identity] = unique(withSuffix(base, "_NAME"))
idOfConst[t.identity] = unique(withSuffix(base, "_ID"))
serialOfConst[t.identity] = unique(withSuffix(base, "_SERIAL_NUM"))
// Suffix the pre-suffix base (not nameC), same as the other three: suffixing an
// already-suffixed identifier would produce a redundant compound like NAME_OTEL_NAME.
if (t.otelName != null) otelNameOfConst[t.name] = unique(withSuffix(base, "_OTEL_NAME"))
if (t.otelName != null) otelNameOfConst[t.identity] = unique(withSuffix(base, "_OTEL_NAME"))
}
fun nameC(name: String) = nameOfConst.getValue(name)
fun idC(name: String) = idOfConst.getValue(name)
fun serialC(name: String) = serialOfConst.getValue(name)
fun otelNameC(name: String) = otelNameOfConst.getValue(name)
fun nameC(identity: TagConventions.TagIdentity) = nameOfConst.getValue(identity)

val order = reg.tags.map { it.name } // stable emit order
// The expression nameOf returns: the NAME constant, or the shared name's literal.
fun nameExpr(t: TagRegistry.Tag) = nameOfConst[t.identity] ?: "\"${escape(t.ddName)}\""
fun idC(identity: TagConventions.TagIdentity) = idOfConst.getValue(identity)
fun serialC(identity: TagConventions.TagIdentity) = serialOfConst.getValue(identity)
fun otelNameC(identity: TagConventions.TagIdentity) = otelNameOfConst.getValue(identity)

val order = reg.tags.map { it.identity } // stable emit order
// keyOf has no direction argument, so omit shared names and let them resolve to 0 (unknown).
val keyOfOrder = reg.tags.filter { it.sharedNameDirection == null }.map { it.identity }
// canonical name -> OpenTelemetry name, for the reverse (openTelemetryNameOf) switch.
val otelName = reg.tags.mapNotNull { t -> t.otelName?.let { t.name to it } }.toMap()
val otelName = reg.tags.mapNotNull { t -> t.otelName?.let { t.identity to it } }.toMap()
return buildString {
// Public API first (name + encoded id couplets), so readers see the useful parts up top; the
// serial ids and keyOf/resolver machinery follow below. Derivation is in the trailing comment.
Expand All @@ -78,14 +86,18 @@ object KnownTagsEmitter {
)

for (t in reg.tags) {
appendLine(
"""
public static final String ${nameC(t.name)} = "${escape(t.name)}";
public static final long ${idC(t.name)} = ${hex(t.id)};
""".trimIndent()
)
val direction = t.sharedNameDirection
if (direction == null) {
appendLine(" public static final String ${nameC(t.identity)} = \"${escape(t.ddName)}\";")
} else {
appendLine(
" /** {@code ${escape(t.ddName)}} on ${direction.yamlKey} spans. This ID identifies the direction; " +
"the Datadog name is shared across directions. */"
)
}
appendLine(" public static final long ${idC(t.identity)} = ${hex(t.id)};")
if (t.otelName != null) {
appendLine(" public static final String ${otelNameC(t.name)} = \"${escape(t.otelName)}\";")
appendLine(" public static final String ${otelNameC(t.identity)} = \"${escape(t.otelName)}\";")
}
append("// makeTagId(serial=${t.serial})")
if (t.traceLevel) append(" + trace-level")
Expand All @@ -97,14 +109,14 @@ object KnownTagsEmitter {
// Serial numbers (globalSerial per tag) — package-private, consumed by the resolver switch.
appendLine(" // ---- serial numbers ----")
for (t in reg.tags) {
appendLine(" static final int ${serialC(t.name)} = ${t.serial};")
appendLine(" static final int ${serialC(t.identity)} = ${t.serial};")
}

// OpenTelemetry name -> canonical tag name. Validation ensures aliases are distinct from all
// canonical names. Sort by OTel name to keep output deterministic.
val otelByCanonical =
reg.tags
.mapNotNull { t -> t.otelName?.let { it to t.name } }
.mapNotNull { t -> t.otelName?.let { it to t.identity } }
.sortedBy { it.first }

// keyOf table (open-addressed, via StringIndex.EmbeddingSupport). Canonical names first, then
Expand All @@ -116,7 +128,7 @@ object KnownTagsEmitter {
private static final String[] KEYOF_NAMES = {
""".trimIndent()
)
order.forEach { appendLine(" ${nameC(it)},") }
keyOfOrder.forEach { appendLine(" ${nameC(it)},") }
otelByCanonical.forEach { (otel, _) ->
appendLine(" \"${escape(otel)}\",")
}
Expand All @@ -126,7 +138,7 @@ object KnownTagsEmitter {
private static final long[] KEYOF_VALUES = {
""".trimIndent()
)
order.forEach { appendLine(" ${idC(it)},") }
keyOfOrder.forEach { appendLine(" ${idC(it)},") }
otelByCanonical.forEach { (_, canonical) ->
appendLine(" ${idC(canonical)},")
}
Expand Down Expand Up @@ -163,11 +175,11 @@ object KnownTagsEmitter {
switch (KnownTagCodec.serialNum(tagId)) {
""".trimIndent()
)
for (name in order) {
for (t in reg.tags) {
appendLine(
"""
case ${serialC(name)}:
return ${nameC(name)};
case ${serialC(t.identity)}:
return ${nameExpr(t)};
""".trimIndent()
)
}
Expand All @@ -185,12 +197,12 @@ object KnownTagsEmitter {
switch (KnownTagCodec.serialNum(tagId)) {
""".trimIndent()
)
for (name in order) {
if (otelName[name] == null) continue
for (identity in order) {
if (otelName[identity] == null) continue
appendLine(
"""
case ${serialC(name)}:
return ${otelNameC(name)};
case ${serialC(identity)}:
return ${otelNameC(identity)};
""".trimIndent()
)
}
Expand Down
Loading
Loading