diff --git a/ballerina/natives.bal b/ballerina/natives.bal index ca77e69..4e59e23 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 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" +} external; + +# Get the value of a given key of a tag. +# +# + tagKey - Key of the tag +# + 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" +} 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..66d9a21 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-20251016-073200-0d348172 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..b78f212 --- /dev/null +++ b/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/AddTag.java @@ -0,0 +1,28 @@ +/* + * 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; +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..dec3bea --- /dev/null +++ b/native/src/main/java/io/ballerina/stdlib/observe/nativeimpl/GetTagValue.java @@ -0,0 +1,41 @@ +/* + * 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; +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; + } +}