Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,19 @@ public static ClientContext create(StubSettings settings) throws IOException {
if (watchdogProvider != null && watchdogProvider.shouldAutoClose()) {
backgroundResources.add(watchdog);
}
String serviceName = endpointContext.serviceName();
Comment thread
diegomarquezp marked this conversation as resolved.
Outdated
String universeDomain = endpointContext.resolvedUniverseDomain();
String urlDomain = null;
if (!Strings.isNullOrEmpty(serviceName) && !Strings.isNullOrEmpty(universeDomain)) {
urlDomain = serviceName + "." + universeDomain;
}

ApiTracerContext apiTracerContext =
ApiTracerContext.newBuilder()
.setServerAddress(endpointContext.resolvedServerAddress())
.setServerPort(endpointContext.resolvedServerPort())
.setLibraryMetadata(settings.getLibraryMetadata())
.setUrlDomain(urlDomain)
.build();
ApiTracerFactory apiTracerFactory = settings.getTracerFactory();
if (apiTracerFactory instanceof SpanTracerFactory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ public Map<String, Object> getAttemptAttributes() {
attributes.put(
ObservabilityAttributes.DESTINATION_RESOURCE_ID_ATTRIBUTE, destinationResourceId());
}
if (!Strings.isNullOrEmpty(urlDomain())) {
attributes.put(ObservabilityAttributes.URL_DOMAIN_ATTRIBUTE, urlDomain());
}
return attributes;
}

Expand All @@ -232,10 +235,10 @@ Map<String, Object> getMetricsAttributes() {
if (!Strings.isNullOrEmpty(fullMethodName())) {
attributes.put(ObservabilityAttributes.GRPC_RPC_METHOD_ATTRIBUTE, fullMethodName());
}
if (!Strings.isNullOrEmpty(urlDomain())) {
attributes.put(ObservabilityAttributes.URL_DOMAIN_ATTRIBUTE, urlDomain());
}
if (transport() == Transport.HTTP) {
if (!Strings.isNullOrEmpty(urlDomain())) {
attributes.put(ObservabilityAttributes.URL_DOMAIN_ATTRIBUTE, urlDomain());
}
if (!Strings.isNullOrEmpty(httpPathTemplate())) {
attributes.put(ObservabilityAttributes.URL_TEMPLATE_ATTRIBUTE, httpPathTemplate());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ void testGetAttemptAttributes_emptyStrings() {
assertThat(attributes).isEmpty();
}

@Test
void testGetAttemptAttributes_urlDomain() {
ApiTracerContext context =
ApiTracerContext.newBuilder()
.setLibraryMetadata(LibraryMetadata.empty())
.setUrlDomain("test-domain.com")
.build();
Map<String, Object> attributes = context.getAttemptAttributes();

assertThat(attributes)
.containsEntry(ObservabilityAttributes.URL_DOMAIN_ATTRIBUTE, "test-domain.com");
}

@Test
void testGetMetricsAttributes_serverPort() {
ApiTracerContext context =
Expand Down Expand Up @@ -304,7 +317,8 @@ void testGetMetricsAttributes_urlDomain_notHttp() {
.build();
Map<String, Object> attributes = context.getMetricsAttributes();

assertThat(attributes).doesNotContainKey(ObservabilityAttributes.URL_DOMAIN_ATTRIBUTE);
assertThat(attributes)
.containsEntry(ObservabilityAttributes.URL_DOMAIN_ATTRIBUTE, "test-domain.com");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
import com.google.showcase.v1beta1.EchoRequest;
import com.google.showcase.v1beta1.EchoResponse;
import com.google.showcase.v1beta1.EchoSettings;
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import com.google.showcase.v1beta1.stub.EchoStub;
import com.google.showcase.v1beta1.stub.EchoStubSettings;
import io.grpc.ManagedChannelBuilder;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.SpanKind;
Expand Down Expand Up @@ -100,8 +100,29 @@ void tearDown() {
void testTracing_successfulEcho_grpc() throws Exception {
SpanTracerFactory tracingFactory = new SpanTracerFactory(openTelemetrySdk);

try (EchoClient client =
TestClientInitializer.createGrpcEchoClientOpentelemetry(tracingFactory)) {
EchoSettings grpcEchoSettings =
EchoSettings.newBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
EchoSettings.defaultGrpcTransportProviderBuilder()
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
.build())
.setEndpoint("localhost:7469")
.build();

EchoStubSettings.Builder stubSettingsBuilder =
(EchoStubSettings.Builder) grpcEchoSettings.getStubSettings().toBuilder();
stubSettingsBuilder.setTracerFactory(tracingFactory);

EchoStubSettings stubSettings =
new EchoStubSettings(stubSettingsBuilder) {
@Override
public String getServiceName() {
return "showcase";
}
};

try (EchoClient client = EchoClient.create(stubSettings.createStub())) {

client.echo(EchoRequest.newBuilder().setContent("tracing-test").build());

Expand Down Expand Up @@ -149,17 +170,43 @@ void testTracing_successfulEcho_grpc() throws Exception {
.getAttributes()
.get(AttributeKey.stringKey(ObservabilityAttributes.GRPC_RPC_METHOD_ATTRIBUTE)))
.isEqualTo("google.showcase.v1beta1.Echo/Echo");
assertThat(
attemptSpan
.getAttributes()
.get(AttributeKey.stringKey(ObservabilityAttributes.URL_DOMAIN_ATTRIBUTE)))
.isEqualTo("showcase.googleapis.com");
assertThat(attemptSpan.getInstrumentationScopeInfo().getName()).isEqualTo(SHOWCASE_ARTIFACT);
// {x-version-update-end}
}
}

@Test
void testTracing_successfulEcho_httpjson() throws Exception {
SpanTracerFactory tracingFactory = new SpanTracerFactory(openTelemetrySdk);

try (EchoClient client =
TestClientInitializer.createHttpJsonEchoClientOpentelemetry(tracingFactory)) {
EchoSettings httpJsonEchoSettings =
EchoSettings.newHttpJsonBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
EchoSettings.defaultHttpJsonTransportProviderBuilder()
.setHttpTransport(
new NetHttpTransport.Builder().doNotValidateCertificate().build())
.setEndpoint("http://localhost:7469")
.build())
.build();

EchoStubSettings.Builder stubSettingsBuilder =
(EchoStubSettings.Builder) httpJsonEchoSettings.getStubSettings().toBuilder();
stubSettingsBuilder.setTracerFactory(tracingFactory);

EchoStubSettings stubSettings =
new EchoStubSettings(stubSettingsBuilder) {
@Override
public String getServiceName() {
return "showcase";
}
};

try (EchoClient client = EchoClient.create(stubSettings.createStub())) {

client.echo(EchoRequest.newBuilder().setContent("tracing-test").build());

Expand Down Expand Up @@ -208,6 +255,11 @@ void testTracing_successfulEcho_httpjson() throws Exception {
.getAttributes()
.get(AttributeKey.stringKey(ObservabilityAttributes.HTTP_URL_TEMPLATE_ATTRIBUTE)))
.isEqualTo("v1beta1/echo:echo");
assertThat(
attemptSpan
.getAttributes()
.get(AttributeKey.stringKey(ObservabilityAttributes.URL_DOMAIN_ATTRIBUTE)))
.isEqualTo("showcase.googleapis.com");
assertThat(
attemptSpan
.getAttributes()
Expand Down
Loading