From f37ec1dcfbe7e69a6b0badd79f925785c411af15 Mon Sep 17 00:00:00 2001 From: NipunaMadhushan Date: Tue, 19 Nov 2024 22:02:18 +0530 Subject: [PATCH] Add metrics logs observer --- ballerina/init.bal | 12 ++++++++++++ .../stdlib/observe/internal/NativeFunctions.java | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/ballerina/init.bal b/ballerina/init.bal index 688708b..acfc22d 100644 --- a/ballerina/init.bal +++ b/ballerina/init.bal @@ -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 { @@ -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" diff --git a/native/src/main/java/io/ballerina/stdlib/observe/internal/NativeFunctions.java b/native/src/main/java/io/ballerina/stdlib/observe/internal/NativeFunctions.java index 548c4be..ea80c63 100644 --- a/native/src/main/java/io/ballerina/stdlib/observe/internal/NativeFunctions.java +++ b/native/src/main/java/io/ballerina/stdlib/observe/internal/NativeFunctions.java @@ -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; @@ -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()); }