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 1 commit
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
29 changes: 24 additions & 5 deletions src/main/java/io/lumigo/core/SpansContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
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;
import io.lumigo.models.Span;
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;
Expand Down Expand Up @@ -92,8 +95,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 @@ -148,7 +151,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 @@ -198,12 +201,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 @@ -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: "
Expand Down Expand Up @@ -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<String, String> getExecutionTags() {
return getInstance().getExecutionTags();
}
}
73 changes: 73 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,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<Map<String, String>> 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) {
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());
}
}

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();
}
}
14 changes: 14 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 Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I talked with Harel and we agreed that we dont want to add this here, lets expose those methods from the ExecutionTags class.

spansContainer.addExecutionTag(key, value);
}

public void clearExecutionTags() {
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.

I think the only method that should be exposed to the user is AddExecutionTag

spansContainer.clearExecutionTags();
}

public Map<String, String> getExecutionTags() {
return spansContainer.getExecutionTags();
}
}
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 @@ -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;
Expand Down Expand Up @@ -49,6 +51,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