-
Notifications
You must be signed in to change notification settings - Fork 0
Extract common functionality into core #6
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
judoscale-core/src/main/java/com/judoscale/core/Adapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.judoscale.core; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Represents an adapter that integrates with Judoscale. | ||
| * Each adapter (e.g., Spring Boot, job queue libraries) provides its name and version | ||
| * for identification in the metrics report. | ||
| */ | ||
| public final class Adapter { | ||
|
|
||
| private final String name; | ||
| private final String version; | ||
|
|
||
| /** | ||
| * Creates an Adapter with the specified name and version. | ||
| * | ||
| * @param name the adapter name (e.g., "judoscale-spring-boot", "judoscale-spring-boot-2"), must not be null | ||
| * @param version the adapter version, must not be null | ||
| */ | ||
| public Adapter(String name, String version) { | ||
| if (name == null) { | ||
| throw new IllegalArgumentException("Adapter name must not be null"); | ||
| } | ||
| if (version == null) { | ||
| throw new IllegalArgumentException("Adapter version must not be null"); | ||
| } | ||
| this.name = name; | ||
| this.version = version; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the adapter name. | ||
| * | ||
| * @return the adapter name | ||
| */ | ||
| public String name() { | ||
| return name; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the adapter version. | ||
| * | ||
| * @return the adapter version | ||
| */ | ||
| public String version() { | ||
| return version; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Adapter adapter = (Adapter) o; | ||
| return Objects.equals(name, adapter.name) && Objects.equals(version, adapter.version); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(name, version); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Adapter{name='" + name + "', version='" + version + "'}"; | ||
| } | ||
| } |
119 changes: 119 additions & 0 deletions
119
judoscale-core/src/main/java/com/judoscale/core/ConfigBase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package com.judoscale.core; | ||
|
|
||
| /** | ||
| * Base configuration for Judoscale. | ||
| * Contains all configuration properties and logic shared across frameworks. | ||
| * | ||
| * <p>Framework-specific implementations (e.g., Spring Boot) should extend this | ||
| * class and add their configuration binding annotations.</p> | ||
| */ | ||
| public class ConfigBase { | ||
|
|
||
| /** | ||
| * The base URL for the Judoscale API. | ||
| * Typically set via JUDOSCALE_URL environment variable. | ||
| */ | ||
| private String apiBaseUrl; | ||
|
|
||
| /** | ||
| * Alternative property for the API URL (maps to JUDOSCALE_URL env var via relaxed binding). | ||
| */ | ||
| private String url; | ||
|
|
||
| /** | ||
| * How often to report metrics, in seconds. Default is 10. | ||
| */ | ||
| private int reportIntervalSeconds = 10; | ||
|
|
||
| /** | ||
| * Maximum request body size in bytes before ignoring queue time. | ||
| * Large requests can skew queue time measurements. Default is 100KB. | ||
| */ | ||
| private int maxRequestSizeBytes = 100_000; | ||
|
|
||
| /** | ||
| * Whether to ignore queue time for large requests. Default is true. | ||
| */ | ||
| private boolean ignoreLargeRequests = true; | ||
|
|
||
| /** | ||
| * Log level for Judoscale logging. Default is INFO. | ||
| */ | ||
| private String logLevel = "INFO"; | ||
|
|
||
| /** | ||
| * Whether Judoscale is enabled. Default is true. | ||
| */ | ||
| private boolean enabled = true; | ||
|
|
||
| /** | ||
| * Returns the API base URL, preferring explicit apiBaseUrl over url. | ||
| */ | ||
| public String getApiBaseUrl() { | ||
| // Prefer explicit apiBaseUrl, fall back to url (which binds to JUDOSCALE_URL) | ||
| if (apiBaseUrl != null && !apiBaseUrl.trim().isEmpty()) { | ||
| return apiBaseUrl; | ||
| } | ||
| return url; | ||
| } | ||
|
|
||
| public void setApiBaseUrl(String apiBaseUrl) { | ||
| this.apiBaseUrl = apiBaseUrl; | ||
| } | ||
|
|
||
| public String getUrl() { | ||
| return url; | ||
| } | ||
|
|
||
| public void setUrl(String url) { | ||
| this.url = url; | ||
| } | ||
|
|
||
| public int getReportIntervalSeconds() { | ||
| return reportIntervalSeconds; | ||
| } | ||
|
|
||
| public void setReportIntervalSeconds(int reportIntervalSeconds) { | ||
| this.reportIntervalSeconds = reportIntervalSeconds; | ||
| } | ||
|
|
||
| public int getMaxRequestSizeBytes() { | ||
| return maxRequestSizeBytes; | ||
| } | ||
|
|
||
| public void setMaxRequestSizeBytes(int maxRequestSizeBytes) { | ||
| this.maxRequestSizeBytes = maxRequestSizeBytes; | ||
| } | ||
|
|
||
| public boolean isIgnoreLargeRequests() { | ||
| return ignoreLargeRequests; | ||
| } | ||
|
|
||
| public void setIgnoreLargeRequests(boolean ignoreLargeRequests) { | ||
| this.ignoreLargeRequests = ignoreLargeRequests; | ||
| } | ||
|
|
||
| public String getLogLevel() { | ||
| return logLevel; | ||
| } | ||
|
|
||
| public void setLogLevel(String logLevel) { | ||
| this.logLevel = logLevel; | ||
| } | ||
|
|
||
| public boolean isEnabled() { | ||
| return enabled; | ||
| } | ||
|
|
||
| public void setEnabled(boolean enabled) { | ||
| this.enabled = enabled; | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the API URL is configured and not blank. | ||
| */ | ||
| public boolean isConfigured() { | ||
| String configuredUrl = getApiBaseUrl(); | ||
| return configuredUrl != null && !configuredUrl.trim().isEmpty(); | ||
| } | ||
| } |
82 changes: 82 additions & 0 deletions
82
judoscale-core/src/main/java/com/judoscale/core/QueueTimeCalculator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package com.judoscale.core; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| /** | ||
| * Utility class for calculating request queue time from the X-Request-Start header. | ||
| * Handles multiple formats: seconds, milliseconds, microseconds, nanoseconds. | ||
| */ | ||
| public final class QueueTimeCalculator { | ||
|
|
||
| // Cutoffs for determining the unit of the X-Request-Start header | ||
| private static final long MILLISECONDS_CUTOFF = Instant.parse("2000-01-01T00:00:00Z").toEpochMilli(); | ||
| private static final long MICROSECONDS_CUTOFF = MILLISECONDS_CUTOFF * 1000; | ||
| private static final long NANOSECONDS_CUTOFF = MICROSECONDS_CUTOFF * 1000; | ||
|
|
||
| private QueueTimeCalculator() { | ||
| // Utility class, no instantiation | ||
| } | ||
|
|
||
| /** | ||
| * Calculates the queue time in milliseconds from the X-Request-Start header. | ||
| * | ||
| * @param requestStartHeader the X-Request-Start header value | ||
| * @param now the current instant | ||
| * @return the queue time in milliseconds, or -1 if the header could not be parsed | ||
| */ | ||
| public static long calculateQueueTime(String requestStartHeader, Instant now) { | ||
| try { | ||
| // Strip any non-numeric characters (e.g., "t=" prefix from NGINX) | ||
| String cleanValue = requestStartHeader.replaceAll("[^0-9.]", ""); | ||
|
|
||
| long startTimeMs; | ||
|
|
||
| // Use long parsing for integer values to avoid precision loss with large timestamps | ||
| // (nanosecond timestamps can exceed double's precision) | ||
| if (!cleanValue.contains(".")) { | ||
| long value = Long.parseLong(cleanValue); | ||
| startTimeMs = convertToMillis(value); | ||
| } else { | ||
| // Fractional values (typically seconds from NGINX) | ||
| double value = Double.parseDouble(cleanValue); | ||
| if (value > NANOSECONDS_CUTOFF) { | ||
| startTimeMs = (long) (value / 1_000_000); | ||
| } else if (value > MICROSECONDS_CUTOFF) { | ||
| startTimeMs = (long) (value / 1_000); | ||
| } else if (value > MILLISECONDS_CUTOFF) { | ||
| startTimeMs = (long) value; | ||
| } else { | ||
| // Seconds with fractional part | ||
| startTimeMs = (long) (value * 1000); | ||
| } | ||
| } | ||
|
|
||
| long queueTimeMs = now.toEpochMilli() - startTimeMs; | ||
|
|
||
| // Safeguard against negative queue times | ||
| return Math.max(0, queueTimeMs); | ||
|
|
||
| } catch (NumberFormatException e) { | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts an integer timestamp to milliseconds based on its magnitude. | ||
| */ | ||
| private static long convertToMillis(long value) { | ||
| if (value > NANOSECONDS_CUTOFF) { | ||
| // Nanoseconds (Render) | ||
| return value / 1_000_000; | ||
| } else if (value > MICROSECONDS_CUTOFF) { | ||
| // Microseconds | ||
| return value / 1_000; | ||
| } else if (value > MILLISECONDS_CUTOFF) { | ||
| // Milliseconds (Heroku) | ||
| return value; | ||
| } else { | ||
| // Seconds (integer seconds, rare but possible) | ||
| return value * 1000; | ||
| } | ||
| } | ||
| } |
84 changes: 84 additions & 0 deletions
84
judoscale-core/src/main/java/com/judoscale/core/ReportBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package com.judoscale.core; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Properties; | ||
|
|
||
| /** | ||
| * Utility class for building JSON report payloads for the Judoscale API. | ||
| */ | ||
| public final class ReportBuilder { | ||
|
|
||
| private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| private ReportBuilder() { | ||
| // Utility class, no instantiation | ||
| } | ||
|
|
||
| /** | ||
| * Builds the JSON payload for the metrics report. | ||
| * | ||
| * @param metrics the metrics to include in the report | ||
| * @param adapters the adapters to include in the report (supports multiple adapters) | ||
| * @return the JSON string | ||
| */ | ||
| public static String buildReportJson(List<Metric> metrics, Collection<Adapter> adapters) { | ||
| ObjectNode root = objectMapper.createObjectNode(); | ||
|
|
||
| // Build metrics array: each metric is [timestamp, value, identifier, queueName?] | ||
| ArrayNode metricsArray = objectMapper.createArrayNode(); | ||
| for (Metric m : metrics) { | ||
| ArrayNode metricArray = objectMapper.createArrayNode(); | ||
| metricArray.add(m.time().getEpochSecond()); | ||
| metricArray.add(m.value()); | ||
| metricArray.add(m.identifier()); | ||
| if (m.queueName() != null) { | ||
| metricArray.add(m.queueName()); | ||
| } | ||
| metricsArray.add(metricArray); | ||
| } | ||
| root.set("metrics", metricsArray); | ||
|
|
||
| // Build adapters object - each adapter provides its own name and version | ||
| ObjectNode adaptersNode = objectMapper.createObjectNode(); | ||
| for (Adapter adapter : adapters) { | ||
| ObjectNode adapterNode = objectMapper.createObjectNode(); | ||
| adapterNode.put("adapter_version", adapter.version()); | ||
| adaptersNode.set(adapter.name(), adapterNode); | ||
| } | ||
| root.set("adapters", adaptersNode); | ||
|
|
||
| try { | ||
| return objectMapper.writeValueAsString(root); | ||
| } catch (JsonProcessingException e) { | ||
| throw new RuntimeException("Failed to serialize metrics to JSON", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Loads the adapter version from the META-INF/judoscale.properties file. | ||
| * Falls back to "unknown" if the file cannot be read. | ||
| * | ||
| * @param loaderClass the class to use for loading the resource | ||
| * @return the adapter version | ||
| */ | ||
| public static String loadAdapterVersion(Class<?> loaderClass) { | ||
| try (InputStream is = loaderClass.getResourceAsStream("/META-INF/judoscale.properties")) { | ||
| if (is != null) { | ||
| Properties props = new Properties(); | ||
| props.load(is); | ||
| return props.getProperty("version", "unknown"); | ||
| } | ||
| } catch (IOException e) { | ||
| // Fall through to return unknown | ||
| } | ||
| return "unknown"; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.