Skip to content

Commit 8fe6f63

Browse files
committed
feat: add stackdriver exporter
1 parent a8b26e0 commit 8fe6f63

File tree

6 files changed

+763
-0
lines changed

6 files changed

+763
-0
lines changed

google-cloud-bigtable-stats/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@
8585
<artifactId>grpc-api</artifactId>
8686
<scope>test</scope>
8787
</dependency>
88+
<dependency>
89+
<groupId>org.mockito</groupId>
90+
<artifactId>mockito-core</artifactId>
91+
<scope>test</scope>
92+
</dependency>
8893
</dependencies>
8994

9095
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.bigtable.stats.exporter;
17+
18+
import com.google.api.MonitoredResource;
19+
import com.google.api.gax.rpc.ApiException;
20+
import com.google.cloud.bigtable.stats.BuiltinMeasureConstants;
21+
import com.google.cloud.monitoring.v3.MetricServiceClient;
22+
import com.google.monitoring.v3.CreateTimeSeriesRequest;
23+
import com.google.monitoring.v3.ProjectName;
24+
import com.google.monitoring.v3.TimeSeries;
25+
import io.opencensus.exporter.metrics.util.MetricExporter;
26+
import io.opencensus.metrics.LabelKey;
27+
import io.opencensus.metrics.LabelValue;
28+
import io.opencensus.metrics.export.Metric;
29+
import java.util.ArrayList;
30+
import java.util.Collection;
31+
import java.util.List;
32+
import java.util.logging.Level;
33+
import java.util.logging.Logger;
34+
35+
final class BigtableCreateTimeSeriesExporter extends MetricExporter {
36+
private static final Logger logger =
37+
Logger.getLogger(BigtableCreateTimeSeriesExporter.class.getName());
38+
private final ProjectName projectName;
39+
private final MetricServiceClient metricServiceClient;
40+
private final MonitoredResource monitoredResource;
41+
private final String domain;
42+
43+
BigtableCreateTimeSeriesExporter(
44+
String projectId,
45+
MetricServiceClient metricServiceClient,
46+
MonitoredResource monitoredResource) {
47+
this.projectName = ProjectName.newBuilder().setProject(projectId).build();
48+
this.metricServiceClient = metricServiceClient;
49+
this.monitoredResource = monitoredResource;
50+
this.domain = "bigtable.googleapis.com/client/";
51+
}
52+
53+
public void export(Collection<Metric> metrics) {
54+
List<TimeSeries> timeSeriesList = new ArrayList(metrics.size());
55+
56+
for (Metric metric : metrics) {
57+
// only export bigtable metrics
58+
if (!metric.getMetricDescriptor().getName().contains("bigtable")) {
59+
continue;
60+
}
61+
62+
for (io.opencensus.metrics.export.TimeSeries timeSeries : metric.getTimeSeriesList()) {
63+
MonitoredResource.Builder monitoredResourceBuilder = this.monitoredResource.toBuilder();
64+
65+
List<LabelKey> keys = metric.getMetricDescriptor().getLabelKeys();
66+
List<LabelValue> labelValues = timeSeries.getLabelValues();
67+
68+
List<LabelKey> updatedKeys = new ArrayList<>();
69+
List<LabelValue> updatedValues = new ArrayList<>();
70+
71+
for (int i = 0; i < labelValues.size(); i++) {
72+
if (keys.get(i).getKey().equals(BuiltinMeasureConstants.PROJECT_ID.getName())) {
73+
monitoredResourceBuilder.putLabels(
74+
BuiltinMeasureConstants.PROJECT_ID.getName(), labelValues.get(i).getValue());
75+
} else if (keys.get(i).getKey().equals(BuiltinMeasureConstants.INSTANCE_ID.getName())) {
76+
monitoredResourceBuilder.putLabels(
77+
BuiltinMeasureConstants.INSTANCE_ID.getName(), labelValues.get(i).getValue());
78+
} else if (keys.get(i).getKey().equals(BuiltinMeasureConstants.CLUSTER.getName())) {
79+
monitoredResourceBuilder.putLabels(
80+
BuiltinMeasureConstants.CLUSTER.getName(), labelValues.get(i).getValue());
81+
} else if (keys.get(i).getKey().equals(BuiltinMeasureConstants.ZONE.getName())) {
82+
monitoredResourceBuilder.putLabels(
83+
BuiltinMeasureConstants.ZONE.getName(), labelValues.get(i).getValue());
84+
} else if (keys.get(i).getKey().equals(BuiltinMeasureConstants.TABLE.getName())) {
85+
monitoredResourceBuilder.putLabels(
86+
BuiltinMeasureConstants.TABLE.getName(), labelValues.get(i).getValue());
87+
} else {
88+
updatedKeys.add(keys.get(i));
89+
updatedValues.add(labelValues.get(i));
90+
}
91+
}
92+
93+
updatedKeys.add(LabelKey.create(BuiltinMeasureConstants.CLIENT_ID.getName(), "client id"));
94+
updatedValues.add(
95+
LabelValue.create(BigtableStackdriverExportUtils.generateDefaultTaskValue()));
96+
97+
timeSeriesList.add(
98+
BigtableStackdriverExportUtils.convertTimeSeries(
99+
metric.getMetricDescriptor().getName(),
100+
metric.getMetricDescriptor().getType(),
101+
updatedKeys,
102+
updatedValues,
103+
timeSeries,
104+
monitoredResourceBuilder.build(),
105+
this.domain,
106+
this.projectName.getProject()));
107+
}
108+
}
109+
110+
try {
111+
CreateTimeSeriesRequest request =
112+
CreateTimeSeriesRequest.newBuilder()
113+
.setName(this.projectName.toString())
114+
.addAllTimeSeries(timeSeriesList)
115+
.build();
116+
117+
this.metricServiceClient.createServiceTimeSeries(request);
118+
} catch (ApiException e) {
119+
logger.log(Level.WARNING, "ApiException thrown when exporting TimeSeries.", e);
120+
} catch (Throwable e) {
121+
logger.log(Level.WARNING, "Exception thrown when exporting TimeSeries.", e);
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)