Skip to content

Commit 57235de

Browse files
committed
remove unused method and rewrite create timeseries exporter
1 parent 5996119 commit 57235de

File tree

6 files changed

+35
-56
lines changed

6 files changed

+35
-56
lines changed

google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableCreateTimeSeriesExporter.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@
2121
import com.google.monitoring.v3.ProjectName;
2222
import io.opencensus.exporter.metrics.util.MetricExporter;
2323
import io.opencensus.metrics.export.Metric;
24-
import io.opencensus.metrics.export.TimeSeries;
25-
import java.util.ArrayList;
2624
import java.util.Collection;
2725
import java.util.HashMap;
2826
import java.util.List;
2927
import java.util.Map;
3028
import java.util.logging.Level;
3129
import java.util.logging.Logger;
30+
import java.util.stream.Collectors;
3231

3332
final class BigtableCreateTimeSeriesExporter extends MetricExporter {
3433
private static final Logger logger =
@@ -54,24 +53,29 @@ public void export(Collection<Metric> metrics) {
5453
}
5554

5655
try {
57-
for (TimeSeries timeSeries : metric.getTimeSeriesList()) {
58-
// Get the project id from the metrics so we could publish with multiple project ids
59-
String projectId =
60-
BigtableStackdriverExportUtils.getProjectId(metric.getMetricDescriptor(), timeSeries);
61-
List<com.google.monitoring.v3.TimeSeries> timeSeriesList =
62-
projectToTimeSeries.getOrDefault(projectId, new ArrayList<>());
63-
timeSeriesList.add(
64-
BigtableStackdriverExportUtils.convertTimeSeries(
65-
metric.getMetricDescriptor(), timeSeries, clientId, monitoredResource));
66-
projectToTimeSeries.put(projectId, timeSeriesList);
67-
}
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())));
6871

