Skip to content

Commit 7ce915e

Browse files
feat: add stackdriver exporter (#1247)
* remove status from application latency * feat: update tracers to use built in metrics * feat: add response protos * feat: add response protos * feat: add stackdriver exporter * fix tests * fix dependency * remove unused dependency * clean up code * udpates on comments * remove unused setting * make metrics consistent with cloud monitoring * convert undefined to global * update * add bigtable tracer back in the base callable * fix format * fix the tag name * add the link to the form * fix format * fix dependency conflicts * fix image tests * update undefined cluster to global * address comments * tweak export interval * remove unused metric kind * get project id from the metrics * clean up imports * remove unused method and rewrite create timeseries exporter * fix integration test * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent b865578 commit 7ce915e

File tree

15 files changed

+789
-31
lines changed

15 files changed

+789
-31
lines changed

google-cloud-bigtable-stats/pom.xml

Lines changed: 114 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<version>2.10.4-SNAPSHOT</version><!-- {x-version-update:google-cloud-bigtable:current} -->
1717
<description>Experimental project to shade OpenCensus dependencies.</description>
1818

19+
<properties>
20+
<cloud.monitoring.version>3.3.6</cloud.monitoring.version>
21+
</properties>
22+
1923
<dependencyManagement>
2024
<dependencies>
2125
<dependency>
@@ -25,10 +29,60 @@
2529
<type>pom</type>
2630
<scope>import</scope>
2731
</dependency>
32+
<dependency>
33+
<groupId>com.google.cloud</groupId>
34+
<artifactId>google-cloud-monitoring-bom</artifactId>
35+
<version>${cloud.monitoring.version}</version>
36+
<type>pom</type>
37+
<scope>import</scope>
38+
</dependency>
2839
</dependencies>
2940
</dependencyManagement>
3041

3142
<dependencies>
43+
<!-- opencensus dependencies -->
44+
<dependency>
45+
<groupId>io.opencensus</groupId>
46+
<artifactId>opencensus-api</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.opencensus</groupId>
50+
<artifactId>opencensus-exporter-stats-stackdriver</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>io.opencensus</groupId>
54+
<artifactId>opencensus-impl</artifactId>
55+
<scope>runtime</scope>
56+
</dependency>
57+
58+
<!-- cloud monitoring dependencies -->
59+
<dependency>
60+
<groupId>com.google.cloud</groupId>
61+
<artifactId>google-cloud-monitoring</artifactId>
62+
<!-- Excluding http dependencies since they're not used and are causing conflict in google-cloud-bigtable -->
63+
<exclusions>
64+
<exclusion>
65+
<groupId>com.google.http-client</groupId>
66+
<artifactId>google-http-client-gson</artifactId>
67+
</exclusion>
68+
<exclusion>
69+
<groupId>com.google.http-client</groupId>
70+
<artifactId>google-http-client</artifactId>
71+
</exclusion>
72+
</exclusions>
73+
</dependency>
74+
<dependency>
75+
<groupId>com.google.api.grpc</groupId>
76+
<artifactId>proto-google-cloud-monitoring-v3</artifactId>
77+
</dependency>
78+
<dependency>
79+
<groupId>com.google.api.grpc</groupId>
80+
<artifactId>proto-google-common-protos</artifactId>
81+
</dependency>
82+
<dependency>
83+
<groupId>com.google.auth</groupId>
84+
<artifactId>google-auth-library-credentials</artifactId>
85+
</dependency>
3286
<dependency>
3387
<groupId>com.google.api</groupId>
3488
<artifactId>gax</artifactId>
@@ -48,21 +102,35 @@
48102
<groupId>com.google.api</groupId>
49103
<artifactId>api-common</artifactId>
50104
</dependency>
51-
52105
<dependency>
53-
<groupId>io.opencensus</groupId>
54-
<artifactId>opencensus-api</artifactId>
106+
<groupId>com.google.api</groupId>
107+
<artifactId>gax-grpc</artifactId>
108+
</dependency>
109+
<dependency>
110+
<groupId>com.google.protobuf</groupId>
111+
<artifactId>protobuf-java</artifactId>
55112
</dependency>
56113
<dependency>
57114
<groupId>com.google.guava</groupId>
58115
<artifactId>guava</artifactId>
59116
</dependency>
117+
<dependency>
118+
<groupId>org.threeten</groupId>
119+
<artifactId>threetenbp</artifactId>
120+
</dependency>
121+
<dependency>
122+
<groupId>com.google.code.findbugs</groupId>
123+
<artifactId>jsr305</artifactId>
124+
</dependency>
60125

126+
<!-- runtime dependency for native image integration test -->
61127
<dependency>
62-
<groupId>io.opencensus</groupId>
63-
<artifactId>opencensus-impl</artifactId>
128+
<groupId>com.google.http-client</groupId>
129+
<artifactId>google-http-client</artifactId>
64130
<scope>runtime</scope>
65131
</dependency>
132+
133+
<!-- test dependencies -->
66134
<dependency>
67135
<groupId>com.google.truth</groupId>
68136
<artifactId>truth</artifactId>
@@ -73,6 +141,11 @@
73141
<artifactId>junit</artifactId>
74142
<scope>test</scope>
75143
</dependency>
144+
<dependency>
145+
<groupId>org.mockito</groupId>
146+
<artifactId>mockito-core</artifactId>
147+
<scope>test</scope>
148+
</dependency>
76149
</dependencies>
77150

78151
<build>
@@ -111,6 +184,17 @@
111184
<groupId>org.apache.maven.plugins</groupId>
112185
<artifactId>maven-dependency-plugin</artifactId>
113186
<version>3.3.0</version>
187+
<!-- Ignore opencensus-exporter-metrics-util and opencensus-exporter-stats-stackdriver from -->
188+
<!-- dependency analyze. opencensus-exporter-metrics-util is a transitive dependency from -->
189+
<!-- opencensus-exporter-stats-stackdriver but it's not defined in java-shared-dependencies. -->
190+
<!-- To make the opencensus version consistent, only include opencensus-exporter-stats-stackdriver -->
191+
<!-- until opencensus-exporter-metrics-util is added to java-shared-dependencies. -->
192+
<configuration>
193+
<ignoredDependencies>
194+
<ignoredDependency>io.opencensus:opencensus-exporter-metrics-util:*</ignoredDependency>
195+
<ignoredDependency>io.opencensus:opencensus-exporter-stats-stackdriver:*</ignoredDependency>
196+
</ignoredDependencies>
197+
</configuration>
114198
</plugin>
115199
<plugin>
116200
<groupId>org.codehaus.mojo</groupId>
@@ -121,6 +205,31 @@
121205
</excludes>
122206
</configuration>
123207
</plugin>
208+
<plugin>
209+
<groupId>org.apache.maven.plugins</groupId>
210+
<artifactId>maven-enforcer-plugin</artifactId>
211+
<executions>
212+
<execution>
213+
<id>enforce-version-consistency</id>
214+
<goals>
215+
<goal>enforce</goal>
216+
</goals>
217+
<configuration>
218+
<rules>
219+
<bannedDependencies>
220+
<includes>
221+
<!-- gax-grpc transitvely brings in opencensus-proto, which the latest version is 0.2.0-->
222+
<!-- Only allow 0.2.0 for opencensus-proto and then the latest version (currently 0.31.1) for all other modules-->
223+
<!-- this will need to be updated whenever the opencensus version gets updated -->
224+
<dependency>io.opencensus:*:[0.31.1]</dependency>
225+
<dependency>io.opencensus:opencensus-proto:[0.2.0]</dependency>
226+
</includes>
227+
</bannedDependencies>
228+
</rules>
229+
</configuration>
230+
</execution>
231+
</executions>
232+
</plugin>
124233
</plugins>
125234
</build>
126235
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2022 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;
17+
18+
import com.google.api.MonitoredResource;
19+
import com.google.cloud.monitoring.v3.MetricServiceClient;
20+
import com.google.monitoring.v3.CreateTimeSeriesRequest;
21+
import com.google.monitoring.v3.ProjectName;
22+
import io.opencensus.exporter.metrics.util.MetricExporter;
23+
import io.opencensus.metrics.export.Metric;
24+
import java.util.Collection;
25+
import java.util.HashMap;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.logging.Level;
29+
import java.util.logging.Logger;
30+
import java.util.stream.Collectors;
31+
32+
final class BigtableCreateTimeSeriesExporter extends MetricExporter {
33+
private static final Logger logger =
34+
Logger.getLogger(BigtableCreateTimeSeriesExporter.class.getName());
35+
private final MetricServiceClient metricServiceClient;
36+
private final MonitoredResource monitoredResource;
37+
private final String clientId;
38+
39+
BigtableCreateTimeSeriesExporter(
40+
MetricServiceClient metricServiceClient, MonitoredResource monitoredResource) {
41+
this.metricServiceClient = metricServiceClient;
42+
this.monitoredResource = monitoredResource;
43+
this.clientId = BigtableStackdriverExportUtils.getDefaultTaskValue();
44+
}
45+
46+
public void export(Collection<Metric> metrics) {
47+
Map<String, List<com.google.monitoring.v3.TimeSeries>> projectToTimeSeries = new HashMap<>();
48+
49+
for (Metric metric : metrics) {
50+
// only export bigtable metrics
51+
if (!metric.getMetricDescriptor().getName().contains("bigtable")) {
52+
continue;
53+
}
54+
55+
try {
56+
projectToTimeSeries =
57+
metric.getTimeSeriesList().stream()
58+
.collect(
59+
Collectors.groupingBy(
60+
timeSeries ->
61+
BigtableStackdriverExportUtils.getProjectId(
62+
metric.getMetricDescriptor(), timeSeries),
63+
Collectors.mapping(
64+
timeSeries ->
65+
BigtableStackdriverExportUtils.convertTimeSeries(
66+
metric.getMetricDescriptor(),
67+
timeSeries,
68+
clientId,
69+
monitoredResource),
70+
Collectors.toList())));
71+
72+
for (Map.Entry<String, List<com.google.monitoring.v3.TimeSeries>> entry :
73+
projectToTimeSeries.entrySet()) {
74+
ProjectName projectName = ProjectName.of(entry.getKey());
75+
CreateTimeSeriesRequest request =
76+
CreateTimeSeriesRequest.newBuilder()
77+
.setName(projectName.toString())
78+
.addAllTimeSeries(entry.getValue())
79+
.build();
80+
this.metricServiceClient.createServiceTimeSeries(request);
81+
}
82+
} catch (Throwable e) {
83+
logger.log(Level.WARNING, "Exception thrown when exporting TimeSeries.", e);
84+
}
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)