remove unregistering stackdriver exporters (#9442)

This commit is contained in:
DNVindhya 2022-08-12 12:20:43 -07:00 committed by GitHub
parent 7169b174e6
commit 03430c786a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 1 additions and 30 deletions

View File

@ -50,14 +50,11 @@ import io.opencensus.trace.config.TraceConfig;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map; import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** The main class for gRPC Google Cloud Platform Observability features. */ /** The main class for gRPC Google Cloud Platform Observability features. */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/8869") @ExperimentalApi("https://github.com/grpc/grpc-java/issues/8869")
public final class GcpObservability implements AutoCloseable { public final class GcpObservability implements AutoCloseable {
private static final Logger logger = Logger.getLogger(GcpObservability.class.getName());
private static final int METRICS_EXPORT_INTERVAL = 30; private static final int METRICS_EXPORT_INTERVAL = 30;
private static final ImmutableSet<String> SERVICES_TO_EXCLUDE = ImmutableSet.of( private static final ImmutableSet<String> SERVICES_TO_EXCLUDE = ImmutableSet.of(
"google.logging.v2.LoggingServiceV2", "google.monitoring.v3.MetricService", "google.logging.v2.LoggingServiceV2", "google.monitoring.v3.MetricService",
@ -68,8 +65,6 @@ public final class GcpObservability implements AutoCloseable {
private final ArrayList<ClientInterceptor> clientInterceptors = new ArrayList<>(); private final ArrayList<ClientInterceptor> clientInterceptors = new ArrayList<>();
private final ArrayList<ServerInterceptor> serverInterceptors = new ArrayList<>(); private final ArrayList<ServerInterceptor> serverInterceptors = new ArrayList<>();
private final ArrayList<ServerStreamTracer.Factory> tracerFactories = new ArrayList<>(); private final ArrayList<ServerStreamTracer.Factory> tracerFactories = new ArrayList<>();
private boolean metricsEnabled;
private boolean tracesEnabled;
/** /**
* Initialize grpc-observability. * Initialize grpc-observability.
@ -115,7 +110,6 @@ public final class GcpObservability implements AutoCloseable {
if (instance == null) { if (instance == null) {
throw new IllegalStateException("GcpObservability already closed!"); throw new IllegalStateException("GcpObservability already closed!");
} }
unRegisterStackDriverExporter();
sink.close(); sink.close();
instance = null; instance = null;
} }
@ -169,7 +163,6 @@ public final class GcpObservability implements AutoCloseable {
} }
statsConfigurationBuilder.setExportInterval(Duration.create(METRICS_EXPORT_INTERVAL, 0)); statsConfigurationBuilder.setExportInterval(Duration.create(METRICS_EXPORT_INTERVAL, 0));
StackdriverStatsExporter.createAndRegister(statsConfigurationBuilder.build()); StackdriverStatsExporter.createAndRegister(statsConfigurationBuilder.build());
metricsEnabled = true;
} }
if (config.isEnableCloudTracing()) { if (config.isEnableCloudTracing()) {
@ -188,29 +181,6 @@ public final class GcpObservability implements AutoCloseable {
traceConfigurationBuilder.setFixedAttributes(fixedAttributes); traceConfigurationBuilder.setFixedAttributes(fixedAttributes);
} }
StackdriverTraceExporter.createAndRegister(traceConfigurationBuilder.build()); StackdriverTraceExporter.createAndRegister(traceConfigurationBuilder.build());
tracesEnabled = true;
}
}
private void unRegisterStackDriverExporter() {
if (metricsEnabled) {
try {
StackdriverStatsExporter.unregister();
} catch (IllegalStateException e) {
logger.log(
Level.SEVERE, "Failed to unregister Stackdriver stats exporter, " + e.getMessage());
}
metricsEnabled = false;
}
if (tracesEnabled) {
try {
StackdriverTraceExporter.unregister();
} catch (IllegalStateException e) {
logger.log(
Level.SEVERE, "Failed to unregister Stackdriver trace exporter, " + e.getMessage());
}
tracesEnabled = false;
} }
} }

View File

@ -148,6 +148,7 @@ public class GcpLogSink implements Sink {
ImmutableMap.Builder<String, String> tagsBuilder = ImmutableMap.builder(); ImmutableMap.Builder<String, String> tagsBuilder = ImmutableMap.builder();
String sourceProjectId = locationTags.get("project_id"); String sourceProjectId = locationTags.get("project_id");
if (!Strings.isNullOrEmpty(destinationProjectId) if (!Strings.isNullOrEmpty(destinationProjectId)
&& !Strings.isNullOrEmpty(sourceProjectId)
&& !Objects.equals(sourceProjectId, destinationProjectId)) { && !Objects.equals(sourceProjectId, destinationProjectId)) {
tagsBuilder.put("source_project_id", sourceProjectId); tagsBuilder.put("source_project_id", sourceProjectId);
} }