Skip to content
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

feat: migrate to OTEL and enable metrics by default #2166

Merged
merged 35 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
043d551
feat: migrate to OTEL exporter (#1788)
mutianf Jan 23, 2024
beb0219
chore: refactor factory class (#2081)
mutianf Feb 5, 2024
b8fd4d5
chore: refactor TracerFactory creation (#2102)
mutianf Feb 16, 2024
08e1719
feat: migrate built in metrics to OTEL (#1796)
mutianf Feb 21, 2024
5e4eb51
Merge branch 'main' into otel
mutianf Feb 23, 2024
65493e7
merge from main
mutianf Feb 23, 2024
cd3467c
feat: migrate per connection error count metric to otel (#2133)
mutianf Mar 4, 2024
eb46d54
feat: migrate per connection error exporter to otel (#2152)
mutianf Mar 11, 2024
15fb42c
chore: clean up stats package (#2163)
mutianf Mar 12, 2024
9d5eb08
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Mar 12, 2024
b35d956
Merge branch 'main' into otel
mutianf Mar 13, 2024
da09fae
fix clirr and integration test
mutianf Mar 13, 2024
15c28ee
fix clirr and integration test
mutianf Mar 13, 2024
618ba7f
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Mar 14, 2024
f383e33
Merge branch 'otel' of https://github.com/googleapis/java-bigtable in…
gcf-owl-bot[bot] Mar 14, 2024
f0b458b
log more information on test failures
mutianf Mar 15, 2024
f07a64b
Merge branch 'main' into otel
mutianf Mar 15, 2024
452571e
address comments
mutianf Mar 18, 2024
970f4f7
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Mar 18, 2024
29cd9ae
include version
mutianf Mar 19, 2024
98aef25
fix flaky test
mutianf Mar 19, 2024
2e26e20
Merge branch 'main' into otel
mutianf Mar 25, 2024
377d385
remove otel dependencies since they're added to shared dependencies
mutianf Mar 25, 2024
388cd71
Merge branch 'main' into otel
mutianf Mar 28, 2024
b816dda
backport new tests since 2.37
mutianf Mar 28, 2024
21cad39
set feature flag when metrics is enabled
mutianf Mar 28, 2024
1f291fb
record metrics whenever it's registered
mutianf Mar 29, 2024
56d55cb
record metrics whenever it's registered
mutianf Mar 29, 2024
59a05f4
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Mar 29, 2024
b890843
Merge branch 'otel' of https://github.com/googleapis/java-bigtable in…
gcf-owl-bot[bot] Mar 29, 2024
5349c2b
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Mar 29, 2024
a6c0d91
Merge branch 'otel' of https://github.com/googleapis/java-bigtable in…
gcf-owl-bot[bot] Mar 29, 2024
f00e838
remove stale reference
mutianf Apr 2, 2024
b330948
clean up
mutianf Apr 2, 2024
a2423ec
Merge branch 'main' into otel
mutianf Apr 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
feat: migrate to OTEL exporter (#1788)
* feat: migrate exporter to OTEL

* address comments

* filter out only bigtable metrics

* fix test

* use the bom

* update

* update

* update completeResultCode

* add a comment

* address comments

* address comments

* update pom

* small fix

* also check timestamp

* address comment

* updates

* update

* do not block on shutdown
  • Loading branch information
mutianf committed Jan 23, 2024
commit 043d551fba10b2156e0efe118b21249259e9c21c
7 changes: 7 additions & 0 deletions google-cloud-bigtable-deps-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-bom</artifactId>
<version>1.34.1</version>
igorbernstein2 marked this conversation as resolved.
Show resolved Hide resolved
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
20 changes: 20 additions & 0 deletions google-cloud-bigtable/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,26 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-metrics</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-common</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring</artifactId>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-cloud-monitoring-v3</artifactId>
</dependency>
mutianf marked this conversation as resolved.
Show resolved Hide resolved
</dependencies>

<profiles>
Expand Down
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 {

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);
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();
}
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();
}

@Override
public CompletableResultCode shutdown() {
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;
}
}