Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,22 @@

import com.google.api.client.http.HttpTransport;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.rpc.StatusCode;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.api.gax.rpc.UnavailableException;
import com.google.api.gax.tracing.GoldenSignalsMetricsTracerFactory;
import com.google.api.gax.tracing.ObservabilityAttributes;
import com.google.common.collect.ImmutableList;
import com.google.rpc.Status;
import com.google.showcase.v1beta1.EchoClient;
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.EchoStubSettings;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.MethodDescriptor;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
Expand Down Expand Up @@ -103,7 +100,8 @@ void testMetrics_successfulEcho_grpc() throws Exception {

// The end of an operation is tracked in a separate thread.
// Add a small sleep to make sure the tracking is completed.
// This is implemented by adding a TraceFinisher to ApiFuture as a callback in TracedUnaryCallable,
// This is implemented by adding a TraceFinisher to ApiFuture as a callback in
// TracedUnaryCallable,
// which could be executed in a different thread.
Thread.sleep(100);
Collection<MetricData> metrics = metricReader.collectAllMetrics();
Expand All @@ -122,22 +120,27 @@ void testMetrics_successfulEcho_grpc() throws Exception {
durationMetric.getHistogramData().getPoints().iterator().next().getAttributes();

assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.SERVER_ADDRESS_ATTRIBUTE)))
attributes.get(
AttributeKey.stringKey(ObservabilityAttributes.SERVER_ADDRESS_ATTRIBUTE)))
.isEqualTo(SHOWCASE_SERVER_ADDRESS);
assertThat(
attributes.get(AttributeKey.longKey(ObservabilityAttributes.SERVER_PORT_ATTRIBUTE)))
.isEqualTo(SHOWCASE_SERVER_PORT);
assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.RPC_SYSTEM_NAME_ATTRIBUTE)))
attributes.get(
AttributeKey.stringKey(ObservabilityAttributes.RPC_SYSTEM_NAME_ATTRIBUTE)))
.isEqualTo("grpc");
assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.GCP_CLIENT_SERVICE_ATTRIBUTE)))
attributes.get(
AttributeKey.stringKey(ObservabilityAttributes.GCP_CLIENT_SERVICE_ATTRIBUTE)))
.isEqualTo("showcase");
assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.GRPC_RPC_METHOD_ATTRIBUTE)))
attributes.get(
AttributeKey.stringKey(ObservabilityAttributes.GRPC_RPC_METHOD_ATTRIBUTE)))
.isEqualTo("google.showcase.v1beta1.Echo/Echo");
assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE)))
attributes.get(
AttributeKey.stringKey(ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE)))
.isEqualTo("OK");
}
}
Expand Down Expand Up @@ -201,7 +204,8 @@ public void sendMessage(ReqT message) {}
durationMetric.getHistogramData().getPoints().iterator().next().getAttributes();

assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE)))
attributes.get(
AttributeKey.stringKey(ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE)))
.isEqualTo("UNAVAILABLE");
assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE)))
Expand Down Expand Up @@ -236,29 +240,36 @@ void testMetrics_successfulEcho_httpjson() throws Exception {
durationMetric.getHistogramData().getPoints().iterator().next().getAttributes();

assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.SERVER_ADDRESS_ATTRIBUTE)))
attributes.get(
AttributeKey.stringKey(ObservabilityAttributes.SERVER_ADDRESS_ATTRIBUTE)))
.isEqualTo(SHOWCASE_SERVER_ADDRESS);
assertThat(
attributes.get(AttributeKey.longKey(ObservabilityAttributes.SERVER_PORT_ATTRIBUTE)))
.isEqualTo(SHOWCASE_SERVER_PORT);
assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.RPC_SYSTEM_NAME_ATTRIBUTE)))
attributes.get(
AttributeKey.stringKey(ObservabilityAttributes.RPC_SYSTEM_NAME_ATTRIBUTE)))
.isEqualTo("http");
assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.GCP_CLIENT_SERVICE_ATTRIBUTE)))
attributes.get(
AttributeKey.stringKey(ObservabilityAttributes.GCP_CLIENT_SERVICE_ATTRIBUTE)))
.isEqualTo("showcase");
assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE)))
attributes.get(
AttributeKey.stringKey(ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE)))
.isEqualTo("OK");
assertThat(
attributes.get(AttributeKey.longKey(ObservabilityAttributes.HTTP_RESPONSE_STATUS_ATTRIBUTE)))
attributes.get(
AttributeKey.longKey(ObservabilityAttributes.HTTP_RESPONSE_STATUS_ATTRIBUTE)))
.isEqualTo(200L);
assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.URL_TEMPLATE_ATTRIBUTE)))
attributes.get(
AttributeKey.stringKey(ObservabilityAttributes.URL_TEMPLATE_ATTRIBUTE)))
.isEqualTo("v1beta1/echo:echo");
assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.GRPC_RPC_METHOD_ATTRIBUTE)))
.isEqualTo("google.showcase.v1beta1.Echo/Echo");
attributes.get(
AttributeKey.stringKey(ObservabilityAttributes.GRPC_RPC_METHOD_ATTRIBUTE)))
.isEqualTo("google.showcase.v1beta1.Echo/Echo");
}
}

