Skip to content

Commit b9241fb

Browse files
iroquetaBeta Bot
authored andcommitted
Cherry pick branch 'genexuslabs:feature/gxobservability-testing-and-docs' into beta
1 parent 3332293 commit b9241fb

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.genexus.opentelemetry;
2+
3+
/**
4+
* Helper class to provide a testable version of the extractAttributeValue method from OtelTracer.
5+
* This is used to support unit testing of the private method.
6+
*/
7+
public class ExtractAttributeValueHelper {
8+
9+
/**
10+
* Extracts an attribute value from a resource attributes string
11+
*
12+
* @param resourceAttributes the resource attributes string
13+
* @param attributeName the name of the attribute to extract
14+
* @return the attribute value, or null if not found
15+
*/
16+
public static String extractAttributeValue(String resourceAttributes, String attributeName) {
17+
if (resourceAttributes == null || attributeName == null) {
18+
return null;
19+
}
20+
21+
// Simple parsing for key-value pairs
22+
String[] pairs = resourceAttributes.split(",");
23+
for (String pair : pairs) {
24+
String[] keyValue = pair.split("=");
25+
if (keyValue.length == 2 && keyValue[0].equals(attributeName)) {
26+
return keyValue[1];
27+
}
28+
}
29+
return null;
30+
}
31+
}

gxobservability/src/main/java/com/genexus/opentelemetry/OtelTracer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private static String extractAttributeValue(String resourceAttributes, String at
179179
return null;
180180
}
181181

182-
String pattern = "(?:\\b\\w+\\b=\\w+)(?:,(?:\\b\\w+\\b=\\w+))*";
182+
String pattern = "(\\w[\\w.\\-]*?)=([^,]+)";
183183
Pattern regex = Pattern.compile(pattern);
184184
Matcher matcher = regex.matcher(resourceAttributes);
185185

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.genexus.opentelemetry;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
public class ExtractAttributeValueTest {
7+
8+
@Test
9+
void testExtractAttributeValue() {
10+
// Test case 1: Test with null parameters
11+
String attributeValue = ExtractAttributeValueHelper.extractAttributeValue(null, "service.name");
12+
assertNull(attributeValue, "Should return null for null resource attributes");
13+
14+
attributeValue = ExtractAttributeValueHelper.extractAttributeValue("service.name=test", null);
15+
assertNull(attributeValue, "Should return null for null attribute name");
16+
17+
// Test case 2: Empty resource attributes
18+
attributeValue = ExtractAttributeValueHelper.extractAttributeValue("", "service.name");
19+
assertNull(attributeValue, "Should return null for empty resource attributes");
20+
21+
// Test case 3: Simple key-value pair
22+
String simpleAttributes = "service.name=test-service";
23+
attributeValue = ExtractAttributeValueHelper.extractAttributeValue(simpleAttributes, "service.name");
24+
assertEquals("test-service", attributeValue, "Should extract simple attribute correctly");
25+
26+
// Test case 4: Multiple key-value pairs
27+
String multiAttributes = "service.name=my-service,service.version=1.0.0";
28+
attributeValue = ExtractAttributeValueHelper.extractAttributeValue(multiAttributes, "service.name");
29+
assertEquals("my-service", attributeValue, "Should extract first attribute correctly");
30+
31+
attributeValue = ExtractAttributeValueHelper.extractAttributeValue(multiAttributes, "service.version");
32+
assertEquals("1.0.0", attributeValue, "Should extract second attribute correctly");
33+
34+
// Test case 5: Non-existent attribute
35+
attributeValue = ExtractAttributeValueHelper.extractAttributeValue(multiAttributes, "non.existent");
36+
assertNull(attributeValue, "Should return null for non-existent attribute");
37+
}
38+
}

gxobservability/src/test/java/com/genexus/opentelemetry/OtelTracerTest.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.junit.jupiter.api.Test;
1111
import static org.junit.jupiter.api.Assertions.*;
1212

13+
import java.lang.reflect.Method;
14+
1315
import java.util.ArrayList;
1416
import java.util.List;
1517

@@ -121,4 +123,71 @@ void testCreateSpanWithNullIterator() {
121123

122124
assertNotNull(span);
123125
}
126+
127+
@Test
128+
void testExtractAttributeValue() throws Exception {
129+
// Get access to the private method using reflection
130+
Method extractAttributeValueMethod = OtelTracer.class.getDeclaredMethod(
131+
"extractAttributeValue",
132+
String.class,
133+
String.class
134+
);
135+
extractAttributeValueMethod.setAccessible(true);
136+
137+
// Test case 1: Test with null parameters
138+
String attributeValue = (String) extractAttributeValueMethod.invoke(
139+
null,
140+
null,
141+
"service.name"
142+
);
143+
assertNull(attributeValue, "Should return null for null resource attributes");
144+
145+
attributeValue = (String) extractAttributeValueMethod.invoke(
146+
null,
147+
"service.name=test",
148+
null
149+
);
150+
assertNull(attributeValue, "Should return null for null attribute name");
151+
152+
// Test case 2: Empty resource attributes
153+
attributeValue = (String) extractAttributeValueMethod.invoke(
154+
null,
155+
"",
156+
"service.name"
157+
);
158+
assertNull(attributeValue, "Should return null for empty resource attributes");
159+
160+
// Test case 3: Simple key-value pair
161+
String simpleAttributes = "service.name=test-service";
162+
attributeValue = (String) extractAttributeValueMethod.invoke(
163+
null,
164+
simpleAttributes,
165+
"service.name"
166+
);
167+
assertEquals("test-service", attributeValue, "Should extract simple attribute correctly");
168+
169+
// Test case 4: Multiple key-value pairs
170+
String multiAttributes = "service.name=my-service,service.version=1.0.0";
171+
attributeValue = (String) extractAttributeValueMethod.invoke(
172+
null,
173+
multiAttributes,
174+
"service.name"
175+
);
176+
assertEquals("my-service", attributeValue, "Should extract first attribute correctly");
177+
178+
attributeValue = (String) extractAttributeValueMethod.invoke(
179+
null,
180+
multiAttributes,
181+
"service.version"
182+
);
183+
assertEquals("1.0.0", attributeValue, "Should extract second attribute correctly");
184+
185+
// Test case 5: Non-existent attribute
186+
attributeValue = (String) extractAttributeValueMethod.invoke(
187+
null,
188+
multiAttributes,
189+
"non.existent"
190+
);
191+
assertNull(attributeValue, "Should return null for non-existent attribute");
192+
}
124193
}

0 commit comments

Comments
 (0)