Skip to content

Commit bf7ffd2

Browse files
authored
Merge pull request #142 from NipunaMadhushan/add-get-tag-api
Include addTag and getTagValue APIs
2 parents 92e7f58 + e8486f1 commit bf7ffd2

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed

ballerina/natives.bal

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ public isolated function startSpan(string spanName, map<string>? tags = (), int
4242
name: "startSpan"
4343
} external;
4444

45+
# Add a key-value pair as a tag to the root.
46+
#
47+
# + tagKey - Key of the tag
48+
# + tagValue - Value of the tag
49+
# + return - An error if adding the tag failed; otherwise null
50+
public isolated function addTag(string tagKey, string tagValue) returns error? = @java:Method {
51+
'class: "io.ballerina.stdlib.observe.nativeimpl.AddTag",
52+
name: "addTag"
53+
} external;
54+
55+
# Get the value of a given key of a tag.
56+
#
57+
# + tagKey - Key of the tag
58+
# + return - The value of the tag if present; otherwise null
59+
public isolated function getTagValue(string tagKey) returns string? = @java:Method {
60+
'class: "io.ballerina.stdlib.observe.nativeimpl.GetTagValue",
61+
name: "getTagValue"
62+
} external;
63+
4564
# Add a key value pair as a tag to the span.
4665
#
4766
# + spanId - Id of span to which the tags should be added or -1 to add tags to the current active span

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
org.gradle.caching=true
1616
group=io.ballerina.stdlib
17-
version=1.5.1-SNAPSHOT
18-
ballerinaLangVersion=2201.12.0
17+
version=1.6.0-SNAPSHOT
18+
ballerinaLangVersion=2201.13.0-20251016-073200-0d348172
1919

2020
spotbugsPluginVersion=6.0.18
2121
shadowJarPluginVersion=8.1.1
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2025, WSO2 LLC. (http://wso2.com).
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.ballerina.stdlib.observe.nativeimpl;
18+
19+
import io.ballerina.runtime.api.values.BString;
20+
import io.ballerina.runtime.observability.ObserveUtils;
21+
22+
public class AddTag {
23+
public static void addTag(BString tagKey, BString tagValue) {
24+
if (ObserveUtils.isObservabilityEnabled()) {
25+
ObserveUtils.addTag(tagKey.getValue(), tagValue.getValue());
26+
}
27+
}
28+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2025, WSO2 LLC. (http://wso2.com).
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.ballerina.stdlib.observe.nativeimpl;
18+
19+
import io.ballerina.runtime.api.Environment;
20+
import io.ballerina.runtime.api.utils.StringUtils;
21+
import io.ballerina.runtime.api.values.BString;
22+
import io.ballerina.runtime.observability.ObserveUtils;
23+
import io.ballerina.runtime.observability.ObserverContext;
24+
import io.ballerina.runtime.observability.metrics.Tag;
25+
26+
public class GetTagValue {
27+
public static Object getTagValue(Environment env, BString tagKey) {
28+
if (ObserveUtils.isObservabilityEnabled()) {
29+
ObserverContext observerContext = ObserveUtils.getObserverContextOfCurrentFrame(env);
30+
if (observerContext == null) {
31+
return null;
32+
}
33+
Tag tag = observerContext.getTag(tagKey.getValue());
34+
if (tag != null) {
35+
return StringUtils.fromString(tag.getValue());
36+
}
37+
return null;
38+
}
39+
return null;
40+
}
41+
}

0 commit comments

Comments
 (0)