Skip to content

Commit

Permalink
remove unused method and rewrite create timeseries exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
mutianf committed Aug 11, 2022
1 parent 5996119 commit ee04276
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import com.google.monitoring.v3.ProjectName;
import io.opencensus.exporter.metrics.util.MetricExporter;
import io.opencensus.metrics.export.Metric;
import io.opencensus.metrics.export.TimeSeries;
import java.util.ArrayList;
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 =
Expand All @@ -54,24 +53,29 @@ public void export(Collection<Metric> metrics) {
}

try {
for (TimeSeries timeSeries : metric.getTimeSeriesList()) {
// Get the project id from the metrics so we could publish with multiple project ids
String projectId =
BigtableStackdriverExportUtils.getProjectId(metric.getMetricDescriptor(), timeSeries);
List<com.google.monitoring.v3.TimeSeries> timeSeriesList =
projectToTimeSeries.getOrDefault(projectId, new ArrayList<>());
timeSeriesList.add(
BigtableStackdriverExportUtils.convertTimeSeries(
metric.getMetricDescriptor(), timeSeries, clientId, monitoredResource));
projectToTimeSeries.put(projectId, timeSeriesList);
}
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 (String projectId : projectToTimeSeries.keySet()) {
ProjectName projectName = ProjectName.of(projectId);
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(projectToTimeSeries.get(projectId))
.addAllTimeSeries(entry.getValue())
.build();
this.metricServiceClient.createServiceTimeSeries(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,15 @@ static com.google.monitoring.v3.TimeSeries convertTimeSeries(
return builder.build();
}

static String getProjectId(MetricDescriptor metricDescriptor, TimeSeries timeSeries)
throws Exception {
static String getProjectId(MetricDescriptor metricDescriptor, TimeSeries timeSeries) {
List<LabelKey> labelKeys = metricDescriptor.getLabelKeys();
List<LabelValue> labelValues = timeSeries.getLabelValues();
for (int i = 0; i < labelKeys.size(); i++) {
if (labelKeys.get(i).getKey().equals(BuiltinMeasureConstants.PROJECT_ID.getName())) {
return labelValues.get(i).getValue();
}
}
throw new Exception("Can't find project id for the current timeseries");
throw new IllegalStateException("Can't find project id for the current timeseries");
}

static String getDefaultTaskValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,4 @@ static MetricServiceClient createMetricServiceClient(Credentials credentials, Du
settingsBuilder.createTimeSeriesSettings().setSimpleTimeoutNoRetries(stackdriverDuration);
return MetricServiceClient.create(settingsBuilder.build());
}

public static void unregister() {
synchronized (lock) {
if (instance != null) {
instance.intervalMetricReader.stop();
}

instance = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,25 @@ public static void enableGfeOpenCensusStats() {
}

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

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

/** Unregister built in metrics. * */
@BetaApi("Built in metrics is not currently stable and may change in the future")
public static void unregisterBuiltinMetrics() {
if (BUILTIN_METRICS_REGISTERED.compareAndSet(true, false)) {
BigtableStackdriverStatsExporter.unregister();
}
}

/** Returns the target project id. */
public String getProjectId() {
return stubSettings.getProjectId();
Expand Down Expand Up @@ -283,16 +278,6 @@ public Long getBatchMutationsTargetRpcLatencyMs() {
return stubSettings.bulkMutateRowsSettings().getTargetRpcLatencyMs();
}

/**
* Gets if built in metrics are registered.
*
* @return
*/
@BetaApi("Built in metrics is not currently stable and may change in the future")
public static boolean isBuiltinMetricsRegistered() {
return BUILTIN_METRICS_REGISTERED.get();
}

/** Returns the underlying RPC settings. */
public EnhancedBigtableStubSettings getStubSettings() {
return stubSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class BuiltinMetricsTracer extends BigtableTracer {
private AtomicInteger requestLeft = new AtomicInteger(0);

// Monitored resource labels
private String tableId = "undefined";
private String tableId = "unspecified";
private String zone = "global";
private String cluster = "global";
private String cluster = "unspecified";

// gfe stats
private AtomicLong gfeMissingHeaders = new AtomicLong(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ public void testMutateRowAttempts() {
verify(statsRecorderWrapper, timeout(50).times(fakeService.getAttemptCounter().get() + 1))
.record(status.capture(), tableId.capture(), zone.capture(), cluster.capture());
assertThat(zone.getAllValues()).containsExactly("global", "global", ZONE, ZONE);
assertThat(cluster.getAllValues()).containsExactly("global", "global", CLUSTER, CLUSTER);
assertThat(cluster.getAllValues())
.containsExactly("unspecified", "unspecified", CLUSTER, CLUSTER);
assertThat(status.getAllValues()).containsExactly("UNAVAILABLE", "UNAVAILABLE", "OK", "OK");
}

Expand Down

0 comments on commit ee04276

Please sign in to comment.