diff --git a/README.md b/README.md index 48459db..b52fb9a 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,15 @@ There are 2 way to pass configuration properties Adding `LUMIGO_TRACER_TOKEN` environment variables +#### Execution Tags + +Execution tags can be added by utilizing `ExecutionTags` Module + +```java +ExecutionTags.addTag("key", "value", true); +``` + + #### Static code initiation ```java diff --git a/src/main/java/io/lumigo/core/SpansContainer.java b/src/main/java/io/lumigo/core/SpansContainer.java index 07813ef..0d9d842 100644 --- a/src/main/java/io/lumigo/core/SpansContainer.java +++ b/src/main/java/io/lumigo/core/SpansContainer.java @@ -9,6 +9,7 @@ import io.lumigo.core.parsers.v1.AwsSdkV1ParserFactory; import io.lumigo.core.parsers.v2.AwsSdkV2ParserFactory; import io.lumigo.core.utils.AwsUtils; +import io.lumigo.core.utils.ExecutionTags; import io.lumigo.core.utils.EnvUtil; import io.lumigo.core.utils.JsonUtils; import io.lumigo.core.utils.SecretScrubber; @@ -19,6 +20,7 @@ import java.io.*; import java.util.*; import java.util.concurrent.Callable; + import lombok.Getter; import org.apache.http.Header; import org.apache.http.HttpEntity; @@ -73,7 +75,9 @@ public void clear() { rttDuration = null; endFunctionSpan = null; reporter = null; + httpSpans = new LinkedList<>(); spans = new LinkedList<>(); + ExecutionTags.clear(); } private SpansContainer() {} @@ -108,8 +112,8 @@ public void init(Map env, Reporter reporter, Context context, Ob .maxFinishTime( startTime + ((context.getRemainingTimeInMillis() > 0) - ? context.getRemainingTimeInMillis() - : MAX_LAMBDA_TIME)) + ? context.getRemainingTimeInMillis() + : MAX_LAMBDA_TIME)) .transactionId(AwsUtils.extractAwsTraceTransactionId(awsTracerId)) .info( Span.Info.builder() @@ -164,7 +168,7 @@ public void init(Map env, Reporter reporter, Context context, Ob .event( Configuration.getInstance().isLumigoVerboseMode() ? JsonUtils.getObjectAsJsonString( - EventParserFactory.parseEvent(event)) + EventParserFactory.parseEvent(event)) : null) .build(); } @@ -213,12 +217,17 @@ public void end() throws IOException { } private void end(Span endFunctionSpan) throws IOException { + List> executionTags = ExecutionTags.getTags(); this.endFunctionSpan = endFunctionSpan .toBuilder() .reporter_rtt(rttDuration) .ended(System.currentTimeMillis()) .id(this.baseSpan.getId()) + .info( + endFunctionSpan.getInfo().toBuilder() + .tags(executionTags) + .build()) .build(); reporter.reportSpans( prepareToSend(getAllCollectedSpans(), endFunctionSpan.getError() != null), @@ -428,8 +437,7 @@ public void addHttpSpan( context .response()))) .statusCode(context.httpResponse().statusCode()) - .build()) - .build()); + .build()); Logger.debug( "Trying to extract aws custom properties for service: " diff --git a/src/main/java/io/lumigo/core/utils/ExecutionTags.java b/src/main/java/io/lumigo/core/utils/ExecutionTags.java new file mode 100644 index 0000000..e155182 --- /dev/null +++ b/src/main/java/io/lumigo/core/utils/ExecutionTags.java @@ -0,0 +1,85 @@ +package io.lumigo.core.utils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.HashMap; +import org.pmw.tinylog.Logger; + +public class ExecutionTags { + private static final int MAX_TAG_KEY_LEN = 100; + private static final int MAX_TAG_VALUE_LEN = 100; + private static final int MAX_TAGS = 50; + private static final String ADD_TAG_ERROR_MSG_PREFIX = "Error adding tag"; + + private static final List> tags = new ArrayList<>(); + + private ExecutionTags() {} + + private static class Holder { + private static final ExecutionTags INSTANCE = new ExecutionTags(); + } + + // Method to return the singleton instance + private static ExecutionTags getInstance() { + return Holder.INSTANCE; + } + + private static boolean validateTag(String key, String value, boolean shouldLogErrors) { + key = String.valueOf(key); + value = String.valueOf(value); + if (key.isEmpty() || key.length() > MAX_TAG_KEY_LEN) { + if (shouldLogErrors) { + Logger.error(String.format("%s: key length should be between 1 and %d: %s - %s", + ADD_TAG_ERROR_MSG_PREFIX, MAX_TAG_KEY_LEN, key, value)); + } + return false; + } + if (value.isEmpty() || value.length() > MAX_TAG_VALUE_LEN) { + if (shouldLogErrors) { + Logger.error(String.format("%s: value length should be between 1 and %d: %s - %s", + ADD_TAG_ERROR_MSG_PREFIX, MAX_TAG_VALUE_LEN, key, value)); + } + return false; + } + if (tags.size() >= MAX_TAGS) { + if (shouldLogErrors) { + Logger.error(String.format("%s: maximum number of tags is %d: %s - %s", + ADD_TAG_ERROR_MSG_PREFIX, MAX_TAGS, key, value)); + } + return false; + } + return true; + } + + private static String normalizeTag(Object val) { + return (val == null) ? null : String.valueOf(val); + } + + public static void addTag(String key, String value, boolean shouldLogErrors) { + try { + Logger.info(String.format("Adding tag: %s - %s", key, value)); + if (!validateTag(key, value, shouldLogErrors)) { + return; + } + Map tag = new HashMap<>(); + tag.put("key", normalizeTag(key)); + tag.put("value", normalizeTag(value)); + tags.add(tag); + } catch (Exception err) { + if (shouldLogErrors) { + Logger.error(ADD_TAG_ERROR_MSG_PREFIX); + } + Logger.error(err.getMessage()); + Logger.error(String.format("%s - %s", ADD_TAG_ERROR_MSG_PREFIX, err.getMessage())); + } + } + + public static List> getTags() { + return new ArrayList<>(tags); + } + + public static void clear() { + tags.clear(); + } +} diff --git a/src/main/java/io/lumigo/handlers/LumigoRequestHandler.java b/src/main/java/io/lumigo/handlers/LumigoRequestHandler.java index d32cede..a630908 100644 --- a/src/main/java/io/lumigo/handlers/LumigoRequestHandler.java +++ b/src/main/java/io/lumigo/handlers/LumigoRequestHandler.java @@ -7,6 +7,8 @@ import io.lumigo.core.instrumentation.agent.Installer; import io.lumigo.core.network.Reporter; import io.lumigo.core.utils.EnvUtil; + +import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; diff --git a/src/main/java/io/lumigo/models/Span.java b/src/main/java/io/lumigo/models/Span.java index f151184..c8cfac8 100644 --- a/src/main/java/io/lumigo/models/Span.java +++ b/src/main/java/io/lumigo/models/Span.java @@ -7,6 +7,8 @@ import io.lumigo.core.utils.StringUtils; import java.util.List; import java.util.Locale; +import java.util.Map; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -53,6 +55,7 @@ public static class Info { private String stage; private String messageId; private List messageIds; + private List> tags; private long approxEventCreationTime; }