From ee35f40f98f755425e4a35135ccd33752049d209 Mon Sep 17 00:00:00 2001 From: Vadim Korolik Date: Thu, 2 Nov 2023 12:07:53 -0700 Subject: [PATCH] ensure the highlight SDK is compatible with java 11 (#7051) ## Summary Ensure the Java SDK is comparible with Java 11 per customer request. ## How did you test this change? CI testing ## Are there any deployment considerations? Will be tagging and releasing a new SDK version. ## Does this work require review from our design team? No --- .../highlight/sdk/common/HighlightHeader.java | 55 +- .../sdk/common/HighlightOptions.java | 364 ++++++---- .../io/highlight/sdk/common/Severity.java | 677 ++++++++++-------- .../sdk/common/record/HighlightRecord.java | 2 +- .../log/java/HighlightLoggerHandler.java | 2 +- .../main/java/io/highlight/sdk/Highlight.java | 6 +- .../io/highlight/sdk/HighlightSession.java | 151 ++-- sdk/highlight-java/pom.xml | 6 +- 8 files changed, 729 insertions(+), 534 deletions(-) diff --git a/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/HighlightHeader.java b/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/HighlightHeader.java index c4d1f3790bb..c95405f1f8a 100644 --- a/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/HighlightHeader.java +++ b/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/HighlightHeader.java @@ -1,14 +1,51 @@ package io.highlight.sdk.common; -public record HighlightHeader(String sessionId, String requestId) { +public final class HighlightHeader { - public static final String X_HIGHLIGHT_REQUEST = "x-highlight-request"; + public static final String X_HIGHLIGHT_REQUEST = "x-highlight-request"; + private final String sessionId; + private final String requestId; + + public HighlightHeader(String sessionId, String requestId) { + this.sessionId = sessionId; + this.requestId = requestId; + } + + public static HighlightHeader parse(String header) { + String[] split = header.split("/"); + if (split.length == 2) { + return new HighlightHeader(split[0], split[1]); + } + return null; + } + + public String sessionId() { + return sessionId; + } + + public String requestId() { + return requestId; + } + + @java.lang.Override + public boolean equals(java.lang.Object obj) { + if (obj == this) return true; + if (obj == null || obj.getClass() != this.getClass()) return false; + var that = (HighlightHeader) obj; + return java.util.Objects.equals(this.sessionId, that.sessionId) && + java.util.Objects.equals(this.requestId, that.requestId); + } + + @java.lang.Override + public int hashCode() { + return java.util.Objects.hash(sessionId, requestId); + } + + @java.lang.Override + public String toString() { + return "HighlightHeader[" + + "sessionId=" + sessionId + ", " + + "requestId=" + requestId + ']'; + } - public static HighlightHeader parse(String header) { - String[] split = header.split("/"); - if (split.length == 2) { - return new HighlightHeader(split[0], split[1]); - } - return null; - } } \ No newline at end of file diff --git a/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/HighlightOptions.java b/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/HighlightOptions.java index 80042b56323..025acff25f0 100644 --- a/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/HighlightOptions.java +++ b/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/HighlightOptions.java @@ -9,146 +9,226 @@ * Represents the options for highlighting in a project, including project ID, * backend URL, environment, version, and default attributes. */ -public record HighlightOptions(String projectId, String backendUrl, String enviroment, String version, String serviceName, boolean metric, - Attributes defaultAttributes) { - - /** - * Returns a new builder for constructing {@link HighlightOptions} with the - * specified project ID. - * - * @param projectId the project ID to use - * @return a new builder for constructing {@link HighlightOptions} - */ - public static Builder builder(String projectId) { - return new Builder(projectId); - } - - /** - * A builder class for constructing {@link HighlightOptions}. - */ - public static class Builder { - - private static final String DEFAULT_ENVIROMENT = "development"; - private static final String DEFAULT_VERSION = "unknown"; - private static final String DEFAULT_SERVICE_NAME = "unknown"; - - private final String projectId; - - private String backendUrl = null; - - private String environment = null; - private String version = null; - private String serviceName = null; - - private boolean metric = true; - - private AttributesBuilder defaultAttributes = Attributes.builder(); - - /** - * Creates a new builder for constructing {@link HighlightOptions} with the - * specified project ID. - * - * @param projectId the project ID to use - */ - public Builder(String projectId) { - this.projectId = projectId; - } - - /** - * Sets the backend URL for the highlight options being constructed.
- * If not set, the default value is https://otel.highlight.io:4318 - * - * @param backendUrl the backend URL to set - * @return this builder - */ - public Builder backendUrl(String backendUrl) { - this.backendUrl = backendUrl; - return this; - } - - /** - * Sets the environment for the highlight options being constructed.
- * If not set, the default value is development. - * - * @param environment the environment to set - * @return this builder - */ - public Builder environment(String environment) { - this.environment = environment; - return this; - } - - /** - * Sets the version for the highlight options being constructed.
- * If not set, the default value is "unknown". - * - * @param version the version to set - * @return this builder - */ - public Builder version(String version) { - this.version = version; - return this; - } - - - /** - * Sets the service name for the highlight options being constructed.
- * If not set, the default value is "unknown". - * - * @param serviceName the service name to set - * @return this builder - */ - public Builder serviceName(String serviceName) { - this.serviceName = serviceName; - return this; - } - - /** - * Sets whether metric is enabled or not for the highlight options being - * constructed.
- * If not set, the default value is true. - * - * @param enabled whether to enable metric or not - * @return this builder - */ - public Builder metric(boolean enabled) { - this.metric = enabled; - return this; - } - - /** - * Sets the default attributes for the highlight options being constructed using - * the specified consumer function. - * - * @param attributes a consumer function that accepts an - * {@link AttributesBuilder} - * @return this builder - */ - public Builder attributes(Consumer attributes) { - attributes.accept(this.defaultAttributes); - return this; - } - - /** - * Builds the {@link HighlightOptions} using the specified project ID, backend - * URL, environment, version, metric, and default attributes. - * - * @return the constructed {@link HighlightOptions} - */ - public HighlightOptions build() { - if (this.environment == null) { - this.environment = DEFAULT_ENVIROMENT; - } - if (this.version == null) { - this.version = DEFAULT_VERSION; - } - - if (this.serviceName == null) { - this.serviceName = DEFAULT_SERVICE_NAME; - } - - return new HighlightOptions(this.projectId, this.backendUrl, this.environment, this.version, this.serviceName, this.metric, - this.defaultAttributes.build()); - } - } +public final class HighlightOptions { + private final String projectId; + private final String backendUrl; + private final String enviroment; + private final String version; + private final String serviceName; + private final boolean metric; + private final Attributes defaultAttributes; + + /** + * + */ + public HighlightOptions(String projectId, String backendUrl, String enviroment, String version, String serviceName, boolean metric, + Attributes defaultAttributes) { + this.projectId = projectId; + this.backendUrl = backendUrl; + this.enviroment = enviroment; + this.version = version; + this.serviceName = serviceName; + this.metric = metric; + this.defaultAttributes = defaultAttributes; + } + + /** + * Returns a new builder for constructing {@link HighlightOptions} with the + * specified project ID. + * + * @param projectId the project ID to use + * @return a new builder for constructing {@link HighlightOptions} + */ + public static Builder builder(String projectId) { + return new Builder(projectId); + } + + public String projectId() { + return projectId; + } + + public String backendUrl() { + return backendUrl; + } + + public String enviroment() { + return enviroment; + } + + public String version() { + return version; + } + + public String serviceName() { + return serviceName; + } + + public boolean metric() { + return metric; + } + + public Attributes defaultAttributes() { + return defaultAttributes; + } + + @java.lang.Override + public boolean equals(java.lang.Object obj) { + if (obj == this) return true; + if (obj == null || obj.getClass() != this.getClass()) return false; + var that = (HighlightOptions) obj; + return java.util.Objects.equals(this.projectId, that.projectId) && + java.util.Objects.equals(this.backendUrl, that.backendUrl) && + java.util.Objects.equals(this.enviroment, that.enviroment) && + java.util.Objects.equals(this.version, that.version) && + java.util.Objects.equals(this.serviceName, that.serviceName) && + this.metric == that.metric && + java.util.Objects.equals(this.defaultAttributes, that.defaultAttributes); + } + + @java.lang.Override + public int hashCode() { + return java.util.Objects.hash(projectId, backendUrl, enviroment, version, serviceName, metric, defaultAttributes); + } + + @java.lang.Override + public String toString() { + return "HighlightOptions[" + + "projectId=" + projectId + ", " + + "backendUrl=" + backendUrl + ", " + + "enviroment=" + enviroment + ", " + + "version=" + version + ", " + + "serviceName=" + serviceName + ", " + + "metric=" + metric + ", " + + "defaultAttributes=" + defaultAttributes + ']'; + } + + + /** + * A builder class for constructing {@link HighlightOptions}. + */ + public static class Builder { + + private static final String DEFAULT_ENVIROMENT = "development"; + private static final String DEFAULT_VERSION = "unknown"; + private static final String DEFAULT_SERVICE_NAME = "unknown"; + + private final String projectId; + + private String backendUrl = null; + + private String environment = null; + private String version = null; + private String serviceName = null; + + private boolean metric = true; + + private AttributesBuilder defaultAttributes = Attributes.builder(); + + /** + * Creates a new builder for constructing {@link HighlightOptions} with the + * specified project ID. + * + * @param projectId the project ID to use + */ + public Builder(String projectId) { + this.projectId = projectId; + } + + /** + * Sets the backend URL for the highlight options being constructed.
+ * If not set, the default value is https://otel.highlight.io:4318 + * + * @param backendUrl the backend URL to set + * @return this builder + */ + public Builder backendUrl(String backendUrl) { + this.backendUrl = backendUrl; + return this; + } + + /** + * Sets the environment for the highlight options being constructed.
+ * If not set, the default value is development. + * + * @param environment the environment to set + * @return this builder + */ + public Builder environment(String environment) { + this.environment = environment; + return this; + } + + /** + * Sets the version for the highlight options being constructed.
+ * If not set, the default value is "unknown". + * + * @param version the version to set + * @return this builder + */ + public Builder version(String version) { + this.version = version; + return this; + } + + + /** + * Sets the service name for the highlight options being constructed.
+ * If not set, the default value is "unknown". + * + * @param serviceName the service name to set + * @return this builder + */ + public Builder serviceName(String serviceName) { + this.serviceName = serviceName; + return this; + } + + /** + * Sets whether metric is enabled or not for the highlight options being + * constructed.
+ * If not set, the default value is true. + * + * @param enabled whether to enable metric or not + * @return this builder + */ + public Builder metric(boolean enabled) { + this.metric = enabled; + return this; + } + + /** + * Sets the default attributes for the highlight options being constructed using + * the specified consumer function. + * + * @param attributes a consumer function that accepts an + * {@link AttributesBuilder} + * @return this builder + */ + public Builder attributes(Consumer attributes) { + attributes.accept(this.defaultAttributes); + return this; + } + + /** + * Builds the {@link HighlightOptions} using the specified project ID, backend + * URL, environment, version, metric, and default attributes. + * + * @return the constructed {@link HighlightOptions} + */ + public HighlightOptions build() { + if (this.environment == null) { + this.environment = DEFAULT_ENVIROMENT; + } + if (this.version == null) { + this.version = DEFAULT_VERSION; + } + + if (this.serviceName == null) { + this.serviceName = DEFAULT_SERVICE_NAME; + } + + return new HighlightOptions(this.projectId, this.backendUrl, this.environment, this.version, this.serviceName, this.metric, + this.defaultAttributes.build()); + } + } } diff --git a/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/Severity.java b/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/Severity.java index 875cf3e6d34..88f3d3ebb61 100644 --- a/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/Severity.java +++ b/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/Severity.java @@ -1,332 +1,373 @@ package io.highlight.sdk.common; /** - * *

* Represents the severity of a log message, along with its text and priority. *
* The severity can be one of TRACE, DEBUG, INFO, WARN, ERROR, or FATAL, and * each severity level can have an associated identifier and priority level. *

- * + *

* See: severity-fields */ -public record Severity(String text, int id, Priority priority) { - - // ID values for the different severity levels. - private static final int TRACE_ID = 1; - private static final int DEBUG_ID = 5; - private static final int INFO_ID = 9; - private static final int WARN_ID = 13; - private static final int ERROR_ID = 17; - private static final int FATAL_ID = 21; - - // Predefined instances of Severity for each severity level. - public static final Severity TRACE = Severity.trace(Priority.LOW); - public static final Severity DEBUG = Severity.debug(Priority.LOW); - public static final Severity INFO = Severity.info(Priority.LOW); - public static final Severity WARN = Severity.warn(Priority.LOW); - public static final Severity ERROR = Severity.error(Priority.LOW); - public static final Severity FATAL = Severity.fatal(Priority.LOW); - - /** - * Creates a new log message with severity level TRACE and the given text and a - * default low priority level. - * - * @param text the text of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity trace(String text) { - return Severity.trace(text, Priority.LOW); - } - - /** - * Creates a new log message with severity level TRACE and the given priority. - * - * @param priority the priority level of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity trace(Priority priority) { - return Severity.trace(null, priority); - } - - /** - * Creates a new log message with severity level TRACE and the given text and - * priority. - * - * @param text the text of the log message, or null if not provided - * @param priority the priority level of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity trace(String text, Priority priority) { - return new Severity(text, TRACE_ID, priority); - } - - /** - * Creates a new log message with severity level DEBUG and the given text and a - * default low priority level. - * - * @param text the text of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity debug(String text) { - return Severity.debug(text, Priority.LOW); - } - - /** - * Creates a new log message with severity level DEBUG and the given priority. - * - * @param priority the priority level of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity debug(Priority priority) { - return Severity.debug(null, priority); - } - - /** - * Creates a new log message with severity level DEBUG and the given text and - * priority. - * - * @param text the text of the log message, or null if not provided - * @param priority the priority level of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity debug(String text, Priority priority) { - return new Severity(text, DEBUG_ID, priority); - } - - /** - * Creates a new log message with severity level INFO and the given text and a - * default low priority level. - * - * @param text the text of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity info(String text) { - return Severity.info(text, Priority.LOW); - } - - /** - * Creates a new log message with severity level INFO and the given priority. - * - * @param priority the priority level of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity info(Priority priority) { - return Severity.info(null, priority); - } - - /** - * Creates a new log message with severity level INFO and the given text and - * priority. - * - * @param text the text of the log message, or null if not provided - * @param priority the priority level of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity info(String text, Priority priority) { - return new Severity(text, INFO_ID, priority); - } - - /** - * Creates a new log message with severity level WARN and the given text and a - * default low priority level. - * - * @param text the text of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity warn(String text) { - return Severity.warn(text, Priority.LOW); - } - - /** - * Creates a new log message with severity level WARN and the given priority. - * - * @param priority the priority level of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity warn(Priority priority) { - return Severity.warn(null, priority); - } - - /** - * Creates a new log message with severity level WARN and the given text and - * priority. - * - * @param text the text of the log message, or null if not provided - * @param priority the priority level of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity warn(String text, Priority priority) { - return new Severity(text, WARN_ID, priority); - } - - /** - * Creates a new log message with severity level ERROR and the given text and a - * default low priority level. - * - * @param text the text of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity error(String text) { - return Severity.error(text, Priority.LOW); - } - - /** - * Creates a new log message with severity level ERROR and the given priority. - * - * @param priority the priority level of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity error(Priority priority) { - return Severity.error(null, priority); - } - - /** - * Creates a new log message with severity level ERROR and the given text and - * priority. - * - * @param text the text of the log message, or null if not provided - * @param priority the priority level of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity error(String text, Priority priority) { - return new Severity(text, ERROR_ID, priority); - } - - /** - * Creates a new log message with severity level FATAL and the given text and a - * default low priority level. - * - * @param text the text of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity fatal(String text) { - return Severity.fatal(text, Priority.LOW); - } - - /** - * Creates a new log message with severity level FATAL and the given priority. - * - * @param priority the priority level of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity fatal(Priority priority) { - return Severity.fatal(null, priority); - } - - /** - * Creates a new log message with severity level FATAL and the given text and - * priority. - * - * @param text the text of the log message, or null if not provided - * @param priority the priority level of the log message - * @return a new instance of the Severity class with the given parameters - */ - public static Severity fatal(String text, Priority priority) { - return new Severity(text, FATAL_ID, priority); - } - - /** - * Return the severity level combined with the priority - * - * @return severity level with priority - */ - public int id() { - return this.id + priority.difference; - } - - /** - * Return a readable severity level name without the priority level - * - * @return severity level name - */ - public String name() { - return switch (this.id) { - case TRACE_ID -> "TRACE"; - case DEBUG_ID -> "DEBUG"; - case INFO_ID -> "INFO"; - case WARN_ID -> "WARN"; - case ERROR_ID -> "ERROR"; - case FATAL_ID -> "FATAL"; - default -> throw new IllegalArgumentException("Unexpected value: " + this.id); - }; - } - - /** - * Return a readable severity level name with the priority level - * - * See: displaying-severity - * - * @return severity level name - */ - public String shortName() { - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(switch (this.id) { - case TRACE_ID -> "TRACE"; - case DEBUG_ID -> "DEBUG"; - case INFO_ID -> "INFO"; - case WARN_ID -> "WARN"; - case ERROR_ID -> "ERROR"; - case FATAL_ID -> "FATAL"; - default -> throw new IllegalArgumentException("Unexpected value: " + this.id); - }); - - if (this.priority != Priority.LOW) { - stringBuilder.append(this.priority.difference); - } - - return stringBuilder.toString(); - } - - /** - * Transform the currently severity into the opentelemetry {@code io.opentelemetry.api.logs.Severity} class - * - * @return severity transformed into {@code io.opentelemetry.api.logs.Severity} - */ - public io.opentelemetry.api.logs.Severity toOpenTelemetry() { - return io.opentelemetry.api.logs.Severity.values()[this.id]; - } - - /** - * Represents the priority of a log message. The priority can be one of LOW, - * NORMAL, MEDIUM, or HIGH, and each priority level have an associated numeric - * difference value that is added to the severity ID when calculating the - * overall priority of a log message. - */ - public static enum Priority { - /** - * LOW priority is the default one and represent a value from zero - */ - LOW(0), - - /** - * NORMAL priority represent a value from one - */ - NORMAL(1), - - /** - * MEDIUM priority represent a value from two - */ - MEDIUM(2), - - /** - * HIGH priority represent a value from three - */ - HIGH(3); - - final int difference; - - /** - * Constructs a new instance of Priority with the specified numeric difference - * value. - * - * @param difference the numeric difference value for this priority level - */ - private Priority(int difference) { - this.difference = difference; +public final class Severity { + + // ID values for the different severity levels. + private static final int TRACE_ID = 1; + private static final int DEBUG_ID = 5; + private static final int INFO_ID = 9; + private static final int WARN_ID = 13; + private static final int ERROR_ID = 17; + private static final int FATAL_ID = 21; + + // Predefined instances of Severity for each severity level. + public static final Severity TRACE = Severity.trace(Priority.LOW); + public static final Severity DEBUG = Severity.debug(Priority.LOW); + public static final Severity INFO = Severity.info(Priority.LOW); + public static final Severity WARN = Severity.warn(Priority.LOW); + public static final Severity ERROR = Severity.error(Priority.LOW); + public static final Severity FATAL = Severity.fatal(Priority.LOW); + private final String text; + private final int id; + private final Priority priority; + + /** + * + */ + public Severity(String text, int id, Priority priority) { + this.text = text; + this.id = id; + this.priority = priority; + } + + /** + * Creates a new log message with severity level TRACE and the given text and a + * default low priority level. + * + * @param text the text of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity trace(String text) { + return Severity.trace(text, Priority.LOW); + } + + /** + * Creates a new log message with severity level TRACE and the given priority. + * + * @param priority the priority level of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity trace(Priority priority) { + return Severity.trace(null, priority); + } + + /** + * Creates a new log message with severity level TRACE and the given text and + * priority. + * + * @param text the text of the log message, or null if not provided + * @param priority the priority level of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity trace(String text, Priority priority) { + return new Severity(text, TRACE_ID, priority); + } + + /** + * Creates a new log message with severity level DEBUG and the given text and a + * default low priority level. + * + * @param text the text of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity debug(String text) { + return Severity.debug(text, Priority.LOW); + } + + /** + * Creates a new log message with severity level DEBUG and the given priority. + * + * @param priority the priority level of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity debug(Priority priority) { + return Severity.debug(null, priority); + } + + /** + * Creates a new log message with severity level DEBUG and the given text and + * priority. + * + * @param text the text of the log message, or null if not provided + * @param priority the priority level of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity debug(String text, Priority priority) { + return new Severity(text, DEBUG_ID, priority); + } + + /** + * Creates a new log message with severity level INFO and the given text and a + * default low priority level. + * + * @param text the text of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity info(String text) { + return Severity.info(text, Priority.LOW); + } + + /** + * Creates a new log message with severity level INFO and the given priority. + * + * @param priority the priority level of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity info(Priority priority) { + return Severity.info(null, priority); + } + + /** + * Creates a new log message with severity level INFO and the given text and + * priority. + * + * @param text the text of the log message, or null if not provided + * @param priority the priority level of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity info(String text, Priority priority) { + return new Severity(text, INFO_ID, priority); + } + + /** + * Creates a new log message with severity level WARN and the given text and a + * default low priority level. + * + * @param text the text of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity warn(String text) { + return Severity.warn(text, Priority.LOW); + } + + /** + * Creates a new log message with severity level WARN and the given priority. + * + * @param priority the priority level of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity warn(Priority priority) { + return Severity.warn(null, priority); + } + + /** + * Creates a new log message with severity level WARN and the given text and + * priority. + * + * @param text the text of the log message, or null if not provided + * @param priority the priority level of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity warn(String text, Priority priority) { + return new Severity(text, WARN_ID, priority); + } + + /** + * Creates a new log message with severity level ERROR and the given text and a + * default low priority level. + * + * @param text the text of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity error(String text) { + return Severity.error(text, Priority.LOW); + } + + /** + * Creates a new log message with severity level ERROR and the given priority. + * + * @param priority the priority level of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity error(Priority priority) { + return Severity.error(null, priority); + } + + /** + * Creates a new log message with severity level ERROR and the given text and + * priority. + * + * @param text the text of the log message, or null if not provided + * @param priority the priority level of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity error(String text, Priority priority) { + return new Severity(text, ERROR_ID, priority); + } + + /** + * Creates a new log message with severity level FATAL and the given text and a + * default low priority level. + * + * @param text the text of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity fatal(String text) { + return Severity.fatal(text, Priority.LOW); + } + + /** + * Creates a new log message with severity level FATAL and the given priority. + * + * @param priority the priority level of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity fatal(Priority priority) { + return Severity.fatal(null, priority); + } + + /** + * Creates a new log message with severity level FATAL and the given text and + * priority. + * + * @param text the text of the log message, or null if not provided + * @param priority the priority level of the log message + * @return a new instance of the Severity class with the given parameters + */ + public static Severity fatal(String text, Priority priority) { + return new Severity(text, FATAL_ID, priority); + } + + /** + * Return the severity level combined with the priority + * + * @return severity level with priority + */ + public int id() { + return this.id + priority.difference; + } + + /** + * Return a readable severity level name without the priority level + * + * @return severity level name + */ + public String name() { + if (this.id == TRACE_ID) { + return "TRACE"; + } else if (this.id == DEBUG_ID) { + return "DEBUG"; + } else if (this.id == INFO_ID) { + return "INFO"; + } else if (this.id == WARN_ID) { + return "WARN"; + } else if (this.id == ERROR_ID) { + return "ERROR"; + } else if (this.id == FATAL_ID) { + return "FATAL"; + } else { + throw new IllegalArgumentException("Unexpected value: " + this.id); } - } + } + + /** + * Return a readable severity level name with the priority level + *

+ * See: displaying-severity + * + * @return severity level name + */ + public String shortName() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(this.name()); + + if (this.priority != Priority.LOW) { + stringBuilder.append(this.priority.difference); + } + + return stringBuilder.toString(); + } + + /** + * Transform the currently severity into the opentelemetry {@code io.opentelemetry.api.logs.Severity} class + * + * @return severity transformed into {@code io.opentelemetry.api.logs.Severity} + */ + public io.opentelemetry.api.logs.Severity toOpenTelemetry() { + return io.opentelemetry.api.logs.Severity.values()[this.id]; + } + + public String text() { + return text; + } + + public Priority priority() { + return priority; + } + + @java.lang.Override + public boolean equals(java.lang.Object obj) { + if (obj == this) return true; + if (obj == null || obj.getClass() != this.getClass()) return false; + var that = (Severity) obj; + return java.util.Objects.equals(this.text, that.text) && + this.id == that.id && + java.util.Objects.equals(this.priority, that.priority); + } + + @java.lang.Override + public int hashCode() { + return java.util.Objects.hash(text, id, priority); + } + + @java.lang.Override + public String toString() { + return "Severity[" + + "text=" + text + ", " + + "id=" + id + ", " + + "priority=" + priority + ']'; + } + + + /** + * Represents the priority of a log message. The priority can be one of LOW, + * NORMAL, MEDIUM, or HIGH, and each priority level have an associated numeric + * difference value that is added to the severity ID when calculating the + * overall priority of a log message. + */ + public static enum Priority { + /** + * LOW priority is the default one and represent a value from zero + */ + LOW(0), + + /** + * NORMAL priority represent a value from one + */ + NORMAL(1), + + /** + * MEDIUM priority represent a value from two + */ + MEDIUM(2), + + /** + * HIGH priority represent a value from three + */ + HIGH(3); + + final int difference; + + /** + * Constructs a new instance of Priority with the specified numeric difference + * value. + * + * @param difference the numeric difference value for this priority level + */ + private Priority(int difference) { + this.difference = difference; + } + } } \ No newline at end of file diff --git a/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/record/HighlightRecord.java b/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/record/HighlightRecord.java index a3aace5a162..aff744b3118 100644 --- a/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/record/HighlightRecord.java +++ b/sdk/highlight-java/highlight-common/src/main/java/io/highlight/sdk/common/record/HighlightRecord.java @@ -26,7 +26,7 @@ * @see HighlightErrorRecord * @see Severity */ -public sealed class HighlightRecord permits HighlightErrorRecord, HighlightLogRecord { +public class HighlightRecord { /** * Returns a builder for creating a new error record. diff --git a/sdk/highlight-java/highlight-log/highlight-log-java/src/main/java/io/highlight/log/java/HighlightLoggerHandler.java b/sdk/highlight-java/highlight-log/highlight-log-java/src/main/java/io/highlight/log/java/HighlightLoggerHandler.java index 8bdb0add864..03160c0e0a6 100644 --- a/sdk/highlight-java/highlight-log/highlight-log-java/src/main/java/io/highlight/log/java/HighlightLoggerHandler.java +++ b/sdk/highlight-java/highlight-log/highlight-log-java/src/main/java/io/highlight/log/java/HighlightLoggerHandler.java @@ -51,7 +51,7 @@ public void publish(LogRecord logRecord) { .put(HighlightAttributes.HIGHLIGHT_JAVA_SEQUENCE_NUMBER, logRecord.getSequenceNumber()) .put(HighlightAttributes.HIGHLIGHT_JAVA_SOURCE_CLASS_NAME, logRecord.getSourceClassName()) .put(HighlightAttributes.HIGHLIGHT_JAVA_SOURCE_METHOD_NAME, logRecord.getSourceMethodName()) - .put(HighlightAttributes.HIGHLIGHT_JAVA_THREAD_ID, logRecord.getLongThreadID())); + .put(HighlightAttributes.HIGHLIGHT_JAVA_THREAD_ID, logRecord.getThreadID())); this.highlight.capture(highlightRecord.build()); } diff --git a/sdk/highlight-java/highlight-sdk/src/main/java/io/highlight/sdk/Highlight.java b/sdk/highlight-java/highlight-sdk/src/main/java/io/highlight/sdk/Highlight.java index 3c503a6f37e..d624bfb2d27 100644 --- a/sdk/highlight-java/highlight-sdk/src/main/java/io/highlight/sdk/Highlight.java +++ b/sdk/highlight-java/highlight-sdk/src/main/java/io/highlight/sdk/Highlight.java @@ -245,9 +245,11 @@ public void capture(HighlightRecord record) { throw new HighlightIllegalStateException("Highlight state is not running"); } - if (record instanceof HighlightLogRecord logRecord) { + if (record instanceof HighlightLogRecord) { + HighlightLogRecord logRecord = (HighlightLogRecord) record; this.logger.process(logRecord); - } else if (record instanceof HighlightErrorRecord errorRecord) { + } else if (record instanceof HighlightErrorRecord) { + HighlightErrorRecord errorRecord = (HighlightErrorRecord) record; this.tracer.process(errorRecord); } else { throw new HighlightInvalidRecordException("Invalid record type", record); diff --git a/sdk/highlight-java/highlight-sdk/src/main/java/io/highlight/sdk/HighlightSession.java b/sdk/highlight-java/highlight-sdk/src/main/java/io/highlight/sdk/HighlightSession.java index 9fd5a205277..2d61cc698a7 100644 --- a/sdk/highlight-java/highlight-sdk/src/main/java/io/highlight/sdk/HighlightSession.java +++ b/sdk/highlight-java/highlight-sdk/src/main/java/io/highlight/sdk/HighlightSession.java @@ -13,68 +13,103 @@ * It implements the HighlightSessionId interface which provides a unique * identifier for the session. */ -public record HighlightSession(String sessionId) implements HighlightSessionId { +public final class HighlightSession implements HighlightSessionId { + private final String sessionId; - /** - * Captures a record and add the current session ID. - * - * @param record The builder used to build the log record. - */ - public void captureRecord(HighlightRecord.Builder record) { - record.userSession(this); + /** + * + */ + public HighlightSession(String sessionId) { + this.sessionId = sessionId; + } - Highlight.captureRecord(record.build()); - } + /** + * Captures a record and add the current session ID. + * + * @param record The builder used to build the log record. + */ + public void captureRecord(HighlightRecord.Builder record) { + record.userSession(this); - /** - * Captures the provided record.
- * If the record is a HighlightLogRecord, a log record will be captured.
- * If the record is a HighlightErrorRecord, an error record will be captured. - *
- * Otherwise, a HighlightInvalidRecordException will be thrown. - * - * @throws HighlightInvalidRecordException when the given class is invalid - * @param record The record to be captured. - */ - public void captureRecord(HighlightRecord record) { - if (record instanceof HighlightLogRecord logRecord) { - this.captureRecord(HighlightRecord.log(logRecord)); - } else if (record instanceof HighlightErrorRecord errorRecord) { - this.captureRecord(HighlightRecord.error(errorRecord)); - } else { - throw new HighlightInvalidRecordException("Invalid record type", record); - } - } + Highlight.captureRecord(record.build()); + } - /** - * Captures an error record for the provided throwable. - * - * @param error The throwable for which to capture an error record. - */ - public void captureException(Throwable error) { - this.captureRecord(HighlightRecord.error() - .throwable(error)); - } + /** + * Captures the provided record.
+ * If the record is a HighlightLogRecord, a log record will be captured.
+ * If the record is a HighlightErrorRecord, an error record will be captured. + *
+ * Otherwise, a HighlightInvalidRecordException will be thrown. + * + * @param record The record to be captured. + * @throws HighlightInvalidRecordException when the given class is invalid + */ + public void captureRecord(HighlightRecord record) { + if (record instanceof HighlightLogRecord) { + HighlightLogRecord logRecord = (HighlightLogRecord) record; + this.captureRecord(HighlightRecord.log(logRecord)); + } else if (record instanceof HighlightErrorRecord) { + HighlightErrorRecord errorRecord = (HighlightErrorRecord) record; + this.captureRecord(HighlightRecord.error(errorRecord)); + } else { + throw new HighlightInvalidRecordException("Invalid record type", record); + } + } - /** - * Captures a log record with the provided severity. - * - * @param severity The severity of the log record to be captured. - */ - public void captureLog(Severity severity) { - this.captureRecord(HighlightRecord.log() - .severity(severity)); - } + /** + * Captures an error record for the provided throwable. + * + * @param error The throwable for which to capture an error record. + */ + public void captureException(Throwable error) { + this.captureRecord(HighlightRecord.error() + .throwable(error)); + } + + /** + * Captures a log record with the provided severity. + * + * @param severity The severity of the log record to be captured. + */ + public void captureLog(Severity severity) { + this.captureRecord(HighlightRecord.log() + .severity(severity)); + } + + /** + * Captures a log record with the provided severity and message. + * + * @param severity The severity of the log record to be captured. + * @param message The message to be included in the log record. + */ + public void captureLog(Severity severity, String message) { + this.captureRecord(HighlightRecord.log() + .severity(severity) + .message(message)); + } + + @java.lang.Override + public String sessionId() { + return sessionId; + } + + @java.lang.Override + public boolean equals(java.lang.Object obj) { + if (obj == this) return true; + if (obj == null || obj.getClass() != this.getClass()) return false; + var that = (HighlightSession) obj; + return java.util.Objects.equals(this.sessionId, that.sessionId); + } + + @java.lang.Override + public int hashCode() { + return java.util.Objects.hash(sessionId); + } + + @java.lang.Override + public String toString() { + return "HighlightSession[" + + "sessionId=" + sessionId + ']'; + } - /** - * Captures a log record with the provided severity and message. - * - * @param severity The severity of the log record to be captured. - * @param message The message to be included in the log record. - */ - public void captureLog(Severity severity, String message) { - this.captureRecord(HighlightRecord.log() - .severity(severity) - .message(message)); - } } \ No newline at end of file diff --git a/sdk/highlight-java/pom.xml b/sdk/highlight-java/pom.xml index 86378d7aeb7..6b007faaa26 100644 --- a/sdk/highlight-java/pom.xml +++ b/sdk/highlight-java/pom.xml @@ -46,8 +46,8 @@ ${revision} ${revision} java - 17 - 17 + 11 + 11 true true @@ -177,7 +177,7 @@ maven-compiler-plugin ${plugin.compile.version} - 17 + 11