-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
386 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...orter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/Mappings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.monitor.opentelemetry.exporter.implementation; | ||
|
||
import com.azure.core.util.logging.ClientLogger; | ||
import com.azure.monitor.opentelemetry.exporter.implementation.builders.AbstractTelemetryBuilder; | ||
import com.azure.monitor.opentelemetry.exporter.implementation.utils.Trie; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.AttributeType; | ||
import io.opentelemetry.api.common.Attributes; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import reactor.util.annotation.Nullable; | ||
|
||
class Mappings { | ||
|
||
private static final ClientLogger logger = new ClientLogger(Mappings.class); | ||
private static final Set<AttributeType> unexpectedTypesLogged = ConcurrentHashMap.newKeySet(); | ||
|
||
private final Map<String, MappingsBuilder.ExactMapping> exactMappings; | ||
private final Trie<MappingsBuilder.PrefixMapping> prefixMappings; | ||
|
||
Mappings( | ||
Map<String, MappingsBuilder.ExactMapping> exactMappings, | ||
Trie<MappingsBuilder.PrefixMapping> prefixMappings) { | ||
this.exactMappings = exactMappings; | ||
this.prefixMappings = prefixMappings; | ||
} | ||
|
||
void map(Attributes attributes, AbstractTelemetryBuilder telemetryBuilder) { | ||
attributes.forEach( | ||
(attributeKey, value) -> { | ||
map(telemetryBuilder, attributeKey, value); | ||
}); | ||
} | ||
|
||
private void map( | ||
AbstractTelemetryBuilder telemetryBuilder, AttributeKey attributeKey, Object value) { | ||
String key = attributeKey.getKey(); | ||
MappingsBuilder.ExactMapping exactMapping = exactMappings.get(key); | ||
if (exactMapping != null) { | ||
exactMapping.map(telemetryBuilder, value); | ||
return; | ||
} | ||
MappingsBuilder.PrefixMapping prefixMapping = prefixMappings.getOrNull(key); | ||
if (prefixMapping != null) { | ||
prefixMapping.map(telemetryBuilder, key, value); | ||
return; | ||
} | ||
String val = convertToString(value, attributeKey.getType()); | ||
if (val != null) { | ||
telemetryBuilder.addProperty(attributeKey.getKey(), val); | ||
} | ||
} | ||
|
||
@Nullable | ||
private static String convertToString(Object value, AttributeType type) { | ||
switch (type) { | ||
case STRING: | ||
case BOOLEAN: | ||
case LONG: | ||
case DOUBLE: | ||
return String.valueOf(value); | ||
case STRING_ARRAY: | ||
case BOOLEAN_ARRAY: | ||
case LONG_ARRAY: | ||
case DOUBLE_ARRAY: | ||
return join((List<?>) value); | ||
} | ||
if (unexpectedTypesLogged.add(type)) { | ||
logger.warning("unexpected attribute type: {}", type); | ||
} | ||
return null; | ||
} | ||
|
||
static <T> String join(List<T> values) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (Object val : values) { | ||
if (sb.length() > 0) { | ||
sb.append(", "); | ||
} | ||
sb.append(val); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
Oops, something went wrong.