Skip to content

Commit 1a988df

Browse files
feat: Metrics tracer addAttribute map overload (#3202)
This PR adds an overload for adding multiple attributes at once. Earlier the client had to call the `addAttributes` method in a loop to add each attributes. Below overload takes a map and add all the attributes at once ``` public void addAttributes(Map<String, String> attributes) { this.attributes.putAll(attributes); }; ``` --------- Co-authored-by: Blake Li <[email protected]>
1 parent e26bc25 commit 1a988df

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

gax-java/gax/src/main/java/com/google/api/gax/tracing/MetricsTracer.java

+9
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ public void addAttributes(String key, String value) {
245245
attributes.put(key, value);
246246
};
247247

248+
/**
249+
* Add attributes that will be attached to all metrics. This is expected to be called by
250+
* handwritten client teams to add additional attributes that are not supposed be collected by
251+
* Gax.
252+
*/
253+
public void addAttributes(Map<String, String> attributes) {
254+
this.attributes.putAll(attributes);
255+
};
256+
248257
@VisibleForTesting
249258
Map<String, String> getAttributes() {
250259
return attributes;

gax-java/gax/src/test/java/com/google/api/gax/tracing/MetricsTracerTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.google.api.gax.rpc.StatusCode.Code;
4444
import com.google.api.gax.rpc.testing.FakeStatusCode;
4545
import com.google.common.collect.ImmutableMap;
46+
import java.util.HashMap;
4647
import java.util.Map;
4748
import org.junit.jupiter.api.BeforeEach;
4849
import org.junit.jupiter.api.Test;
@@ -228,6 +229,16 @@ void testAddAttributes_recordsAttributes() {
228229
assertThat(metricsTracer.getAttributes().get("FakeTableId")).isEqualTo("12345");
229230
}
230231

232+
@Test
233+
void testAddAttributes_recordsAttributesWithMap() {
234+
Map<String, String> attributes = new HashMap<>();
235+
attributes.put("FakeTableId", "12345");
236+
attributes.put("FakeInstanceId", "67890");
237+
metricsTracer.addAttributes(attributes);
238+
assertThat(metricsTracer.getAttributes().get("FakeTableId")).isEqualTo("12345");
239+
assertThat(metricsTracer.getAttributes().get("FakeInstanceId")).isEqualTo("67890");
240+
}
241+
231242
@Test
232243
void testExtractStatus_errorConversion_apiExceptions() {
233244
ApiException error =

0 commit comments

Comments
 (0)