Expand Down Expand Up @@ -369,10 +380,12 @@ public String getHeaderValue(int index) {
durationMetric.getHistogramData().getPoints().iterator().next().getAttributes();

assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE)))
attributes.get(
AttributeKey.stringKey(ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE)))
.isEqualTo("UNAVAILABLE");
assertThat(
attributes.get(AttributeKey.longKey(ObservabilityAttributes.HTTP_RESPONSE_STATUS_ATTRIBUTE)))
attributes.get(
AttributeKey.longKey(ObservabilityAttributes.HTTP_RESPONSE_STATUS_ATTRIBUTE)))
.isEqualTo(503L);
assertThat(
attributes.get(AttributeKey.stringKey(ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@
import com.google.showcase.v1beta1.EchoSettings;
import com.google.showcase.v1beta1.GetUserRequest;
import com.google.showcase.v1beta1.IdentityClient;
import com.google.showcase.v1beta1.IdentitySettings;
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import com.google.showcase.v1beta1.stub.EchoStub;
import com.google.showcase.v1beta1.stub.EchoStubSettings;
import com.google.showcase.v1beta1.stub.IdentityStub;
import com.google.showcase.v1beta1.stub.IdentityStubSettings;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
Expand Down Expand Up @@ -115,7 +118,7 @@ class ITOtelTracing {
private static final String VALUE_GRPC = "grpc";
private static final String VALUE_HTTP = "http";
private static final String VALUE_OK = "OK";
private static final String VALUE_TEST_USER = "users/test-user";
private static final String VALUE_TEST_USER = "//showcase.googleapis.com/users/test-user";
private static final String VALUE_UNAVAILABLE = "UNAVAILABLE";
private static final String VALUE_UNAVAILABLE_EXCEPTION = "UnavailableException";
private static final String VALUE_SERVICE_UNAVAILABLE = "Service Unavailable";
Expand Down Expand Up @@ -316,8 +319,10 @@ private long computeExpectedHttpJsonResponseSize(Message message)
void testTracing_successfulIdentityGetUser_grpc() throws Exception {
SpanTracerFactory tracingFactory = new SpanTracerFactory(openTelemetrySdk);

try (IdentityClient client =
TestClientInitializer.createGrpcIdentityClientOpentelemetry(tracingFactory)) {
IdentitySettings grpcIdentitySettings = createIdentitySettings(false);
IdentityStub stub = createIdentityStubWithServiceName(grpcIdentitySettings, tracingFactory);

try (IdentityClient client = IdentityClient.create(stub)) {

try {
client.getUser(GetUserRequest.newBuilder().setName("users/test-user").build());
Expand All @@ -343,8 +348,10 @@ void testTracing_successfulIdentityGetUser_grpc() throws Exception {
void testTracing_successfulIdentityGetUser_httpjson() throws Exception {
SpanTracerFactory tracingFactory = new SpanTracerFactory(openTelemetrySdk);

try (IdentityClient client =
TestClientInitializer.createHttpJsonIdentityClientOpentelemetry(tracingFactory)) {
IdentitySettings httpJsonIdentitySettings = createIdentitySettings(true);
IdentityStub stub = createIdentityStubWithServiceName(httpJsonIdentitySettings, tracingFactory);

try (IdentityClient client = IdentityClient.create(stub)) {

try {
client.getUser(GetUserRequest.newBuilder().setName("users/test-user").build());
Expand Down Expand Up @@ -797,6 +804,37 @@ private EchoStub createStubWithServiceName(
return new ExtendedEchoStubSettings(builder).createStub();
}

private IdentityStub createIdentityStubWithServiceName(
IdentitySettings settings, SpanTracerFactory tracingFactory) throws IOException {
IdentityStubSettings.Builder builder =
(IdentityStubSettings.Builder) settings.getStubSettings().toBuilder();
builder.setTracerFactory(tracingFactory);
return new ExtendedIdentityStubSettings(builder).createStub();
}

private IdentitySettings createIdentitySettings(boolean isHttpJson) throws Exception {
if (isHttpJson) {
return IdentitySettings.newHttpJsonBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
IdentitySettings.defaultHttpJsonTransportProviderBuilder()
.setHttpTransport(
new NetHttpTransport.Builder().doNotValidateCertificate().build())
.build())
.setEndpoint(SHOWCASE_HTTPJSON_ENDPOINT)
.build();
} else {
return IdentitySettings.newBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
IdentitySettings.defaultGrpcTransportProviderBuilder()
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
.build())
.setEndpoint(SHOWCASE_GRPC_ENDPOINT)
.build();
}
}

/** Custom wrapper to set a service name for showcase clients, which lack one by default. */
private static class ExtendedEchoStubSettings extends EchoStubSettings {
protected ExtendedEchoStubSettings(EchoStubSettings.Builder builder) throws IOException {
Expand All @@ -808,4 +846,16 @@ public String getServiceName() {
return "showcase";
}
}

private static class ExtendedIdentityStubSettings extends IdentityStubSettings {
protected ExtendedIdentityStubSettings(IdentityStubSettings.Builder builder)
throws IOException {
super(builder);
}

@Override
public String getServiceName() {
return "showcase";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.google.showcase.v1beta1.stub.EchoStubSettings;
import io.grpc.ClientInterceptor;
import io.grpc.ManagedChannelBuilder;

import java.io.IOException;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -371,9 +370,9 @@ public static IdentityClient createHttpJsonIdentityClientOpentelemetry(
}

private static EchoStub createStubWithServiceName(
EchoSettings settings, ApiTracerFactory tracingFactory) throws IOException {
EchoSettings settings, ApiTracerFactory tracingFactory) throws IOException {
EchoStubSettings.Builder builder =
(EchoStubSettings.Builder) settings.getStubSettings().toBuilder();
(EchoStubSettings.Builder) settings.getStubSettings().toBuilder();
builder.setTracerFactory(tracingFactory);
return new ExtendedEchoStubSettings(builder).createStub();
}
Expand Down
Loading