Skip to content

Commit

Permalink
feat: add stackdriver exporter (#1247)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
mutianf and gcf-owl-bot[bot] authored Aug 12, 2022
1 parent b865578 commit 7ce915e
Show file tree
Hide file tree
Showing 15 changed files with 789 additions and 31 deletions.
119 changes: 114 additions & 5 deletions google-cloud-bigtable-stats/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<version>2.10.4-SNAPSHOT</version><!-- {x-version-update:google-cloud-bigtable:current} -->
<description>Experimental project to shade OpenCensus dependencies.</description>

<properties>
<cloud.monitoring.version>3.3.6</cloud.monitoring.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
Expand All @@ -25,10 +29,60 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring-bom</artifactId>
<version>${cloud.monitoring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- opencensus dependencies -->
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-exporter-stats-stackdriver</artifactId>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-impl</artifactId>
<scope>runtime</scope>
</dependency>

<!-- cloud monitoring dependencies -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring</artifactId>
<!-- Excluding http dependencies since they're not used and are causing conflict in google-cloud-bigtable -->
<exclusions>
<exclusion>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-gson</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-cloud-monitoring-v3</artifactId>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-common-protos</artifactId>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-credentials</artifactId>
</dependency>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax</artifactId>
Expand All @@ -48,21 +102,35 @@
<groupId>com.google.api</groupId>
<artifactId>api-common</artifactId>
</dependency>

<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
<groupId>com.google.api</groupId>
<artifactId>gax-grpc</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>

<!-- runtime dependency for native image integration test -->
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-impl</artifactId>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<scope>runtime</scope>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
Expand All @@ -73,6 +141,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -111,6 +184,17 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
<!-- Ignore opencensus-exporter-metrics-util and opencensus-exporter-stats-stackdriver from -->
<!-- dependency analyze. opencensus-exporter-metrics-util is a transitive dependency from -->
<!-- opencensus-exporter-stats-stackdriver but it's not defined in java-shared-dependencies. -->
<!-- To make the opencensus version consistent, only include opencensus-exporter-stats-stackdriver -->
<!-- until opencensus-exporter-metrics-util is added to java-shared-dependencies. -->
<configuration>
<ignoredDependencies>
<ignoredDependency>io.opencensus:opencensus-exporter-metrics-util:*</ignoredDependency>
<ignoredDependency>io.opencensus:opencensus-exporter-stats-stackdriver:*</ignoredDependency>
</ignoredDependencies>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand All @@ -121,6 +205,31 @@
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-version-consistency</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<bannedDependencies>
<includes>
<!-- gax-grpc transitvely brings in opencensus-proto, which the latest version is 0.2.0-->
<!-- Only allow 0.2.0 for opencensus-proto and then the latest version (currently 0.31.1) for all other modules-->
<!-- this will need to be updated whenever the opencensus version gets updated -->
<dependency>io.opencensus:*:[0.31.1]</dependency>
<dependency>io.opencensus:opencensus-proto:[0.2.0]</dependency>
</includes>
</bannedDependencies>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2022 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;

import com.google.api.MonitoredResource;
import com.google.cloud.monitoring.v3.MetricServiceClient;
import com.google.monitoring.v3.CreateTimeSeriesRequest;
import com.google.monitoring.v3.ProjectName;
import io.opencensus.exporter.metrics.util.MetricExporter;
import io.opencensus.metrics.export.Metric;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

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

BigtableCreateTimeSeriesExporter(
MetricServiceClient metricServiceClient, MonitoredResource monitoredResource) {
this.metricServiceClient = metricServiceClient;
this.monitoredResource = monitoredResource;
this.clientId = BigtableStackdriverExportUtils.getDefaultTaskValue();
}

public void export(Collection<Metric> metrics) {
Map<String, List<com.google.monitoring.v3.TimeSeries>> projectToTimeSeries = new HashMap<>();

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

try {
projectToTimeSeries =
metric.getTimeSeriesList().stream()
.collect(
Collectors.groupingBy(
timeSeries ->
BigtableStackdriverExportUtils.getProjectId(
metric.getMetricDescriptor(), timeSeries),
Collectors.mapping(
timeSeries ->
BigtableStackdriverExportUtils.convertTimeSeries(
metric.getMetricDescriptor(),
timeSeries,
clientId,
monitoredResource),
Collectors.toList())));

for (Map.Entry<String, List<com.google.monitoring.v3.TimeSeries>> entry :
projectToTimeSeries.entrySet()) {
ProjectName projectName = ProjectName.of(entry.getKey());
CreateTimeSeriesRequest request =
CreateTimeSeriesRequest.newBuilder()
.setName(projectName.toString())
.addAllTimeSeries(entry.getValue())
.build();
this.metricServiceClient.createServiceTimeSeries(request);
}
} catch (Throwable e) {
logger.log(Level.WARNING, "Exception thrown when exporting TimeSeries.", e);
}
}
}
}
Loading

0 comments on commit 7ce915e

Please sign in to comment.