Skip to content

Commit

Permalink
Update smoke tests to verify exception from the span has been suppres…
Browse files Browse the repository at this point in the history
…sed when already logged
  • Loading branch information
heyams committed Feb 17, 2024
1 parent 9b18674 commit b41e03b
Show file tree
Hide file tree
Showing 17 changed files with 1,593 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

import static com.azure.monitor.opentelemetry.exporter.implementation.utils.AzureMonitorMsgId.EXPORTER_MAPPING_ERROR;

import com.azure.monitor.opentelemetry.exporter.implementation.SpanDataMapper;
import com.azure.monitor.opentelemetry.exporter.implementation.logging.OperationLogger;
import com.azure.monitor.opentelemetry.exporter.implementation.models.TelemetryItem;
import com.azure.monitor.opentelemetry.exporter.implementation.quickpulse.QuickPulse;
import com.azure.monitor.opentelemetry.exporter.implementation.utils.Strings;
import com.microsoft.applicationinsights.agent.internal.init.SpanDataMapper;
import com.microsoft.applicationinsights.agent.internal.telemetry.BatchItemProcessor;
import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient;
import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryObservers;
Expand Down
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();
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.azure.monitor.opentelemetry.exporter.implementation.AzureMonitorSpanExporterProvider;
import com.azure.monitor.opentelemetry.exporter.implementation.LogDataMapper;
import com.azure.monitor.opentelemetry.exporter.implementation.MetricDataMapper;
import com.azure.monitor.opentelemetry.exporter.implementation.SpanDataMapper;
import com.azure.monitor.opentelemetry.exporter.implementation.configuration.ConnectionString;
import com.azure.monitor.opentelemetry.exporter.implementation.heartbeat.HeartbeatExporter;
import com.azure.monitor.opentelemetry.exporter.implementation.models.TelemetryItem;
Expand Down
Loading

0 comments on commit b41e03b

Please sign in to comment.