observability: change the config parsing to not require logging_config (#9023)

and make Observability implement AutoCloseable
This commit is contained in:
sanjaypujare 2022-03-28 16:20:26 -07:00 committed by GitHub
parent 4a84c6fa96
commit 9b1023bafe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 24 deletions

View File

@ -29,7 +29,7 @@ import java.io.IOException;
/** The main class for gRPC Observability features. */ /** The main class for gRPC Observability features. */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/8869") @ExperimentalApi("https://github.com/grpc/grpc-java/issues/8869")
public final class Observability { public final class Observability implements AutoCloseable {
private static Observability instance = null; private static Observability instance = null;
private final Sink sink; private final Sink sink;
@ -64,10 +64,11 @@ public final class Observability {
} }
/** Un-initialize/shutdown grpc-observability. */ /** Un-initialize/shutdown grpc-observability. */
public void grpcShutdown() { @Override
public void close() {
synchronized (Observability.class) { synchronized (Observability.class) {
if (instance == null) { if (instance == null) {
throw new IllegalStateException("Observability already shutdown!"); throw new IllegalStateException("Observability already closed!");
} }
LoggingChannelProvider.shutdown(); LoggingChannelProvider.shutdown();
LoggingServerProvider.shutdown(); LoggingServerProvider.shutdown();

View File

@ -43,8 +43,7 @@ final class ObservabilityConfigImpl implements ObservabilityConfig {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
void parse(String config) throws IOException { void parse(String config) throws IOException {
checkArgument(config != null, CONFIG_ENV_VAR_NAME + " value is null!"); checkArgument(config != null, CONFIG_ENV_VAR_NAME + " value is null!");
parseLoggingConfig( parseLoggingConfig((Map<String, ?>) JsonParser.parse(config));
JsonUtil.getObject((Map<String, ?>) JsonParser.parse(config), "logging_config"));
} }
private void parseLoggingConfig(Map<String,?> loggingConfig) { private void parseLoggingConfig(Map<String,?> loggingConfig) {

View File

@ -31,15 +31,12 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class ObservabilityConfigImplTest { public class ObservabilityConfigImplTest {
private static final String EVENT_TYPES = "{\n" private static final String EVENT_TYPES = "{\n"
+ " \"logging_config\": {\n"
+ " \"enable_cloud_logging\": false,\n" + " \"enable_cloud_logging\": false,\n"
+ " \"event_types\": " + " \"event_types\": "
+ "[\"GRPC_CALL_REQUEST_HEADER\", \"GRPC_CALL_HALF_CLOSE\", \"GRPC_CALL_TRAILER\"]\n" + "[\"GRPC_CALL_REQUEST_HEADER\", \"GRPC_CALL_HALF_CLOSE\", \"GRPC_CALL_TRAILER\"]\n"
+ " }\n"
+ "}"; + "}";
private static final String LOG_FILTERS = "{\n" private static final String LOG_FILTERS = "{\n"
+ " \"logging_config\": {\n"
+ " \"enable_cloud_logging\": true,\n" + " \"enable_cloud_logging\": true,\n"
+ " \"destination_project_id\": \"grpc-testing\",\n" + " \"destination_project_id\": \"grpc-testing\",\n"
+ " \"log_filters\": [{\n" + " \"log_filters\": [{\n"
@ -51,19 +48,15 @@ public class ObservabilityConfigImplTest {
+ " \"pattern\": \"service1/Method2\"\n" + " \"pattern\": \"service1/Method2\"\n"
+ " }" + " }"
+ " ]\n" + " ]\n"
+ " }\n"
+ "}"; + "}";
private static final String DEST_PROJECT_ID = "{\n" private static final String DEST_PROJECT_ID = "{\n"
+ " \"logging_config\": {\n"
+ " \"enable_cloud_logging\": true,\n" + " \"enable_cloud_logging\": true,\n"
+ " \"destination_project_id\": \"grpc-testing\"\n" + " \"destination_project_id\": \"grpc-testing\"\n"
+ " }\n"
+ "}"; + "}";
private static final String DISABLE_CLOUD_LOGGING = "{\n" private static final String DISABLE_CLOUD_LOGGING = "{\n"
+ " \"logging_config\": {\n" + " \"enable_cloud_logging\": false\n"
+ " \"enable_cloud_logging\": false\n" + " }\n"
+ "}"; + "}";
ObservabilityConfigImpl observabilityConfig = new ObservabilityConfigImpl(); ObservabilityConfigImpl observabilityConfig = new ObservabilityConfigImpl();

View File

@ -42,23 +42,24 @@ public class ObservabilityTest {
InternalLoggingChannelInterceptor.Factory.class); InternalLoggingChannelInterceptor.Factory.class);
InternalLoggingServerInterceptor.Factory serverInterceptorFactory = mock( InternalLoggingServerInterceptor.Factory serverInterceptorFactory = mock(
InternalLoggingServerInterceptor.Factory.class); InternalLoggingServerInterceptor.Factory.class);
Observability observability = Observability.grpcInit(sink, channelInterceptorFactory, Observability observability1;
serverInterceptorFactory); try (Observability observability = Observability.grpcInit(sink, channelInterceptorFactory,
assertThat(ManagedChannelProvider.provider()).isInstanceOf(LoggingChannelProvider.class); serverInterceptorFactory)) {
assertThat(ServerProvider.provider()).isInstanceOf(ServerProvider.class); assertThat(ManagedChannelProvider.provider()).isInstanceOf(LoggingChannelProvider.class);
Observability observability1 = Observability.grpcInit(sink, channelInterceptorFactory, assertThat(ServerProvider.provider()).isInstanceOf(ServerProvider.class);
serverInterceptorFactory); observability1 = Observability.grpcInit(sink, channelInterceptorFactory,
assertThat(observability1).isSameInstanceAs(observability); serverInterceptorFactory);
assertThat(observability1).isSameInstanceAs(observability);
observability.grpcShutdown(); }
verify(sink).close(); verify(sink).close();
assertThat(ManagedChannelProvider.provider()).isSameInstanceAs(prevChannelProvider); assertThat(ManagedChannelProvider.provider()).isSameInstanceAs(prevChannelProvider);
assertThat(ServerProvider.provider()).isSameInstanceAs(prevServerProvider); assertThat(ServerProvider.provider()).isSameInstanceAs(prevServerProvider);
try { try {
observability.grpcShutdown(); observability1.close();
fail("should have failed for calling grpcShutdown() second time"); fail("should have failed for calling close() second time");
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
assertThat(e).hasMessageThat().contains("Observability already shutdown!"); assertThat(e).hasMessageThat().contains("Observability already closed!");
} }
} }
} }