feat(o11y): introduce server.port attribute by diegomarquezp · Pull Request #4128 · googleapis/sdk-platform-java · GitHub
Skip to content
This repository was archived by the owner on May 14, 2026. It is now read-only.

feat(o11y): introduce server.port attribute#4128

Merged
diegomarquezp merged 10 commits into
mainfrom
observability/tracing-attr/server.port
Mar 5, 2026
Merged

feat(o11y): introduce server.port attribute#4128
diegomarquezp merged 10 commits into
mainfrom
observability/tracing-attr/server.port

Conversation

@diegomarquezp

@diegomarquezp diegomarquezp commented Mar 2, 2026

Copy link
Copy Markdown
Contributor
image

@product-auto-label product-auto-label Bot added the size: m Pull request size is medium. label Mar 2, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the server.port attribute for observability, which is a valuable addition for tracing. The changes are consistently applied across the codebase, from endpoint parsing in EndpointContext to the OpenTelemetryTraceManager. The necessary API changes, like updating attribute maps to Map<String, Object>, are handled correctly. The tests, including unit and integration tests, are updated to cover the new functionality. I have one suggestion to improve the robustness of attribute handling in OpenTelemetryTraceManager for future maintainability.

Comment on lines +62 to +67
(k, v) -> {
if (v instanceof String) {
spanBuilder.setAttribute(k, (String) v);
} else if (v instanceof Integer) {
spanBuilder.setAttribute(k, (long) (Integer) v);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation for setting attributes on the span only handles String and Integer types. While this is sufficient for the current changes, it makes the code brittle. If other attribute types (like Long, Double, or Boolean) are added in the future, they will be silently ignored. To improve future maintainability, it would be more robust to handle all primitive types supported by OpenTelemetry attributes.

          (k, v) -> {
            if (v instanceof String) {
              spanBuilder.setAttribute(k, (String) v);
            } else if (v instanceof Integer) {
              spanBuilder.setAttribute(k, (long) (Integer) v);
            } else if (v instanceof Long) {
              spanBuilder.setAttribute(k, (Long) v);
            } else if (v instanceof Double) {
              spanBuilder.setAttribute(k, (Double) v);
            } else if (v instanceof Boolean) {
              spanBuilder.setAttribute(k, (Boolean) v);
            }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/gemini I think it's worth to restrict it to only types we are introducing for now. New types are not expected and expanding this list will also imply additional testing (maintenance) for adding only one more type.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the type matter? Would it cause any differences in the UI? If not, can we just use String?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Offline discussion: let's conform to SemConv and keep port as an Integer.

@diegomarquezp diegomarquezp marked this pull request as ready for review March 2, 2026 22:46
}
try {
HostAndPort parsedHostPort = HostAndPort.fromString(hostPort);
if (parsedHostPort.hasPort()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic in this method is pretty much identical to the last method parseServerAddress, can we merge these two?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines +62 to +67
(k, v) -> {
if (v instanceof String) {
spanBuilder.setAttribute(k, (String) v);
} else if (v instanceof Integer) {
spanBuilder.setAttribute(k, (long) (Integer) v);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the type matter? Would it cause any differences in the UI? If not, can we just use String?

.setTransportChannelProviderEndpoint(null)
.build();
Truth.assertThat(endpointContext.resolvedServerAddress()).isEqualTo("localhost");
Truth.assertThat(endpointContext.resolvedServerPort()).isEqualTo(7469);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might missed this in the first PR, it's better to split these test cases to one case per test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (serverAddress() != null) {
attributes.put(ObservabilityAttributes.SERVER_ADDRESS_ATTRIBUTE, serverAddress());
}
if (serverPort() != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to check both null and empty, this applies to all attributes too. We can use Strings.isNullOrEmpty.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the check for the other if-statements, but serverPort() is an Integer.

@diegomarquezp

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the server.port attribute to observability traces, which is a valuable addition. The changes involve parsing the port from the endpoint in EndpointContext and propagating it through to the TraceManager. The related tracing classes have been updated to handle Map<String, Object> for attributes to support the integer-based port number.

I've identified a critical regression in the endpoint parsing logic that affects bare IPv6 addresses. Additionally, there's an opportunity to make the attribute handling in OpenTelemetryTraceManager more robust. Please see my detailed comments.

Comment thread gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java Outdated
Comment thread gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java Outdated
@sonarqubecloud

sonarqubecloud Bot commented Mar 5, 2026

Copy link
Copy Markdown

@sonarqubecloud

sonarqubecloud Bot commented Mar 5, 2026

Copy link
Copy Markdown

@diegomarquezp diegomarquezp merged commit 56aa343 into main Mar 5, 2026
55 of 57 checks passed
@diegomarquezp diegomarquezp deleted the observability/tracing-attr/server.port branch March 5, 2026 20:04
lqiu96 pushed a commit that referenced this pull request Mar 17, 2026
🤖 I have created a release *beep* *boop*
---


<details><summary>2.68.0</summary>

##
[2.68.0](v2.67.0...v2.68.0)
(2026-03-17)


### Features

* Add client request duration metric.
([#4132](#4132))
([6a76397](6a76397))
* Add more attributes to golden signals metrics.
([#4135](#4135))
([59d0624](59d0624))
* **gax-httpjson:** add HttpJsonErrorParser utility
([#4137](#4137))
([a1b7565](a1b7565))
* **generator:** add extra allowed modules that will not be removed from
the monorepo if they are present
([#4124](#4124))
([774fe6e](774fe6e))
* **o11y:** introduce `gcp.client.repo` and `gcp.client.artifact`
attributes
([#4120](#4120))
([105f644](105f644))
* **o11y:** Introduce `rpc.system.name` and `rpc.method` in gRPC
([#4121](#4121))
([7ab6d2e](7ab6d2e))
* **o11y:** introduce server.port attribute
([#4128](#4128))
([56aa343](56aa343))


### Bug Fixes

* add null checks for ApiTracerFactory in ClientContext
([#4122](#4122))
([4b3dbe2](4b3dbe2))
* Decrease log level for directpath warnings outside GCE
([#4139](#4139))
([c9651e7](c9651e7))
* **gax-grpc:** add pick_first fallback to direct path service config
([#4143](#4143))
([b150fe9](b150fe9))
* Populate method level attributes in metrics recording
([#4149](#4149))
([7b7e6c9](7b7e6c9))
* suppress warnings in generated projects for non-idiomatic durations
([#4119](#4119))
([4206e6e](4206e6e))
* Use ServiceName + MethodName as the regex for Otel
([#2543](#2543))
([b9ae73f](b9ae73f))


### Documentation

* **hermetic_build:** fix config field name in readme
([#4130](#4130))
([a0c8f67](a0c8f67))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
chingor13 pushed a commit to googleapis/google-cloud-java that referenced this pull request Mar 20, 2026
🤖 I have created a release *beep* *boop*
---


<details><summary>2.68.0</summary>

##
[2.68.0](googleapis/sdk-platform-java@v2.67.0...v2.68.0)
(2026-03-17)


### Features

* Add client request duration metric.
([#4132](googleapis/sdk-platform-java#4132))
([487650e](googleapis/sdk-platform-java@487650e))
* Add more attributes to golden signals metrics.
([#4135](googleapis/sdk-platform-java#4135))
([bc82dcb](googleapis/sdk-platform-java@bc82dcb))
* **gax-httpjson:** add HttpJsonErrorParser utility
([#4137](googleapis/sdk-platform-java#4137))
([6fe2446](googleapis/sdk-platform-java@6fe2446))
* **generator:** add extra allowed modules that will not be removed from
the monorepo if they are present
([#4124](googleapis/sdk-platform-java#4124))
([6a440da](googleapis/sdk-platform-java@6a440da))
* **o11y:** introduce `gcp.client.repo` and `gcp.client.artifact`
attributes
([#4120](googleapis/sdk-platform-java#4120))
([4954de5](googleapis/sdk-platform-java@4954de5))
* **o11y:** Introduce `rpc.system.name` and `rpc.method` in gRPC
([#4121](googleapis/sdk-platform-java#4121))
([3593c30](googleapis/sdk-platform-java@3593c30))
* **o11y:** introduce server.port attribute
([#4128](googleapis/sdk-platform-java#4128))
([1b10e02](googleapis/sdk-platform-java@1b10e02))


### Bug Fixes

* add null checks for ApiTracerFactory in ClientContext
([#4122](googleapis/sdk-platform-java#4122))
([ded1922](googleapis/sdk-platform-java@ded1922))
* Decrease log level for directpath warnings outside GCE
([#4139](googleapis/sdk-platform-java#4139))
([5151f34](googleapis/sdk-platform-java@5151f34))
* **gax-grpc:** add pick_first fallback to direct path service config
([#4143](googleapis/sdk-platform-java#4143))
([4934ad8](googleapis/sdk-platform-java@4934ad8))
* Populate method level attributes in metrics recording
([#4149](googleapis/sdk-platform-java#4149))
([69aabf8](googleapis/sdk-platform-java@69aabf8))
* suppress warnings in generated projects for non-idiomatic durations
([#4119](googleapis/sdk-platform-java#4119))
([8a0c565](googleapis/sdk-platform-java@8a0c565))
* Use ServiceName + MethodName as the regex for Otel
([#2543](googleapis/sdk-platform-java#2543))
([8196b8f](googleapis/sdk-platform-java@8196b8f))


### Documentation

* **hermetic_build:** fix config field name in readme
([#4130](googleapis/sdk-platform-java#4130))
([0d98c37](googleapis/sdk-platform-java@0d98c37))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants