-
Notifications
You must be signed in to change notification settings - Fork 101
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 7 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
131 changes: 131 additions & 0 deletions
131
.../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,131 @@ | ||
| /* | ||
| * 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.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.base.Preconditions; | ||
| import com.google.monitoring.v3.CreateTimeSeriesRequest; | ||
| import com.google.monitoring.v3.ProjectName; | ||
| import com.google.monitoring.v3.TimeSeries; | ||
| 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.Collection; | ||
| import java.util.List; | ||
| import java.util.logging.Logger; | ||
| import java.util.stream.Collectors; | ||
| import org.threeten.bp.Duration; | ||
|
|
||
| /** Bigtable Cloud Monitoring OpenTelemetry Exporter. */ | ||
| 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 boolean isShutdown = false; | ||
|
|
||
| private static final String RESOURCE_TYPE = "bigtable_client_raw"; | ||
|
|
||
| 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); | ||
| 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) { | ||
| return CompletableResultCode.ofFailure(); | ||
| } | ||
| Preconditions.checkArgument( | ||
| collection.stream() | ||
| .flatMap(metricData -> metricData.getData().getPoints().stream()) | ||
| .allMatch(pd -> BigtableExporterUtils.getProjectId(pd).equals(projectId)), | ||
| "Some metric data has unexpected projectId"); | ||
mutianf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (MetricData metricData : collection) { | ||
mutianf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!metricData.getInstrumentationScopeInfo().getName().equals("bigtable.googleapis.com")) { | ||
| continue; | ||
| } | ||
| List<TimeSeries> timeSeries = | ||
| metricData.getData().getPoints().stream() | ||
| .map( | ||
| pointData -> | ||
| BigtableExporterUtils.convertPointToTimeSeries( | ||
| metricData, pointData, taskId, monitoredResource)) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| ProjectName projectName = ProjectName.of(projectId); | ||
| CreateTimeSeriesRequest request = | ||
| CreateTimeSeriesRequest.newBuilder() | ||
| .setName(projectName.toString()) | ||
| .addAllTimeSeries(timeSeries) | ||
| .build(); | ||
|
|
||
| this.client.createServiceTimeSeriesCallable().futureCall(request); | ||
mutianf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return CompletableResultCode.ofSuccess(); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableResultCode flush() { | ||
| 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
|
||
| client.shutdown(); | ||
| isShutdown = true; | ||
| return CompletableResultCode.ofSuccess(); | ||
| } | ||
|
|
||
| @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.