Skip to content

Commit

Permalink
feat: add stackdriver exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
mutianf committed May 12, 2022
1 parent a8b26e0 commit 8fe6f63
Show file tree
Hide file tree
Showing 6 changed files with 763 additions and 0 deletions.
5 changes: 5 additions & 0 deletions google-cloud-bigtable-stats/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
<artifactId>grpc-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 2021 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.stats.exporter;

import com.google.api.MonitoredResource;
import com.google.api.gax.rpc.ApiException;
import com.google.cloud.bigtable.stats.BuiltinMeasureConstants;
import com.google.cloud.monitoring.v3.MetricServiceClient;
import com.google.monitoring.v3.CreateTimeSeriesRequest;
import com.google.monitoring.v3.ProjectName;
import com.google.monitoring.v3.TimeSeries;
import io.opencensus.exporter.metrics.util.MetricExporter;
import io.opencensus.metrics.LabelKey;
import io.opencensus.metrics.LabelValue;
import io.opencensus.metrics.export.Metric;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

final class BigtableCreateTimeSeriesExporter extends MetricExporter {
private static final Logger logger =
Logger.getLogger(BigtableCreateTimeSeriesExporter.class.getName());
private final ProjectName projectName;
private final MetricServiceClient metricServiceClient;
private final MonitoredResource monitoredResource;
private final String domain;

BigtableCreateTimeSeriesExporter(
String projectId,
MetricServiceClient metricServiceClient,
MonitoredResource monitoredResource) {
this.projectName = ProjectName.newBuilder().setProject(projectId).build();
this.metricServiceClient = metricServiceClient;
this.monitoredResource = monitoredResource;
this.domain = "bigtable.googleapis.com/client/";
}

public void export(Collection<Metric> metrics) {
List<TimeSeries> timeSeriesList = new ArrayList(metrics.size());

for (Metric metric : metrics) {
// only export bigtable metrics
if (!metric.getMetricDescriptor().getName().contains("bigtable")) {
continue;
}

for (io.opencensus.metrics.export.TimeSeries timeSeries : metric.getTimeSeriesList()) {
MonitoredResource.Builder monitoredResourceBuilder = this.monitoredResource.toBuilder();

List<LabelKey> keys = metric.getMetricDescriptor().getLabelKeys();
List<LabelValue> labelValues = timeSeries.getLabelValues();

List<LabelKey> updatedKeys = new ArrayList<>();
List<LabelValue> updatedValues = new ArrayList<>();

for (int i = 0; i < labelValues.size(); i++) {
if (keys.get(i).getKey().equals(BuiltinMeasureConstants.PROJECT_ID.getName())) {
monitoredResourceBuilder.putLabels(
BuiltinMeasureConstants.PROJECT_ID.getName(), labelValues.get(i).getValue());
} else if (keys.get(i).getKey().equals(BuiltinMeasureConstants.INSTANCE_ID.getName())) {
monitoredResourceBuilder.putLabels(
BuiltinMeasureConstants.INSTANCE_ID.getName(), labelValues.get(i).getValue());
} else if (keys.get(i).getKey().equals(BuiltinMeasureConstants.CLUSTER.getName())) {
monitoredResourceBuilder.putLabels(
BuiltinMeasureConstants.CLUSTER.getName(), labelValues.get(i).getValue());
} else if (keys.get(i).getKey().equals(BuiltinMeasureConstants.ZONE.getName())) {
monitoredResourceBuilder.putLabels(
BuiltinMeasureConstants.ZONE.getName(), labelValues.get(i).getValue());
} else if (keys.get(i).getKey().equals(BuiltinMeasureConstants.TABLE.getName())) {
monitoredResourceBuilder.putLabels(
BuiltinMeasureConstants.TABLE.getName(), labelValues.get(i).getValue());
} else {
updatedKeys.add(keys.get(i));
updatedValues.add(labelValues.get(i));
}
}

updatedKeys.add(LabelKey.create(BuiltinMeasureConstants.CLIENT_ID.getName(), "client id"));
updatedValues.add(
LabelValue.create(BigtableStackdriverExportUtils.generateDefaultTaskValue()));

timeSeriesList.add(
BigtableStackdriverExportUtils.convertTimeSeries(
metric.getMetricDescriptor().getName(),
metric.getMetricDescriptor().getType(),
updatedKeys,
updatedValues,
timeSeries,
monitoredResourceBuilder.build(),
this.domain,
this.projectName.getProject()));
}
}

try {
CreateTimeSeriesRequest request =
CreateTimeSeriesRequest.newBuilder()
.setName(this.projectName.toString())
.addAllTimeSeries(timeSeriesList)
.build();

this.metricServiceClient.createServiceTimeSeries(request);
} catch (ApiException e) {
logger.log(Level.WARNING, "ApiException thrown when exporting TimeSeries.", e);
} catch (Throwable e) {
logger.log(Level.WARNING, "Exception thrown when exporting TimeSeries.", e);
}
}
}
Loading

0 comments on commit 8fe6f63

Please sign in to comment.