-
Notifications
You must be signed in to change notification settings - Fork 102
feat: migrate to OTEL exporter #1788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
08cb1b8
feat: migrate exporter to OTEL
mutianf 26a648a
address comments
mutianf 91fcd80
filter out only bigtable metrics
mutianf 32ac2be
fix test
mutianf af6cbb2
use the bom
mutianf e87cf91
update
mutianf e2b7e58
update
mutianf e8be9c2
update completeResultCode
mutianf cd04252
add a comment
mutianf 2a9a7be
Merge branch 'main' into migrate1
mutianf ee2fe99
address comments
mutianf 3624bcd
Merge branch 'main' into migrate1
mutianf da57c98
address comments
mutianf dfb44a6
update pom
mutianf c3f36ed
small fix
mutianf 1fef2a1
also check timestamp
mutianf faadf77
address comment
mutianf 5a255e8
updates
mutianf 3d0d72f
update
mutianf efda056
do not block on shutdown
mutianf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
.../java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableCloudMonitoringExporter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.google.cloud.bigtable.data.v2.stub.metrics; | ||
|
|
||
| import com.google.api.MonitoredResource; | ||
| import com.google.api.core.ApiFuture; | ||
| import com.google.api.core.ApiFutureCallback; | ||
| import com.google.api.core.ApiFutures; | ||
| import com.google.api.gax.core.FixedCredentialsProvider; | ||
| import com.google.auth.Credentials; | ||
| import com.google.cloud.monitoring.v3.MetricServiceClient; | ||
| import com.google.cloud.monitoring.v3.MetricServiceSettings; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.util.concurrent.MoreExecutors; | ||
| import com.google.monitoring.v3.CreateTimeSeriesRequest; | ||
| import com.google.monitoring.v3.ProjectName; | ||
| import com.google.monitoring.v3.TimeSeries; | ||
| import com.google.protobuf.Empty; | ||
| import io.opentelemetry.sdk.common.CompletableResultCode; | ||
| import io.opentelemetry.sdk.metrics.InstrumentType; | ||
| import io.opentelemetry.sdk.metrics.data.AggregationTemporality; | ||
| import io.opentelemetry.sdk.metrics.data.MetricData; | ||
| import io.opentelemetry.sdk.metrics.export.MetricExporter; | ||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import org.threeten.bp.Duration; | ||
|
|
||
| /** | ||
| * Bigtable Cloud Monitoring OpenTelemetry Exporter. | ||
| * | ||
| * <p>The exporter will look for all bigtable owned metrics under bigtable.googleapis.com | ||
| * instrumentation scope and upload it via the Google Cloud Monitoring API. | ||
| */ | ||
| final class BigtableCloudMonitoringExporter implements MetricExporter { | ||
mutianf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private static final Logger logger = | ||
| Logger.getLogger(BigtableCloudMonitoringExporter.class.getName()); | ||
| private final MetricServiceClient client; | ||
|
|
||
| private final String projectId; | ||
| private final String taskId; | ||
| private final MonitoredResource monitoredResource; | ||
| private final AtomicBoolean isShutdown = new AtomicBoolean(false); | ||
|
|
||
| private static final String RESOURCE_TYPE = "bigtable_client_raw"; | ||
|
|
||
| private CompletableResultCode lastExportCode; | ||
|
|
||
| static BigtableCloudMonitoringExporter create(String projectId, Credentials credentials) | ||
| throws IOException { | ||
| MetricServiceSettings.Builder settingsBuilder = MetricServiceSettings.newBuilder(); | ||
| settingsBuilder.setCredentialsProvider(FixedCredentialsProvider.create(credentials)); | ||
|
|
||
| org.threeten.bp.Duration timeout = Duration.ofMinutes(1); | ||
| // TODO: createServiceTimeSeries needs special handling if the request failed. Leaving | ||
| // it as not retried for now. | ||
| settingsBuilder.createServiceTimeSeriesSettings().setSimpleTimeoutNoRetries(timeout); | ||
mutianf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return new BigtableCloudMonitoringExporter( | ||
| projectId, | ||
| MetricServiceClient.create(settingsBuilder.build()), | ||
| MonitoredResource.newBuilder().setType(RESOURCE_TYPE).build(), | ||
| BigtableExporterUtils.getDefaultTaskValue()); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| BigtableCloudMonitoringExporter( | ||
| String projectId, | ||
| MetricServiceClient client, | ||
| MonitoredResource monitoredResource, | ||
| String taskId) { | ||
| this.client = client; | ||
| this.monitoredResource = monitoredResource; | ||
| this.taskId = taskId; | ||
| this.projectId = projectId; | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableResultCode export(Collection<MetricData> collection) { | ||
| if (isShutdown.get()) { | ||
| logger.log(Level.WARNING, "Exporter is shutting down"); | ||
| return CompletableResultCode.ofFailure(); | ||
mutianf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| if (!collection.stream() | ||
| .flatMap(metricData -> metricData.getData().getPoints().stream()) | ||
| .allMatch(pd -> projectId.equals(BigtableExporterUtils.getProjectId(pd)))) { | ||
| logger.log(Level.WARNING, "Metric data has different a projectId. Skip exporting."); | ||
| return CompletableResultCode.ofFailure(); | ||
| } | ||
|
|
||
| List<TimeSeries> allTimeSeries; | ||
| try { | ||
| allTimeSeries = | ||
| BigtableExporterUtils.convertCollectionToListOfTimeSeries( | ||
| collection, taskId, monitoredResource); | ||
| } catch (Throwable e) { | ||
| logger.log(Level.WARNING, "Failed to convert metric data to cloud monitoring timeseries.", e); | ||
| return CompletableResultCode.ofFailure(); | ||
| } | ||
|
|
||
| ProjectName projectName = ProjectName.of(projectId); | ||
| CreateTimeSeriesRequest request = | ||
| CreateTimeSeriesRequest.newBuilder() | ||
| .setName(projectName.toString()) | ||
| .addAllTimeSeries(allTimeSeries) | ||
| .build(); | ||
|
|
||
| ApiFuture<Empty> future = this.client.createServiceTimeSeriesCallable().futureCall(request); | ||
|
|
||
| lastExportCode = new CompletableResultCode(); | ||
|
|
||
| ApiFutures.addCallback( | ||
| future, | ||
| new ApiFutureCallback<Empty>() { | ||
| @Override | ||
| public void onFailure(Throwable throwable) { | ||
| logger.log(Level.WARNING, "createServiceTimeSeries request failed. ", throwable); | ||
| lastExportCode.fail(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onSuccess(Empty empty) { | ||
| lastExportCode.succeed(); | ||
| } | ||
| }, | ||
| MoreExecutors.directExecutor()); | ||
|
|
||
| return lastExportCode; | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableResultCode flush() { | ||
| if (lastExportCode != null) { | ||
| return lastExportCode; | ||
| } | ||
| return CompletableResultCode.ofSuccess(); | ||
mutianf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public CompletableResultCode shutdown() { | ||
mutianf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!isShutdown.compareAndSet(false, true)) { | ||
| logger.log(Level.WARNING, "shutdown is called multiple times"); | ||
| return CompletableResultCode.ofSuccess(); | ||
| } | ||
| CompletableResultCode flushResult = flush(); | ||
| CompletableResultCode shutdownResult = new CompletableResultCode(); | ||
| flushResult.whenComplete( | ||
| () -> { | ||
| Throwable throwable = null; | ||
| try { | ||
| client.shutdown(); | ||
| } catch (Throwable e) { | ||
| logger.log(Level.WARNING, "failed to shutdown the monitoring client", e); | ||
| throwable = e; | ||
| } | ||
| if (throwable != null) { | ||
| shutdownResult.fail(); | ||
| } else { | ||
| shutdownResult.succeed(); | ||
| } | ||
| }); | ||
| return CompletableResultCode.ofAll(Arrays.asList(flushResult, shutdownResult)); | ||
| } | ||
|
|
||
| /** | ||
| * For Google Cloud Monitoring always return CUMULATIVE to keep track of the cumulative value of a | ||
| * metric over time. | ||
| */ | ||
| @Override | ||
| public AggregationTemporality getAggregationTemporality(InstrumentType instrumentType) { | ||
| return AggregationTemporality.CUMULATIVE; | ||
mutianf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.