69-
for (String projectId : projectToTimeSeries.keySet()) {
70-
ProjectName projectName = ProjectName.of(projectId);
72+
for (Map.Entry<String, List<com.google.monitoring.v3.TimeSeries>> entry :
73+
projectToTimeSeries.entrySet()) {
74+
ProjectName projectName = ProjectName.of(entry.getKey());
7175
CreateTimeSeriesRequest request =
7276
CreateTimeSeriesRequest.newBuilder()
7377
.setName(projectName.toString())
74-
.addAllTimeSeries(projectToTimeSeries.get(projectId))
78+
.addAllTimeSeries(entry.getValue())
7579
.build();
7680
this.metricServiceClient.createServiceTimeSeries(request);
7781
}

google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableStackdriverExportUtils.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,15 @@ static com.google.monitoring.v3.TimeSeries convertTimeSeries(
143143
return builder.build();
144144
}
145145

146-
static String getProjectId(MetricDescriptor metricDescriptor, TimeSeries timeSeries)
147-
throws Exception {
146+
static String getProjectId(MetricDescriptor metricDescriptor, TimeSeries timeSeries) {
148147
List<LabelKey> labelKeys = metricDescriptor.getLabelKeys();
149148
List<LabelValue> labelValues = timeSeries.getLabelValues();
150149
for (int i = 0; i < labelKeys.size(); i++) {
151150
if (labelKeys.get(i).getKey().equals(BuiltinMeasureConstants.PROJECT_ID.getName())) {
152151
return labelValues.get(i).getValue();
153152
}
154153
}
155-
throw new Exception("Can't find project id for the current timeseries");
154+
throw new IllegalStateException("Can't find project id for the current timeseries");
156155
}
157156

158157
static String getDefaultTaskValue() {

google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableStackdriverStatsExporter.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,4 @@ static MetricServiceClient createMetricServiceClient(Credentials credentials, Du
9090
settingsBuilder.createTimeSeriesSettings().setSimpleTimeoutNoRetries(stackdriverDuration);
9191
return MetricServiceClient.create(settingsBuilder.build());
9292
}
93-
94-
public static void unregister() {
95-
synchronized (lock) {
96-
if (instance != null) {
97-
instance.intervalMetricReader.stop();
98-
}
99-
100-
instance = null;
101-
}
102-
}
10393
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/BigtableDataSettings.java

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,25 @@ public static void enableGfeOpenCensusStats() {
201201
}
202202

203203
/**
204-
* Register built in metrics. This is an experimental feature. Please fill up this form to have
205-
* your project allow listed for the private preview: https://forms.gle/xuhu6vCunn2MjV2m9
204+
* Register built in metrics.
205+
*
206+
* <p>This is an experimental feature. Please fill up this form to have your project
207+
* allow listed for the private preview: https://forms.gle/xuhu6vCunn2MjV2m9
206208
*/
207209
@BetaApi("Built in metric is not currently stable and may change in the future")
208210
public static void registerBuiltinMetrics() throws IOException {
209-
GoogleCredentials.getApplicationDefault();
210211
if (BUILTIN_METRICS_REGISTERED.compareAndSet(false, true)) {
211212
BuiltinViews.registerBigtableBuiltinViews();
212213
BigtableStackdriverStatsExporter.register(GoogleCredentials.getApplicationDefault());
213214
}
214215
}
215216

216217
/**
217-
* Register built in metrics with credentials. This is an experimental feature. Please fill up
218-
* this form to have your project allow listed for the private preview:
219-
* https://forms.gle/xuhu6vCunn2MjV2m9
218+
* Register built in metrics with credentials. The credentials need to have metric write access
219+
* for all the projects you're publishing to.
220+
*
221+
* <p>This is an experimental feature. Please fill up this form to have your project allow
222+
* listed for the private preview: https://forms.gle/xuhu6vCunn2MjV2m9
220223
*/
221224
@BetaApi("Built in metric is not currently stable and may change in the future")
222225
public static void registerBuiltinMetrics(Credentials credentials) throws IOException {
@@ -226,14 +229,6 @@ public static void registerBuiltinMetrics(Credentials credentials) throws IOExce
226229
}
227230
}
228231

229-
/** Unregister built in metrics. * */
230-
@BetaApi("Built in metrics is not currently stable and may change in the future")
231-
public static void unregisterBuiltinMetrics() {
232-
if (BUILTIN_METRICS_REGISTERED.compareAndSet(true, false)) {
233-
BigtableStackdriverStatsExporter.unregister();
234-
}
235-
}
236-
237232
/** Returns the target project id. */
238233
public String getProjectId() {
239234
return stubSettings.getProjectId();
@@ -283,16 +278,6 @@ public Long getBatchMutationsTargetRpcLatencyMs() {
283278
return stubSettings.bulkMutateRowsSettings().getTargetRpcLatencyMs();
284279
}
285280

286-
/**
287-
* Gets if built in metrics are registered.
288-
*
289-
* @return
290-
*/
291-
@BetaApi("Built in metrics is not currently stable and may change in the future")
292-
public static boolean isBuiltinMetricsRegistered() {
293-
return BUILTIN_METRICS_REGISTERED.get();
294-
}
295-
296281
/** Returns the underlying RPC settings. */
297282
public EnhancedBigtableStubSettings getStubSettings() {
298283
return stubSettings;

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ class BuiltinMetricsTracer extends BigtableTracer {
6666
private AtomicInteger requestLeft = new AtomicInteger(0);
6767

6868
// Monitored resource labels
69-
private String tableId = "undefined";
69+
private String tableId = "unspecified";
7070
private String zone = "global";
71-
private String cluster = "global";
71+
private String cluster = "unspecified";
7272

7373
// gfe stats
7474
private AtomicLong gfeMissingHeaders = new AtomicLong(0);

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ public void testMutateRowAttempts() {
340340
verify(statsRecorderWrapper, timeout(50).times(fakeService.getAttemptCounter().get() + 1))
341341
.record(status.capture(), tableId.capture(), zone.capture(), cluster.capture());
342342
assertThat(zone.getAllValues()).containsExactly("global", "global", ZONE, ZONE);
343-
assertThat(cluster.getAllValues()).containsExactly("global", "global", CLUSTER, CLUSTER);
343+
assertThat(cluster.getAllValues())
344+
.containsExactly("unspecified", "unspecified", CLUSTER, CLUSTER);
344345
assertThat(status.getAllValues()).containsExactly("UNAVAILABLE", "UNAVAILABLE", "OK", "OK");
345346
}
346347

0 commit comments

Comments
 (0)