-
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.
Update smoke tests to verify exception from the span has been suppres…
…sed when already logged
- Loading branch information
Showing
17 changed files
with
1,593 additions
and
48 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
85 changes: 85 additions & 0 deletions
85
...tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/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,85 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.applicationinsights.agent.internal.init; | ||
|
||
import com.azure.core.util.logging.ClientLogger; | ||
import com.azure.monitor.opentelemetry.exporter.implementation.builders.AbstractTelemetryBuilder; | ||
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 | ||
public 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(); | ||
} | ||
} |
144 changes: 144 additions & 0 deletions
144
.../src/main/java/com/microsoft/applicationinsights/agent/internal/init/MappingsBuilder.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,144 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.applicationinsights.agent.internal.init; | ||
|
||
import static java.util.Arrays.asList; | ||
|
||
import com.azure.monitor.opentelemetry.exporter.implementation.builders.AbstractTelemetryBuilder; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
class MappingsBuilder { | ||
|
||
enum MappingType { | ||
LOG, | ||
SPAN, | ||
METRIC | ||
} | ||
|
||
public static final Mappings EMPTY_MAPPINGS = | ||
new Mappings( | ||
Collections.emptyMap(), Trie.<MappingsBuilder.PrefixMapping>newBuilder().build()); | ||
|
||
// TODO need to keep this list in sync as new semantic conventions are defined | ||
private static final Set<String> IGNORED_LOG_AND_SPAN_STANDARD_ATTRIBUTE_PREFIXES = | ||
new HashSet<>( | ||
asList( | ||
"server.", | ||
"client.", | ||
"network.", | ||
"url.", | ||
"error.", | ||
"http.", | ||
"db.", | ||
"message.", | ||
"messaging.", | ||
"rpc.", | ||
"enduser.", | ||
"net.", | ||
"peer.", | ||
"exception.", | ||
"thread.", | ||
"faas.", | ||
"code.", | ||
"job.", // proposed semantic convention which we use for job, | ||
"applicationinsights.internal.")); | ||
|
||
private static final Set<String> IGNORED_METRIC_INTERNAL_ATTRIBUTE_PREFIXES = | ||
Collections.singleton("applicationinsights.internal."); | ||
|
||
private final Map<String, ExactMapping> exactMappings = new HashMap<>(); | ||
private final Trie.Builder<PrefixMapping> prefixMappings = Trie.newBuilder(); | ||
|
||
MappingsBuilder(MappingType mappingType) { | ||
switch (mappingType) { | ||
case LOG: | ||
case SPAN: | ||
// ignore all standard attribute prefixes for Logs and Spans | ||
for (String prefix : IGNORED_LOG_AND_SPAN_STANDARD_ATTRIBUTE_PREFIXES) { | ||
prefixMappings.put(prefix, (telemetryBuilder, key, value) -> {}); | ||
} | ||
break; | ||
case METRIC: | ||
// ignore all internal attribute prefixes for Metrics | ||
for (String prefix : IGNORED_METRIC_INTERNAL_ATTRIBUTE_PREFIXES) { | ||
prefixMappings.put(prefix, (telemetryBuilder, key, value) -> {}); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
MappingsBuilder ignoreExact(String key) { | ||
exactMappings.put(key, (telemetryBuilder, value) -> {}); | ||
return this; | ||
} | ||
|
||
MappingsBuilder ignorePrefix(String prefix) { | ||
prefixMappings.put(prefix, (telemetryBuilder, key, value) -> {}); | ||
return this; | ||
} | ||
|
||
MappingsBuilder exact(String key, ExactMapping mapping) { | ||
exactMappings.put(key, mapping); | ||
return this; | ||
} | ||
|
||
MappingsBuilder prefix(String prefix, MappingsBuilder.PrefixMapping mapping) { | ||
prefixMappings.put(prefix, mapping); | ||
return this; | ||
} | ||
|
||
MappingsBuilder exactString(AttributeKey<String> attributeKey, String propertyName) { | ||
exactMappings.put( | ||
attributeKey.getKey(), | ||
(telemetryBuilder, value) -> { | ||
if (value instanceof String) { | ||
telemetryBuilder.addProperty(propertyName, (String) value); | ||
} | ||
}); | ||
return this; | ||
} | ||
|
||
MappingsBuilder exactLong(AttributeKey<Long> attributeKey, String propertyName) { | ||
exactMappings.put( | ||
attributeKey.getKey(), | ||
(telemetryBuilder, value) -> { | ||
if (value instanceof Long) { | ||
telemetryBuilder.addProperty(propertyName, Long.toString((Long) value)); | ||
} | ||
}); | ||
return this; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
MappingsBuilder exactStringArray(AttributeKey<List<String>> attributeKey, String propertyName) { | ||
exactMappings.put( | ||
attributeKey.getKey(), | ||
(telemetryBuilder, value) -> { | ||
if (value instanceof List) { | ||
telemetryBuilder.addProperty(propertyName, String.join(",", (List) value)); | ||
} | ||
}); | ||
return this; | ||
} | ||
|
||
public Mappings build() { | ||
return new Mappings(exactMappings, prefixMappings.build()); | ||
} | ||
|
||
@FunctionalInterface | ||
interface ExactMapping { | ||
void map(AbstractTelemetryBuilder telemetryBuilder, Object value); | ||
} | ||
|
||
@FunctionalInterface | ||
interface PrefixMapping { | ||
void map(AbstractTelemetryBuilder telemetryBuilder, String key, Object value); | ||
} | ||
} |
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
Oops, something went wrong.