-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont think we want the |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
There was a problem hiding this comment.
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?