Skip to content

Key transactions prototype #3877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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,6 +39,8 @@
import com.microsoft.applicationinsights.agent.internal.exporter.AgentSpanExporter;
import com.microsoft.applicationinsights.agent.internal.exporter.ExporterUtils;
import com.microsoft.applicationinsights.agent.internal.httpclient.LazyHttpClient;
import com.microsoft.applicationinsights.agent.internal.keytransaction.KeyTransactionConfigSupplier;
import com.microsoft.applicationinsights.agent.internal.keytransaction.KeyTransactionSpanProcessor;
import com.microsoft.applicationinsights.agent.internal.legacyheaders.AiLegacyHeaderSpanProcessor;
import com.microsoft.applicationinsights.agent.internal.processors.ExporterWithLogProcessor;
import com.microsoft.applicationinsights.agent.internal.processors.ExporterWithSpanProcessor;
Expand Down Expand Up @@ -550,6 +552,10 @@ private static SdkTracerProviderBuilder configureTracing(
tracerProvider.addSpanProcessor(new AiLegacyHeaderSpanProcessor());
}

if (KeyTransactionConfigSupplier.KEY_TRANSACTIONS_ENABLED) {
tracerProvider.addSpanProcessor(new KeyTransactionSpanProcessor());
}

return tracerProvider;
}

