From e530735dd1c576da1c27f90c8ca87041d36bf09c Mon Sep 17 00:00:00 2001 From: youhy Date: Fri, 7 Jun 2024 09:28:51 +0200 Subject: [PATCH 1/2] feat(RD-12769): add execution tags --- .../java/io/lumigo/core/SpansContainer.java | 29 ++++++-- .../io/lumigo/core/utils/ExecutionTags.java | 73 +++++++++++++++++++ .../lumigo/handlers/LumigoRequestHandler.java | 14 ++++ src/main/java/io/lumigo/models/Span.java | 3 + 4 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 src/main/java/io/lumigo/core/utils/ExecutionTags.java diff --git a/src/main/java/io/lumigo/core/SpansContainer.java b/src/main/java/io/lumigo/core/SpansContainer.java index acabae0..cc6e0a6 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.JsonUtils; import io.lumigo.core.utils.StringUtils; import io.lumigo.models.HttpSpan; @@ -16,6 +17,8 @@ import java.io.*; import java.util.*; import java.util.concurrent.Callable; +import java.util.stream.Collectors; + import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; @@ -92,8 +95,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() @@ -148,7 +151,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(); } @@ -198,12 +201,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), @@ -421,8 +429,7 @@ public void addHttpSpan( context .response()))) .statusCode(context.httpResponse().statusCode()) - .build()) - .build()); + .build()); Logger.debug( "Trying to extract aws custom properties for service: " @@ -577,4 +584,16 @@ public Object reduceSpanSize(Object span, boolean hasError) { } return span; } + + public void addExecutionTag(String key, String value) { + getInstance().addExecutionTag(key, value); + } + + public void clearExecutionTags() { + getInstance().clearExecutionTags(); + } + + public Map getExecutionTags() { + return getInstance().getExecutionTags(); + } } 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..01d66b5 --- /dev/null +++ b/src/main/java/io/lumigo/core/utils/ExecutionTags.java @@ -0,0 +1,73 @@ +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 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()); + } + } + + 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..13c364f 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; @@ -73,4 +75,16 @@ public OUTPUT handleRequest(INPUT input, Context context) { } public abstract OUTPUT doHandleRequest(INPUT input, Context context); + + public void addExecutionTag(String key, String value) { + spansContainer.addExecutionTag(key, value); + } + + public void clearExecutionTags() { + spansContainer.clearExecutionTags(); + } + + public Map getExecutionTags() { + return spansContainer.getExecutionTags(); + } } diff --git a/src/main/java/io/lumigo/models/Span.java b/src/main/java/io/lumigo/models/Span.java index a37b84c..8a4c8c6 100644 --- a/src/main/java/io/lumigo/models/Span.java +++ b/src/main/java/io/lumigo/models/Span.java @@ -3,6 +3,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; import java.util.Locale; +import java.util.Map; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -49,6 +51,7 @@ public static class Info { private String stage; private String messageId; private List messageIds; + private List> tags; private long approxEventCreationTime; } From 5b360fb029ae0fdeb03aba243d57bcd62a11975a Mon Sep 17 00:00:00 2001 From: youhy Date: Tue, 18 Jun 2024 09:19:31 +0200 Subject: [PATCH 2/2] RD-1269: add execution tags --- README.md | 9 +++++++++ src/main/java/io/lumigo/core/SpansContainer.java | 14 +------------- .../java/io/lumigo/core/utils/ExecutionTags.java | 12 ++++++++++++ .../io/lumigo/handlers/LumigoRequestHandler.java | 12 ------------ 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index c1f6f00..6d0906f 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,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 cc6e0a6..10c7972 100644 --- a/src/main/java/io/lumigo/core/SpansContainer.java +++ b/src/main/java/io/lumigo/core/SpansContainer.java @@ -17,7 +17,6 @@ import java.io.*; import java.util.*; import java.util.concurrent.Callable; -import java.util.stream.Collectors; import org.apache.http.Header; import org.apache.http.HttpEntity; @@ -64,6 +63,7 @@ public void clear() { endFunctionSpan = null; reporter = null; httpSpans = new LinkedList<>(); + ExecutionTags.clear(); // Clear execution tags } private SpansContainer() {} @@ -584,16 +584,4 @@ public Object reduceSpanSize(Object span, boolean hasError) { } return span; } - - public void addExecutionTag(String key, String value) { - getInstance().addExecutionTag(key, value); - } - - public void clearExecutionTags() { - getInstance().clearExecutionTags(); - } - - public Map getExecutionTags() { - return getInstance().getExecutionTags(); - } } diff --git a/src/main/java/io/lumigo/core/utils/ExecutionTags.java b/src/main/java/io/lumigo/core/utils/ExecutionTags.java index 01d66b5..e155182 100644 --- a/src/main/java/io/lumigo/core/utils/ExecutionTags.java +++ b/src/main/java/io/lumigo/core/utils/ExecutionTags.java @@ -14,6 +14,17 @@ public class ExecutionTags { 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); @@ -60,6 +71,7 @@ public static void addTag(String key, String value, boolean 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())); } } diff --git a/src/main/java/io/lumigo/handlers/LumigoRequestHandler.java b/src/main/java/io/lumigo/handlers/LumigoRequestHandler.java index 13c364f..a630908 100644 --- a/src/main/java/io/lumigo/handlers/LumigoRequestHandler.java +++ b/src/main/java/io/lumigo/handlers/LumigoRequestHandler.java @@ -75,16 +75,4 @@ public OUTPUT handleRequest(INPUT input, Context context) { } public abstract OUTPUT doHandleRequest(INPUT input, Context context); - - public void addExecutionTag(String key, String value) { - spansContainer.addExecutionTag(key, value); - } - - public void clearExecutionTags() { - spansContainer.clearExecutionTags(); - } - - public Map getExecutionTags() { - return spansContainer.getExecutionTags(); - } }