mirror of https://github.com/grpc/grpc-java.git
observability: change the config parsing to not require logging_config (#9023)
and make Observability implement AutoCloseable
This commit is contained in:
parent
4a84c6fa96
commit
9b1023bafe
|
|
@ -29,7 +29,7 @@ import java.io.IOException;
|
|||
|
||||
/** The main class for gRPC Observability features. */
|
||||
@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 final Sink sink;
|
||||
|
||||
|
|
@ -64,10 +64,11 @@ public final class Observability {
|
|||
}
|
||||
|
||||
/** Un-initialize/shutdown grpc-observability. */
|
||||
public void grpcShutdown() {
|
||||
@Override
|
||||
public void close() {
|
||||
synchronized (Observability.class) {
|
||||
if (instance == null) {
|
||||
throw new IllegalStateException("Observability already shutdown!");
|
||||
throw new IllegalStateException("Observability already closed!");
|
||||
}
|
||||
LoggingChannelProvider.shutdown();
|
||||
LoggingServerProvider.shutdown();
|
||||
|
|
|
|||
|
|
@ -43,8 +43,7 @@ final class ObservabilityConfigImpl implements ObservabilityConfig {
|
|||
@SuppressWarnings("unchecked")
|
||||
void parse(String config) throws IOException {
|
||||
checkArgument(config != null, CONFIG_ENV_VAR_NAME + " value is null!");
|
||||
parseLoggingConfig(
|
||||
JsonUtil.getObject((Map<String, ?>) JsonParser.parse(config), "logging_config"));
|
||||
parseLoggingConfig((Map<String, ?>) JsonParser.parse(config));
|
||||
}
|
||||
|
||||
private void parseLoggingConfig(Map<String,?> loggingConfig) {
|
||||
|
|
|
|||
|
|
@ -31,15 +31,12 @@ import org.junit.runners.JUnit4;
|
|||
@RunWith(JUnit4.class)
|
||||
public class ObservabilityConfigImplTest {
|
||||
private static final String EVENT_TYPES = "{\n"
|
||||
+ " \"logging_config\": {\n"
|
||||
+ " \"enable_cloud_logging\": false,\n"
|
||||
+ " \"event_types\": "
|
||||
+ "[\"GRPC_CALL_REQUEST_HEADER\", \"GRPC_CALL_HALF_CLOSE\", \"GRPC_CALL_TRAILER\"]\n"
|
||||
+ " }\n"
|
||||
+ "}";
|
||||
|
||||
private static final String LOG_FILTERS = "{\n"
|
||||
+ " \"logging_config\": {\n"
|
||||
+ " \"enable_cloud_logging\": true,\n"
|
||||
+ " \"destination_project_id\": \"grpc-testing\",\n"
|
||||
+ " \"log_filters\": [{\n"
|
||||
|
|
@ -51,19 +48,15 @@ public class ObservabilityConfigImplTest {
|
|||
+ " \"pattern\": \"service1/Method2\"\n"
|
||||
+ " }"
|
||||
+ " ]\n"
|
||||
+ " }\n"
|
||||
+ "}";
|
||||
|
||||
private static final String DEST_PROJECT_ID = "{\n"
|
||||
+ " \"logging_config\": {\n"
|
||||
+ " \"enable_cloud_logging\": true,\n"
|
||||
+ " \"destination_project_id\": \"grpc-testing\"\n"
|
||||
+ " }\n"
|
||||
+ "}";
|
||||
|
||||
private static final String DISABLE_CLOUD_LOGGING = "{\n"
|
||||
+ " \"logging_config\": {\n"
|
||||
+ " \"enable_cloud_logging\": false\n" + " }\n"
|
||||
+ " \"enable_cloud_logging\": false\n"
|
||||
+ "}";
|
||||
|
||||
ObservabilityConfigImpl observabilityConfig = new ObservabilityConfigImpl();
|
||||
|
|
|
|||
|
|
@ -42,23 +42,24 @@ public class ObservabilityTest {
|
|||
InternalLoggingChannelInterceptor.Factory.class);
|
||||
InternalLoggingServerInterceptor.Factory serverInterceptorFactory = mock(
|
||||
InternalLoggingServerInterceptor.Factory.class);
|
||||
Observability observability = Observability.grpcInit(sink, channelInterceptorFactory,
|
||||
serverInterceptorFactory);
|
||||
assertThat(ManagedChannelProvider.provider()).isInstanceOf(LoggingChannelProvider.class);
|
||||
assertThat(ServerProvider.provider()).isInstanceOf(ServerProvider.class);
|
||||
Observability observability1 = Observability.grpcInit(sink, channelInterceptorFactory,
|
||||
serverInterceptorFactory);
|
||||
assertThat(observability1).isSameInstanceAs(observability);
|
||||
Observability observability1;
|
||||
try (Observability observability = Observability.grpcInit(sink, channelInterceptorFactory,
|
||||
serverInterceptorFactory)) {
|
||||
assertThat(ManagedChannelProvider.provider()).isInstanceOf(LoggingChannelProvider.class);
|
||||
assertThat(ServerProvider.provider()).isInstanceOf(ServerProvider.class);
|
||||
observability1 = Observability.grpcInit(sink, channelInterceptorFactory,
|
||||
serverInterceptorFactory);
|
||||
assertThat(observability1).isSameInstanceAs(observability);
|
||||
|
||||
observability.grpcShutdown();
|
||||
}
|
||||
verify(sink).close();
|
||||
assertThat(ManagedChannelProvider.provider()).isSameInstanceAs(prevChannelProvider);
|
||||
assertThat(ServerProvider.provider()).isSameInstanceAs(prevServerProvider);
|
||||
try {
|
||||
observability.grpcShutdown();
|
||||
fail("should have failed for calling grpcShutdown() second time");
|
||||
observability1.close();
|
||||
fail("should have failed for calling close() second time");
|
||||
} catch (IllegalStateException e) {
|
||||
assertThat(e).hasMessageThat().contains("Observability already shutdown!");
|
||||
assertThat(e).hasMessageThat().contains("Observability already closed!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue