Skip to content

Commit

Permalink
Add metrics logs observer
Browse files Browse the repository at this point in the history
  • Loading branch information
NipunaMadhushan committed Nov 19, 2024
1 parent 265054d commit f37ec1d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ballerina/init.bal
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ function init() {
externPrintError("failed to enable tracing");
}
}

if (observe:isMetricsLogsEnabled()) {
var err = externEnableMetricsLogging(observe:getTracingProvider());
if (err is error) {
externPrintError("failed to enable tracing");
}
}
}

function externEnableMetrics(string provider) returns error? = @java:Method {
Expand All @@ -43,6 +50,11 @@ function externEnableTracing(string provider) returns error? = @java:Method {
name: "enableTracing"
} external;

function externEnableMetricsLogging(string provider) returns error? = @java:Method {
'class: "io.ballerina.stdlib.observe.internal.NativeFunctions",
name: "enableMetricsLogging"
} external;

function externPrintError(string message) = @java:Method {
'class: "io.ballerina.stdlib.observe.internal.NativeFunctions",
name: "printError"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
*/
package io.ballerina.stdlib.observe.internal;

import io.ballerina.runtime.api.Environment;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.runtime.observability.ObserveUtils;
import io.ballerina.runtime.observability.metrics.BallerinaMetricsLogsObserver;
import io.ballerina.runtime.observability.metrics.BallerinaMetricsObserver;
import io.ballerina.runtime.observability.metrics.DefaultMetricRegistry;
import io.ballerina.runtime.observability.metrics.MetricRegistry;
Expand Down Expand Up @@ -94,6 +96,15 @@ public static BError enableTracing(BString providerName) {
}
}

public static BError enableMetricsLogging(Environment env, BString providerName) {
try {
ObserveUtils.addObserver(new BallerinaMetricsLogsObserver(env));
return null;
} catch (BError e) {
return e;
}
}

public static void printError(BString message) {
errStream.println("error: " + message.getValue());
}
Expand Down

0 comments on commit f37ec1d

Please sign in to comment.