16
16
17
17
package com .example .appengine .bigquerylogging ;
18
18
19
- // [START bigquery_logging_all]
20
- // [START bigquery_logging_deps]
21
-
22
19
import com .google .api .Metric ;
23
20
import com .google .api .MetricDescriptor ;
24
21
import com .google .cloud .ServiceOptions ;
52
49
import java .util .Set ;
53
50
import java .util .UUID ;
54
51
import java .util .stream .Collectors ;
55
- // [END bigquery_logging_deps]
56
52
57
53
public class BigQueryRunner {
58
54
private static final String CUSTOM_METRIC_FILTER =
59
55
"metric.type = starts_with(\" custom.googleapis.com/\" )" ;
60
56
private static BigQueryRunner instance ;
61
57
62
- // [START bigquery_logging_metrics]
63
58
private static final MetricDescriptor QUERY_DURATION_METRIC = MetricDescriptor
64
59
.newBuilder ()
65
60
.setName ("custom.googleapis.com/queryDuration" )
@@ -81,7 +76,6 @@ public class BigQueryRunner {
81
76
private static final Set <MetricDescriptor > REQUIRED_METRICS = ImmutableSet .of (
82
77
QUERY_DURATION_METRIC , ROWS_RETURNED_METRIC
83
78
);
84
- // [END bigquery_logging_metrics]
85
79
86
80
private static TableResult mostRecentRunResult ;
87
81
private static Set <String > existingMetrics = Sets .newHashSet ();
@@ -117,7 +111,6 @@ public static TableResult getMostRecentRunResult() {
117
111
}
118
112
119
113
public void runQuery () throws InterruptedException {
120
- // [START bigquery_logging_query]
121
114
QueryJobConfiguration queryConfig =
122
115
QueryJobConfiguration .newBuilder (
123
116
"SELECT "
@@ -150,9 +143,7 @@ public void runQuery() throws InterruptedException {
150
143
// errors, not just the latest one.
151
144
throw new RuntimeException (queryJob .getStatus ().getError ().toString ());
152
145
}
153
- // [END bigquery_logging_query]
154
146
155
- // [START bigquery_logging_log_metrics]
156
147
// Log the result metrics.
157
148
TableResult result = queryJob .getQueryResults ();
158
149
@@ -172,7 +163,6 @@ public void runQuery() throws InterruptedException {
172
163
createMetricsIfNeeded ();
173
164
client .createTimeSeries (request );
174
165
os .println ("Done writing metrics." );
175
- // [END bigquery_logging_log_metrics]
176
166
177
167
mostRecentRunResult = result ;
178
168
}
@@ -205,7 +195,6 @@ private TimeSeries prepareMetric(MetricDescriptor requiredMetric, long metricVal
205
195
.build ();
206
196
}
207
197
208
- // [START bigquery_logging_list_time_series]
209
198
public List <TimeSeriesSummary > getTimeSeriesValues () {
210
199
List <TimeSeriesSummary > summaries = Lists .newArrayList ();
211
200
createMetricsIfNeeded ();
@@ -236,9 +225,7 @@ public List<TimeSeriesSummary> getTimeSeriesValues() {
236
225
}
237
226
return summaries ;
238
227
}
239
- // [END bigquery_logging_list_time_series]
240
228
241
- // [START bigquery_logging_list_and_create_metrics]
242
229
private void createMetricsIfNeeded () {
243
230
// If all required metrics already exist, no need to make service calls.
244
231
if (REQUIRED_METRICS .stream ()
@@ -262,9 +249,7 @@ private void createMetricsIfNeeded() {
262
249
.filter (metric -> !existingMetrics .contains (metric .getDisplayName ()))
263
250
.forEach (this ::createMetric );
264
251
}
265
- // [END bigquery_logging_list_and_create_metrics]
266
252
267
- // [START bigquery_logging_create_metric]
268
253
private void createMetric (MetricDescriptor newMetric ) {
269
254
CreateMetricDescriptorRequest request = CreateMetricDescriptorRequest .newBuilder ()
270
255
.setName (projectName )
@@ -273,6 +258,5 @@ private void createMetric(MetricDescriptor newMetric) {
273
258
274
259
client .createMetricDescriptor (request );
275
260
}
276
- // [END bigquery_logging_create_metric]
261
+
277
262
}
278
- // [END bigquery_logging_all]
0 commit comments