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 <blakeli@google.com>
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy