Skip to content
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

feat(RD-12769): add execution tags #71

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/io/lumigo/core/SpansContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -73,7 +75,9 @@ public void clear() {
rttDuration = null;
endFunctionSpan = null;
reporter = null;
httpSpans = new LinkedList<>();
Copy link
Contributor

@nadav3396 nadav3396 Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add this line?

spans = new LinkedList<>();
ExecutionTags.clear();
}

private SpansContainer() {}
Expand Down Expand Up @@ -108,8 +112,8 @@ public void init(Map<String, String> 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()
Expand Down Expand Up @@ -164,7 +168,7 @@ public void init(Map<String, String> env, Reporter reporter, Context context, Ob
.event(
Configuration.getInstance().isLumigoVerboseMode()
? JsonUtils.getObjectAsJsonString(
EventParserFactory.parseEvent(event))
EventParserFactory.parseEvent(event))
: null)
.build();
}
Expand Down Expand Up @@ -213,12 +217,17 @@ public void end() throws IOException {
}

private void end(Span endFunctionSpan) throws IOException {
List<Map<String, String>> 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),
Expand Down Expand Up @@ -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: "
Expand Down
85 changes: 85 additions & 0 deletions src/main/java/io/lumigo/core/utils/ExecutionTags.java
Original file line number Diff line number Diff line change
@@ -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<Map<String, String>> 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we want the shouldLogErrors did you see it in the other tracers?

try {
Logger.info(String.format("Adding tag: %s - %s", key, value));
if (!validateTag(key, value, shouldLogErrors)) {
return;
}
Map<String, String> tag = new HashMap<>();
tag.put("key", normalizeTag(key));
tag.put("value", normalizeTag(value));
tags.add(tag);
} catch (Exception err) {
if (shouldLogErrors) {
Copy link
Contributor

@nadav3396 nadav3396 Jun 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not sure why you need this condition? you are logging the error either way

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<Map<String, String>> getTags() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets make getTags & clearTags package private methods, I dont want to expose them to users

return new ArrayList<>(tags);
}

public static void clear() {
tags.clear();
}
}
2 changes: 2 additions & 0 deletions src/main/java/io/lumigo/handlers/LumigoRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/io/lumigo/models/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,6 +55,7 @@ public static class Info {
private String stage;
private String messageId;
private List<String> messageIds;
private List<Map<String, String>> tags;
nadav3396 marked this conversation as resolved.
Show resolved Hide resolved
private long approxEventCreationTime;
}

Expand Down