-
Notifications
You must be signed in to change notification settings - Fork 753
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
Add Ballerina metrics logs observer #43615
Open
NipunaMadhushan
wants to merge
8
commits into
ballerina-platform:master
Choose a base branch
from
NipunaMadhushan:java21-metrics-logs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+9
−192
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9430ea7
Add Ballerina metrics logs observer
NipunaMadhushan d7133b5
Add comments
NipunaMadhushan 80b4401
Resolve checkstyle errors
NipunaMadhushan baf6570
Resolve checkstyle errors
NipunaMadhushan 6a81b65
Add license header
NipunaMadhushan 4c7862e
Move observer classes to ballerinai/observe module
NipunaMadhushan 4132840
Remove metricsLogsProvider config
NipunaMadhushan 70ee310
Update defining configuration
NipunaMadhushan 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 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
107 changes: 107 additions & 0 deletions
107
...rc/main/java/io/ballerina/runtime/observability/metrics/BallerinaMetricsLogsObserver.java
This file contains 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,107 @@ | ||
package io.ballerina.runtime.observability.metrics; | ||
|
||
import io.ballerina.runtime.api.Environment; | ||
import io.ballerina.runtime.api.Module; | ||
import io.ballerina.runtime.api.creators.ValueCreator; | ||
import io.ballerina.runtime.api.utils.StringUtils; | ||
import io.ballerina.runtime.api.values.BMap; | ||
import io.ballerina.runtime.api.values.BString; | ||
import io.ballerina.runtime.observability.BallerinaObserver; | ||
import io.ballerina.runtime.observability.ObserveUtils; | ||
import io.ballerina.runtime.observability.ObserverContext; | ||
|
||
import java.io.PrintStream; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import static io.ballerina.runtime.observability.ObservabilityConstants.*; | ||
|
||
NipunaMadhushan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class BallerinaMetricsLogsObserver implements BallerinaObserver { | ||
NipunaMadhushan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static final String ORG_NAME = "ballerinax"; | ||
private static final String PROPERTY_START_TIME = "_observation_start_time_"; | ||
private static final PrintStream consoleError = System.err; | ||
|
||
private static Environment environment; | ||
|
||
public BallerinaMetricsLogsObserver(Environment environment) { | ||
BallerinaMetricsLogsObserver.environment = environment; | ||
} | ||
|
||
@Override | ||
public void startServerObservation(ObserverContext observerContext) { | ||
} | ||
|
||
@Override | ||
public void startClientObservation(ObserverContext observerContext) { | ||
} | ||
|
||
@Override | ||
public void stopServerObservation(ObserverContext observerContext) { | ||
if (!observerContext.isStarted()) { | ||
// Do not collect metrics if the observation hasn't started | ||
return; | ||
} | ||
stopObservation(observerContext); | ||
} | ||
|
||
@Override | ||
public void stopClientObservation(ObserverContext observerContext) { | ||
if (!observerContext.isStarted()) { | ||
// Do not collect metrics if the observation hasn't started | ||
return; | ||
} | ||
stopObservation(observerContext); | ||
} | ||
|
||
private void stopObservation(ObserverContext observerContext) { | ||
Set<Tag> tags = new HashSet<>(); | ||
Map<String, Tag> customTags = observerContext.customMetricTags; | ||
if (customTags != null) { | ||
tags.addAll(customTags.values()); | ||
} | ||
tags.addAll(observerContext.getAllTags()); | ||
|
||
// Add status_code_group tag | ||
Integer statusCode = (Integer) observerContext.getProperty(PROPERTY_KEY_HTTP_STATUS_CODE); | ||
if (statusCode != null && statusCode > 0) { | ||
tags.add(Tag.of(TAG_KEY_HTTP_STATUS_CODE_GROUP, (statusCode / 100) + STATUS_CODE_GROUP_SUFFIX)); | ||
} | ||
|
||
try { | ||
Long startTime = (Long) observerContext.getProperty(PROPERTY_START_TIME); | ||
long duration = System.nanoTime() - startTime; | ||
|
||
Optional<String> protocolValue = Optional.empty(); | ||
if (tags.stream().anyMatch(tag -> tag.getKey().equals("protocol"))) { | ||
protocolValue = tags.stream().filter(tag -> tag.getKey().equals("protocol")).map(Tag::getValue).findFirst(); | ||
} | ||
String protocol = protocolValue.orElse("http"); | ||
|
||
BMap<BString, Object> logAttributes = ValueCreator.createMapValue(); | ||
logAttributes.put(StringUtils.fromString("protocol"), StringUtils.fromString(protocol)); | ||
tags.stream().filter(tag -> !tag.getKey().equals("protocol")) | ||
.forEach(tag -> logAttributes.put(StringUtils.fromString(tag.getKey()), | ||
StringUtils.fromString(tag.getValue()))); | ||
logAttributes.put(StringUtils.fromString("response_time_seconds"), | ||
StringUtils.fromString(String.valueOf(duration / 1E9))); | ||
|
||
printMetricLog(logAttributes); | ||
} catch (RuntimeException e) { | ||
handleError("multiple metrics", tags, e); | ||
} | ||
} | ||
|
||
private void handleError(String metricName, Set<Tag> tags, RuntimeException e) { | ||
// Metric Provider may throw exceptions if there is a mismatch in tags. | ||
consoleError.println("error: error collecting metrics for " + metricName + " with tags " + tags + | ||
": " + e.getMessage()); | ||
} | ||
|
||
private static void printMetricLog(BMap<BString, Object> logAttributes) { | ||
// TODO: Remove version when the API is finalized, and add the configured org name. | ||
Module metricsLogsModule = new Module(ORG_NAME, ObserveUtils.getMetricsLogsProvider().getValue(), "1"); | ||
environment.getRuntime().callFunction(metricsLogsModule, "printMetricsLog", null, logAttributes); | ||
} | ||
} |
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.
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.
License text missing