Expand Down Expand Up @@ -745,6 +751,9 @@ private static void drop(

@Override
public void afterAutoConfigure(OpenTelemetrySdk sdk) {

KeyTransactionSpanProcessor.initMeterProvider(sdk.getMeterProvider());

Runtime.getRuntime()
.addShutdownHook(
new Thread(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.applicationinsights.agent.internal.keytransaction;

import com.azure.json.JsonReader;
import com.azure.json.JsonToken;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import java.io.IOException;
import java.util.List;

public class KeyTransactionConfig {

private String name;
private List<Criterion> startCriteria;
private List<Criterion> endCriteria;

String getName() {
return name;
}

List<Criterion> getStartCriteria() {
return startCriteria;
}

List<Criterion> getEndCriteria() {
return endCriteria;
}

public static KeyTransactionConfig fromJson(JsonReader jsonReader) throws IOException {
return jsonReader.readObject(
(reader) -> {
KeyTransactionConfig deserializedValue = new KeyTransactionConfig();

while (reader.nextToken() != JsonToken.END_OBJECT) {
String fieldName = reader.getFieldName();
reader.nextToken();
if ("Name".equals(fieldName)) {
deserializedValue.name = reader.getString();
} else if ("StartCriteria".equals(fieldName)) {
deserializedValue.startCriteria = reader.readArray(Criterion::fromJson);
} else if ("EndCriteria".equals(fieldName)) {
deserializedValue.endCriteria = reader.readArray(Criterion::fromJson);
} else {
reader.skipChildren();
}
}

return deserializedValue;
});
}

public static boolean matches(Attributes attributes, List<Criterion> criteria) {
for (Criterion criterion : criteria) {
String value = attributes.get(criterion.field);
switch (criterion.operator) {
case EQUALS:
if (value == null || !value.equals(criterion.value)) {
return false;
}
break;
case STARTSWITH:
if (value == null || !value.startsWith(criterion.value)) {
return false;
}
break;
case CONTAINS:
if (value == null || !value.contains(criterion.value)) {
return false;
}
break;
default:
// unexpected operator
return false;
}
}
return true;
}

// TODO (not for hackathon) expand this to work with non-String attributes
// a bit tricky since Attributes#get(AttributeKey) requires a known type
// (without iterating over all attributes)
public static class Criterion {
private AttributeKey<String> field;
private String value;
private Operator operator;

// visible for testing
AttributeKey<String> getField() {
return field;
}

// visible for testing
String getValue() {
return value;
}

// visible for testing
Operator getOperator() {
return operator;
}

public static Criterion fromJson(JsonReader jsonReader) throws IOException {
return jsonReader.readObject(
(reader) -> {
Criterion deserializedValue = new Criterion();

while (reader.nextToken() != JsonToken.END_OBJECT) {
String fieldName = reader.getFieldName();
reader.nextToken();
if ("Field".equals(fieldName)) {
deserializedValue.field = AttributeKey.stringKey(reader.getString());
} else if ("Operator".equals(fieldName)) {
deserializedValue.operator = Operator.from(reader.getString());
} else if ("Value".equals(fieldName)) {
deserializedValue.value = reader.getString();
} else {
reader.skipChildren();
}
}

return deserializedValue;
});
}
}

public enum Operator {
EQUALS,
STARTSWITH,
CONTAINS;

private static Operator from(String value) {
switch (value) {
case "==":
return EQUALS;
case "startswith":
return STARTSWITH;
case "contains":
return CONTAINS;
default:
throw new IllegalStateException("Unexpected operator: " + value);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.applicationinsights.agent.internal.keytransaction;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;

import com.azure.json.JsonProviders;
import java.io.IOException;
import java.util.List;
import java.util.function.Supplier;

public class KeyTransactionConfigSupplier implements Supplier<List<KeyTransactionConfig>> {

public static final boolean KEY_TRANSACTIONS_ENABLED = true;
public static final boolean USE_HARDCODED_CONFIG = false;

// TODO remove reliance on global
private static final KeyTransactionConfigSupplier instance = new KeyTransactionConfigSupplier();

static {
if (USE_HARDCODED_CONFIG) {
instance.set(hardcodedDemo());
}
}

public static KeyTransactionConfigSupplier getInstance() {
return instance;
}

private volatile List<KeyTransactionConfig> configs = emptyList();

private KeyTransactionConfigSupplier() {}

@Override
public List<KeyTransactionConfig> get() {
return configs;
}

public void set(List<KeyTransactionConfig> configs) {
this.configs = configs;
}

private static List<KeyTransactionConfig> hardcodedDemo() {
List<KeyTransactionConfig> configs;
try {
configs =
NewResponse.fromJson(
JsonProviders.createReader(
"{\n"
+ " \"itemsReceived\": 13,\n"
+ " \"itemsAccepted\": 13,\n"
+ " \"appId\": null,\n"
+ " \"errors\": [],\n"
+ " \"sdkConfiguration\": [\n"
+ " {\n"
+ " \"Key\": \"Transaction\",\n"
+ " \"Value\": {\n"
+ " \"Name\": \"EarthOrbit\",\n"
+ " \"StartCriteria\": [\n"
+ " {\n"
+ " \"Field\": \"url.path\",\n"
+ " \"Operator\": \"==\",\n"
+ " \"Value\": \"/earth\"\n"
+ " }\n"
+ " ],\n"
+ " \"EndCriteria\": []\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"Key\": \"Transaction\",\n"
+ " \"Value\": {\n"
+ " \"Name\": \"MarsMission\",\n"
+ " \"StartCriteria\": [\n"
+ " {\n"
+ " \"Field\": \"url.path\",\n"
+ " \"Operator\": \"==\",\n"
+ " \"Value\": \"/mars\"\n"
+ " }\n"
+ " ],\n"
+ " \"EndCriteria\": [\n"
+ " {\n"
+ " \"Field\": \"messaging.operation\",\n"
+ " \"Operator\": \"==\",\n"
+ " \"Value\": \"process\"\n"
+ " },\n"
+ " {\n"
+ " \"Field\": \"messaging.destination.name\",\n"
+ " \"Operator\": \"==\",\n"
+ " \"Value\": \"space\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " }\n"
+ " ]\n"
+ "}\n"))
.getSdkConfigurations()
.stream()
.map(SdkConfiguration::getValue)
.collect(toList());
} catch (IOException e) {
throw new IllegalStateException(e);
}
return configs;
}
}
Loading
Loading