From 5eb79cf56908e8a53d746261fc5ea4ae7164b210 Mon Sep 17 00:00:00 2001 From: NipunaMadhushan Date: Thu, 16 Oct 2025 00:27:30 +0530 Subject: [PATCH 1/5] Include addTagValue API --- ballerina/natives.bal | 19 ++++++++++++++ gradle.properties | 4 +-- .../stdlib/observe/nativeimpl/AddTag.java | 12 +++++++++ .../observe/nativeimpl/GetTagValue.java | 25 +++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/AddTag.java create mode 100644 native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/GetTagValue.java diff --git a/ballerina/natives.bal b/ballerina/natives.bal index ca77e69..853cd34 100644 --- a/ballerina/natives.bal +++ b/ballerina/natives.bal @@ -42,6 +42,25 @@ public isolated function startSpan(string spanName, map? tags = (), int name: "startSpan" } external; +# Add a key value pair as a tag to the root. +# +# + tagKey - Key of the tag +# + tagValue - Value of the tag +# + return - An error if an error occurred while attaching tag to the span +public isolated function addTag(string tagKey, string tagValue) returns error? = @java:Method { + 'class: "io.ballerina.stdlib.observe.nativeimpl.AddTag", + name: "addTag" +} external; + +# Get the value of a given key of a tag. +# +# + tagKey - Key of the tag +# + return - An error if an error occurred while attaching tag to the span +public isolated function getTagValue(string tagKey) returns string? = @java:Method { + 'class: "io.ballerina.stdlib.observe.nativeimpl.GetTagValue", + name: "getTagValue" +} external; + # Add a key value pair as a tag to the span. # # + spanId - Id of span to which the tags should be added or -1 to add tags to the current active span diff --git a/gradle.properties b/gradle.properties index b306c85..d0d2df5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,8 +14,8 @@ org.gradle.caching=true group=io.ballerina.stdlib -version=1.5.1-SNAPSHOT -ballerinaLangVersion=2201.12.0 +version=1.6.0-SNAPSHOT +ballerinaLangVersion=2201.13.0-20251015-051600-27035519 spotbugsPluginVersion=6.0.18 shadowJarPluginVersion=8.1.1 diff --git a/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/AddTag.java b/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/AddTag.java new file mode 100644 index 0000000..3c5edb5 --- /dev/null +++ b/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/AddTag.java @@ -0,0 +1,12 @@ +package io.ballerina.stdlib.observe.nativeimpl; + +import io.ballerina.runtime.api.values.BString; +import io.ballerina.runtime.observability.ObserveUtils; + +public class AddTag { + public static void addTag(BString tagKey, BString tagValue) { + if (ObserveUtils.isObservabilityEnabled()) { + ObserveUtils.addTag(tagKey.getValue(), tagValue.getValue()); + } + } +} diff --git a/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/GetTagValue.java b/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/GetTagValue.java new file mode 100644 index 0000000..73126af --- /dev/null +++ b/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/GetTagValue.java @@ -0,0 +1,25 @@ +package io.ballerina.stdlib.observe.nativeimpl; + +import io.ballerina.runtime.api.Environment; +import io.ballerina.runtime.api.utils.StringUtils; +import io.ballerina.runtime.api.values.BString; +import io.ballerina.runtime.observability.ObserveUtils; +import io.ballerina.runtime.observability.ObserverContext; +import io.ballerina.runtime.observability.metrics.Tag; + +public class GetTagValue { + public static Object getTagValue(Environment env, BString tagKey) { + if (ObserveUtils.isObservabilityEnabled()) { + ObserverContext observerContext = ObserveUtils.getObserverContextOfCurrentFrame(env); + if (observerContext == null) { + return null; + } + Tag tag = observerContext.getTag(tagKey.getValue()); + if (tag != null) { + return StringUtils.fromString(tag.getValue()); + } + return null; + } + return null; + } +} From faf42fc12442ae66ddac6fa4c2d2080b975f134f Mon Sep 17 00:00:00 2001 From: Nipuna Madhushan <51471998+NipunaMadhushan@users.noreply.github.com> Date: Thu, 16 Oct 2025 09:46:29 +0530 Subject: [PATCH 2/5] Update lang version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index d0d2df5..66d9a21 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,7 +15,7 @@ org.gradle.caching=true group=io.ballerina.stdlib version=1.6.0-SNAPSHOT -ballerinaLangVersion=2201.13.0-20251015-051600-27035519 +ballerinaLangVersion=2201.13.0-20251016-073200-0d348172 spotbugsPluginVersion=6.0.18 shadowJarPluginVersion=8.1.1 From 1fa3434a7fdc41e0daa0547aee907d9975fee0a9 Mon Sep 17 00:00:00 2001 From: NipunaMadhushan Date: Thu, 16 Oct 2025 10:29:24 +0530 Subject: [PATCH 3/5] Add license headers --- .../stdlib/observe/nativeimpl/AddTag.java | 16 ++++++++++++++++ .../stdlib/observe/nativeimpl/GetTagValue.java | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/AddTag.java b/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/AddTag.java index 3c5edb5..b78f212 100644 --- a/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/AddTag.java +++ b/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/AddTag.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2025, WSO2 LLC. (http://wso2.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package io.ballerina.stdlib.observe.nativeimpl; import io.ballerina.runtime.api.values.BString; diff --git a/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/GetTagValue.java b/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/GetTagValue.java index 73126af..dec3bea 100644 --- a/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/GetTagValue.java +++ b/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/GetTagValue.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2025, WSO2 LLC. (http://wso2.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package io.ballerina.stdlib.observe.nativeimpl; import io.ballerina.runtime.api.Environment; From 9358dc28f55e8a8f219edd816b71e9d9724c416f Mon Sep 17 00:00:00 2001 From: Nipuna Madhushan <51471998+NipunaMadhushan@users.noreply.github.com> Date: Thu, 16 Oct 2025 10:30:58 +0530 Subject: [PATCH 4/5] Update ballerina/natives.bal Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- ballerina/natives.bal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ballerina/natives.bal b/ballerina/natives.bal index 853cd34..9e236ec 100644 --- a/ballerina/natives.bal +++ b/ballerina/natives.bal @@ -55,7 +55,7 @@ public isolated function addTag(string tagKey, string tagValue) returns error? = # Get the value of a given key of a tag. # # + tagKey - Key of the tag -# + return - An error if an error occurred while attaching tag to the span +# + return - The value of the tag if present; otherwise () public isolated function getTagValue(string tagKey) returns string? = @java:Method { 'class: "io.ballerina.stdlib.observe.nativeimpl.GetTagValue", name: "getTagValue" From e8486f19050adc694b82c2a83cfa8927881e6690 Mon Sep 17 00:00:00 2001 From: NipunaMadhushan Date: Thu, 16 Oct 2025 10:33:50 +0530 Subject: [PATCH 5/5] Address review suggestions --- ballerina/natives.bal | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ballerina/natives.bal b/ballerina/natives.bal index 9e236ec..4e59e23 100644 --- a/ballerina/natives.bal +++ b/ballerina/natives.bal @@ -42,11 +42,11 @@ public isolated function startSpan(string spanName, map? tags = (), int name: "startSpan" } external; -# Add a key value pair as a tag to the root. +# Add a key-value pair as a tag to the root. # # + tagKey - Key of the tag # + tagValue - Value of the tag -# + return - An error if an error occurred while attaching tag to the span +# + return - An error if adding the tag failed; otherwise null public isolated function addTag(string tagKey, string tagValue) returns error? = @java:Method { 'class: "io.ballerina.stdlib.observe.nativeimpl.AddTag", name: "addTag" @@ -55,7 +55,7 @@ public isolated function addTag(string tagKey, string tagValue) returns error? = # Get the value of a given key of a tag. # # + tagKey - Key of the tag -# + return - The value of the tag if present; otherwise () +# + return - The value of the tag if present; otherwise null public isolated function getTagValue(string tagKey) returns string? = @java:Method { 'class: "io.ballerina.stdlib.observe.nativeimpl.GetTagValue", name: "getTagValue"