Skip to content

Commit

Permalink
feat: Metrics tracer addAttribute map overload (#3202)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
surbhigarg92 and blakeli0 committed Sep 16, 2024
1 parent e26bc25 commit 1a988df
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ public void addAttributes(String key, String value) {
attributes.put(key, value);
};

/**
* Add attributes that will be attached to all metrics. This is expected to be called by
* handwritten client teams to add additional attributes that are not supposed be collected by
* Gax.
*/
public void addAttributes(Map<String, String> attributes) {
this.attributes.putAll(attributes);
};

@VisibleForTesting
Map<String, String> getAttributes() {
return attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.api.gax.rpc.testing.FakeStatusCode;
import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -228,6 +229,16 @@ void testAddAttributes_recordsAttributes() {
assertThat(metricsTracer.getAttributes().get("FakeTableId")).isEqualTo("12345");
}

@Test
void testAddAttributes_recordsAttributesWithMap() {
Map<String, String> attributes = new HashMap<>();
attributes.put("FakeTableId", "12345");
attributes.put("FakeInstanceId", "67890");
metricsTracer.addAttributes(attributes);
assertThat(metricsTracer.getAttributes().get("FakeTableId")).isEqualTo("12345");
assertThat(metricsTracer.getAttributes().get("FakeInstanceId")).isEqualTo("67890");
}

@Test
void testExtractStatus_errorConversion_apiExceptions() {
ApiException error =
Expand Down

0 comments on commit 1a988df

Please sign in to comment.