Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d32fbb7

Browse files
authoredApr 12, 2024
fix: Fix export to log detect resource errors (#2197)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/java-bigtable/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) - [ ] Rollback plan is reviewed and LGTMed - [ ] All new data plane features have a completed end to end testing plan Fixes #<issue_number_goes_here> ☕️ If you write sample code, please follow the [samples format]( https://togithub.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md).
1 parent 1682939 commit d32fbb7

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed
 

‎google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@
149149
import java.util.List;
150150
import java.util.Map;
151151
import java.util.concurrent.TimeUnit;
152+
import java.util.logging.Level;
153+
import java.util.logging.Logger;
152154
import javax.annotation.Nonnull;
153155
import javax.annotation.Nullable;
154156

@@ -166,6 +168,9 @@
166168
*/
167169
@InternalApi
168170
public class EnhancedBigtableStub implements AutoCloseable {
171+
172+
private static final Logger logger = Logger.getLogger(EnhancedBigtableStub.class.getName());
173+
169174
private static final String CLIENT_NAME = "Bigtable";
170175
private static final long FLOW_CONTROL_ADJUSTING_INTERVAL_MS = TimeUnit.SECONDS.toMillis(20);
171176
private final EnhancedBigtableStubSettings settings;
@@ -238,8 +243,15 @@ public static ClientContext createClientContext(EnhancedBigtableStubSettings set
238243
? ((InstantiatingGrpcChannelProvider) builder.getTransportChannelProvider()).toBuilder()
239244
: null;
240245

241-
OpenTelemetry openTelemetry =
242-
getOpenTelemetry(settings.getProjectId(), settings.getMetricsProvider(), credentials);
246+
OpenTelemetry openTelemetry = null;
247+
try {
248+
// We don't want client side metrics to crash the client, so catch any exception when getting
249+
// the OTEL instance and log the exception instead.
250+
openTelemetry =
251+
getOpenTelemetry(settings.getProjectId(), settings.getMetricsProvider(), credentials);
252+
} catch (Throwable t) {
253+
logger.log(Level.WARNING, "Failed to get OTEL, will skip exporting client side metrics", t);
254+
}
243255
ErrorCountPerConnectionMetricTracker errorCountPerConnectionMetricTracker;
244256
// Skip setting up ErrorCountPerConnectionMetricTracker if openTelemetry is null
245257
if (openTelemetry != null && transportProvider != null) {
@@ -291,7 +303,8 @@ public static ClientContext createClientContext(EnhancedBigtableStubSettings set
291303
}
292304

293305
public static ApiTracerFactory createBigtableTracerFactory(
294-
EnhancedBigtableStubSettings settings, OpenTelemetry openTelemetry) throws IOException {
306+
EnhancedBigtableStubSettings settings, @Nullable OpenTelemetry openTelemetry)
307+
throws IOException {
295308
return createBigtableTracerFactory(
296309
settings, Tags.getTagger(), Stats.getStatsRecorder(), openTelemetry);
297310
}
@@ -301,7 +314,7 @@ public static ApiTracerFactory createBigtableTracerFactory(
301314
EnhancedBigtableStubSettings settings,
302315
Tagger tagger,
303316
StatsRecorder stats,
304-
OpenTelemetry openTelemetry)
317+
@Nullable OpenTelemetry openTelemetry)
305318
throws IOException {
306319
String projectId = settings.getProjectId();
307320
String instanceId = settings.getInstanceId();

‎google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableExporterUtils.java

+33-13
Original file line numberDiff line numberDiff line change
@@ -151,25 +151,36 @@ static List<TimeSeries> convertToApplicationResourceTimeSeries(
151151
static MonitoredResource detectResource() {
152152
GCPPlatformDetector detector = GCPPlatformDetector.DEFAULT_INSTANCE;
153153
DetectedPlatform detectedPlatform = detector.detectPlatform();
154-
switch (detectedPlatform.getSupportedPlatform()) {
155-
case GOOGLE_COMPUTE_ENGINE:
156-
return createGceMonitoredResource(
157-
detectedPlatform.getProjectId(), detectedPlatform.getAttributes());
158-
case GOOGLE_KUBERNETES_ENGINE:
159-
return createGkeMonitoredResource(
160-
detectedPlatform.getProjectId(), detectedPlatform.getAttributes());
161-
default:
162-
return null;
154+
MonitoredResource monitoredResource = null;
155+
try {
156+
switch (detectedPlatform.getSupportedPlatform()) {
157+
case GOOGLE_COMPUTE_ENGINE:
158+
monitoredResource =
159+
createGceMonitoredResource(
160+
detectedPlatform.getProjectId(), detectedPlatform.getAttributes());
161+
break;
162+
case GOOGLE_KUBERNETES_ENGINE:
163+
monitoredResource =
164+
createGkeMonitoredResource(
165+
detectedPlatform.getProjectId(), detectedPlatform.getAttributes());
166+
break;
167+
}
168+
} catch (IllegalStateException e) {
169+
logger.log(
170+
Level.WARNING,
171+
"Failed to create monitored resource for " + detectedPlatform.getSupportedPlatform(),
172+
e);
163173
}
174+
return monitoredResource;
164175
}
165176

166177
private static MonitoredResource createGceMonitoredResource(
167178
String projectId, Map<String, String> attributes) {
168179
return MonitoredResource.newBuilder()
169180
.setType("gce_instance")
170181
.putLabels("project_id", projectId)
171-
.putLabels("instance_id", attributes.get(AttributeKeys.GCE_INSTANCE_ID))
172-
.putLabels("zone", attributes.get(AttributeKeys.GCE_AVAILABILITY_ZONE))
182+
.putLabels("instance_id", getAttribute(attributes, AttributeKeys.GCE_INSTANCE_ID))
183+
.putLabels("zone", getAttribute(attributes, AttributeKeys.GCE_AVAILABILITY_ZONE))
173184
.build();
174185
}
175186

@@ -178,14 +189,23 @@ private static MonitoredResource createGkeMonitoredResource(
178189
return MonitoredResource.newBuilder()
179190
.setType("k8s_container")
180191
.putLabels("project_id", projectId)
181-
.putLabels("location", attributes.get(AttributeKeys.GKE_CLUSTER_LOCATION))
182-
.putLabels("cluster_name", attributes.get(AttributeKeys.GKE_CLUSTER_NAME))
192+
.putLabels("location", getAttribute(attributes, AttributeKeys.GKE_CLUSTER_LOCATION))
193+
.putLabels("cluster_name", getAttribute(attributes, AttributeKeys.GKE_CLUSTER_NAME))
183194
.putLabels("namespace_name", MoreObjects.firstNonNull(System.getenv("NAMESPACE"), ""))
184195
.putLabels("pod_name", MoreObjects.firstNonNull(System.getenv("HOSTNAME"), ""))
185196
.putLabels("container_name", MoreObjects.firstNonNull(System.getenv("CONTAINER_NAME"), ""))
186197
.build();
187198
}
188199

200+
private static String getAttribute(Map<String, String> attributes, String key) {
201+
String value = attributes.get(key);
202+
if (value == null) {
203+
throw new IllegalStateException(
204+
"Required attribute " + key + " does not exist in the attributes map " + attributes);
205+
}
206+
return value;
207+
}
208+
189209
private static TimeSeries convertPointToBigtableTimeSeries(
190210
MetricData metricData, PointData pointData, String taskId) {
191211
TimeSeries.Builder builder =

0 commit comments

Comments
 (0)
Failed to load comments.