This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
/
StackdriverStatsExporter.java
450 lines (428 loc) · 16.6 KB
/
StackdriverStatsExporter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
* Copyright 2017, OpenCensus Authors
*
* 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
*
* http://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 io.opencensus.exporter.stats.stackdriver;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static io.opencensus.exporter.stats.stackdriver.StackdriverExportUtils.DEFAULT_CONSTANT_LABELS;
import static io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration.DEFAULT_DEADLINE;
import static io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration.DEFAULT_PROJECT_ID;
import static io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration.DEFAULT_RESOURCE;
import com.google.api.MonitoredResource;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.gax.rpc.FixedHeaderProvider;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.ServiceOptions;
import com.google.cloud.monitoring.v3.MetricServiceClient;
import com.google.cloud.monitoring.v3.MetricServiceSettings;
import com.google.cloud.monitoring.v3.stub.MetricServiceStub;
import com.google.common.annotations.VisibleForTesting;
import io.opencensus.common.Duration;
import io.opencensus.common.OpenCensusLibraryInformation;
import io.opencensus.exporter.metrics.util.IntervalMetricReader;
import io.opencensus.exporter.metrics.util.MetricReader;
import io.opencensus.metrics.LabelKey;
import io.opencensus.metrics.LabelValue;
import io.opencensus.metrics.Metrics;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
/**
* Exporter to Stackdriver Monitoring Client API v3.
*
* <p>Example of usage on Google Cloud VMs:
*
* <pre><code>
* public static void main(String[] args) {
* StackdriverStatsExporter.createAndRegister(
* StackdriverStatsConfiguration
* .builder()
* .setProjectId("MyStackdriverProjectId")
* .setExportInterval(Duration.fromMillis(100000))
* .build());
* ... // Do work.
* }
* </code></pre>
*
* @since 0.9
*/
@ThreadSafe
public final class StackdriverStatsExporter {
@VisibleForTesting static final Object monitor = new Object();
@GuardedBy("monitor")
@Nullable
private static StackdriverStatsExporter instance = null;
private static final String EXPORTER_SPAN_NAME = "ExportMetricsToStackdriver";
// See io.grpc.internal.GrpcUtil.USER_AGENT_KEY
private static final String USER_AGENT_KEY = "user-agent";
private static final String USER_AGENT =
"opencensus-java/" + OpenCensusLibraryInformation.VERSION;
private static final HeaderProvider OPENCENSUS_USER_AGENT_HEADER_PROVIDER =
FixedHeaderProvider.create(USER_AGENT_KEY, USER_AGENT);
private final IntervalMetricReader intervalMetricReader;
private StackdriverStatsExporter(
String projectId,
MetricServiceClient metricServiceClient,
Duration exportInterval,
MonitoredResource monitoredResource,
@Nullable String metricNamePrefix,
@Nullable String displayNamePrefix,
Map<LabelKey, LabelValue> constantLabels) {
IntervalMetricReader.Options.Builder intervalMetricReaderOptionsBuilder =
IntervalMetricReader.Options.builder();
intervalMetricReaderOptionsBuilder.setExportInterval(exportInterval);
intervalMetricReader =
IntervalMetricReader.create(
new CreateMetricDescriptorExporter(
projectId,
metricServiceClient,
metricNamePrefix,
displayNamePrefix,
constantLabels,
new CreateTimeSeriesExporter(
projectId,
metricServiceClient,
monitoredResource,
metricNamePrefix,
constantLabels)),
MetricReader.create(
MetricReader.Options.builder()
.setMetricProducerManager(
Metrics.getExportComponent().getMetricProducerManager())
.setSpanName(EXPORTER_SPAN_NAME)
.build()),
intervalMetricReaderOptionsBuilder.build());
}
/**
* Creates a StackdriverStatsExporter for an explicit project ID and using explicit credentials,
* with default Monitored Resource.
*
* <p>Only one Stackdriver exporter can be created.
*
* @param credentials a credentials used to authenticate API calls.
* @param projectId the cloud project id.
* @param exportInterval the interval between pushing stats to StackDriver.
* @throws IllegalStateException if a Stackdriver exporter already exists.
* @deprecated in favor of {@link #createAndRegister(StackdriverStatsConfiguration)}.
* @since 0.9
*/
@Deprecated
public static void createAndRegisterWithCredentialsAndProjectId(
Credentials credentials, String projectId, Duration exportInterval) throws IOException {
checkNotNull(credentials, "credentials");
checkNotNull(projectId, "projectId");
checkNotNull(exportInterval, "exportInterval");
createInternal(
credentials,
projectId,
exportInterval,
DEFAULT_RESOURCE,
null,
null,
DEFAULT_CONSTANT_LABELS,
DEFAULT_DEADLINE,
null);
}
/**
* Creates a Stackdriver Stats exporter for an explicit project ID, with default Monitored
* Resource.
*
* <p>Only one Stackdriver exporter can be created.
*
* <p>This uses the default application credentials. See {@link
* GoogleCredentials#getApplicationDefault}.
*
* <p>This is equivalent with:
*
* <pre>{@code
* StackdriverStatsExporter.createWithCredentialsAndProjectId(
* GoogleCredentials.getApplicationDefault(), projectId);
* }</pre>
*
* @param projectId the cloud project id.
* @param exportInterval the interval between pushing stats to StackDriver.
* @throws IllegalStateException if a Stackdriver exporter is already created.
* @deprecated in favor of {@link #createAndRegister(StackdriverStatsConfiguration)}.
* @since 0.9
*/
@Deprecated
public static void createAndRegisterWithProjectId(String projectId, Duration exportInterval)
throws IOException {
checkNotNull(projectId, "projectId");
checkNotNull(exportInterval, "exportInterval");
createInternal(
null,
projectId,
exportInterval,
DEFAULT_RESOURCE,
null,
null,
DEFAULT_CONSTANT_LABELS,
DEFAULT_DEADLINE,
null);
}
/**
* Creates a Stackdriver Stats exporter with a {@link StackdriverStatsConfiguration}.
*
* <p>Only one Stackdriver exporter can be created.
*
* <p>If {@code credentials} of the configuration is not set, the exporter will use the default
* application credentials. See {@link GoogleCredentials#getApplicationDefault}.
*
* <p>If {@code projectId} of the configuration is not set, the exporter will use the default
* project ID configured. See {@link ServiceOptions#getDefaultProjectId}.
*
* <p>If {@code exportInterval} of the configuration is not set, the exporter will use the default
* interval of one minute.
*
* <p>If {@code monitoredResources} of the configuration is not set, the exporter will try to
* create an appropriate {@code monitoredResources} based on the environment variables. In
* addition, please refer to
* cloud.google.com/monitoring/custom-metrics/creating-metrics#which-resource for a list of valid
* {@code MonitoredResource}s.
*
* <p>If {@code metricNamePrefix} of the configuration is not set, the exporter will use the
* default prefix "OpenCensus".
*
* @param configuration the {@code StackdriverStatsConfiguration}.
* @throws IllegalStateException if a Stackdriver exporter is already created.
* @since 0.11.0
*/
public static void createAndRegister(StackdriverStatsConfiguration configuration)
throws IOException {
checkNotNull(configuration, "configuration");
createInternal(
configuration.getCredentials(),
configuration.getProjectId(),
configuration.getExportInterval(),
configuration.getMonitoredResource(),
configuration.getMetricNamePrefix(),
configuration.getDisplayNamePrefix(),
configuration.getConstantLabels(),
configuration.getDeadline(),
configuration.getMetricServiceStub());
}
/**
* Creates a Stackdriver Stats exporter with default settings.
*
* <p>Only one Stackdriver exporter can be created.
*
* <p>This is equivalent with:
*
* <pre>{@code
* StackdriverStatsExporter.createAndRegister(StackdriverStatsConfiguration.builder().build());
* }</pre>
*
* <p>This method uses the default application credentials. See {@link
* GoogleCredentials#getApplicationDefault}.
*
* <p>This method uses the default project ID configured. See {@link
* ServiceOptions#getDefaultProjectId}.
*
* <p>This method uses the default interval of one minute.
*
* <p>This method uses the default resource created from the environment variables.
*
* <p>This method uses the default display name prefix "OpenCensus".
*
* @throws IllegalStateException if a Stackdriver exporter is already created.
* @since 0.11.0
*/
public static void createAndRegister() throws IOException {
createAndRegister(StackdriverStatsConfiguration.builder().build());
}
/**
* Creates a Stackdriver Stats exporter with default Monitored Resource.
*
* <p>Only one Stackdriver exporter can be created.
*
* <p>This uses the default application credentials. See {@link
* GoogleCredentials#getApplicationDefault}.
*
* <p>This uses the default project ID configured see {@link ServiceOptions#getDefaultProjectId}.
*
* <p>This is equivalent with:
*
* <pre>{@code
* StackdriverStatsExporter.createWithProjectId(ServiceOptions.getDefaultProjectId());
* }</pre>
*
* @param exportInterval the interval between pushing stats to StackDriver.
* @throws IllegalStateException if a Stackdriver exporter is already created.
* @deprecated in favor of {@link #createAndRegister(StackdriverStatsConfiguration)}.
* @since 0.9
*/
@Deprecated
public static void createAndRegister(Duration exportInterval) throws IOException {
checkNotNull(exportInterval, "exportInterval");
checkArgument(
!DEFAULT_PROJECT_ID.isEmpty(), "Cannot find a project ID from application default.");
createInternal(
null,
DEFAULT_PROJECT_ID,
exportInterval,
DEFAULT_RESOURCE,
null,
null,
DEFAULT_CONSTANT_LABELS,
DEFAULT_DEADLINE,
null);
}
/**
* Creates a Stackdriver Stats exporter with an explicit project ID and a custom Monitored
* Resource.
*
* <p>Only one Stackdriver exporter can be created.
*
* <p>Please refer to cloud.google.com/monitoring/custom-metrics/creating-metrics#which-resource
* for a list of valid {@code MonitoredResource}s.
*
* <p>This uses the default application credentials. See {@link
* GoogleCredentials#getApplicationDefault}.
*
* @param projectId the cloud project id.
* @param exportInterval the interval between pushing stats to StackDriver.
* @param monitoredResource the Monitored Resource used by exporter.
* @throws IllegalStateException if a Stackdriver exporter is already created.
* @deprecated in favor of {@link #createAndRegister(StackdriverStatsConfiguration)}.
* @since 0.10
*/
@Deprecated
public static void createAndRegisterWithProjectIdAndMonitoredResource(
String projectId, Duration exportInterval, MonitoredResource monitoredResource)
throws IOException {
checkNotNull(projectId, "projectId");
checkNotNull(exportInterval, "exportInterval");
checkNotNull(monitoredResource, "monitoredResource");
createInternal(
null,
projectId,
exportInterval,
monitoredResource,
null,
null,
DEFAULT_CONSTANT_LABELS,
DEFAULT_DEADLINE,
null);
}
/**
* Creates a Stackdriver Stats exporter with a custom Monitored Resource.
*
* <p>Only one Stackdriver exporter can be created.
*
* <p>Please refer to cloud.google.com/monitoring/custom-metrics/creating-metrics#which-resource
* for a list of valid {@code MonitoredResource}s.
*
* <p>This uses the default application credentials. See {@link
* GoogleCredentials#getApplicationDefault}.
*
* <p>This uses the default project ID configured see {@link ServiceOptions#getDefaultProjectId}.
*
* @param exportInterval the interval between pushing stats to StackDriver.
* @param monitoredResource the Monitored Resource used by exporter.
* @throws IllegalStateException if a Stackdriver exporter is already created.
* @deprecated in favor of {@link #createAndRegister(StackdriverStatsConfiguration)}.
* @since 0.10
*/
@Deprecated
public static void createAndRegisterWithMonitoredResource(
Duration exportInterval, MonitoredResource monitoredResource) throws IOException {
checkNotNull(exportInterval, "exportInterval");
checkNotNull(monitoredResource, "monitoredResource");
checkArgument(
!DEFAULT_PROJECT_ID.isEmpty(), "Cannot find a project ID from application default.");
createInternal(
null,
DEFAULT_PROJECT_ID,
exportInterval,
monitoredResource,
null,
null,
DEFAULT_CONSTANT_LABELS,
DEFAULT_DEADLINE,
null);
}
// Use createInternal() (instead of constructor) to enforce singleton.
private static void createInternal(
@Nullable Credentials credentials,
String projectId,
Duration exportInterval,
MonitoredResource monitoredResource,
@Nullable String metricNamePrefix,
@Nullable String displayNamePrefix,
Map<LabelKey, LabelValue> constantLabels,
Duration deadline,
@Nullable MetricServiceStub stub)
throws IOException {
synchronized (monitor) {
checkState(instance == null, "Stackdriver stats exporter is already created.");
MetricServiceClient client =
stub == null
? createMetricServiceClient(credentials, deadline)
: MetricServiceClient.create(stub);
instance =
new StackdriverStatsExporter(
projectId,
client,
exportInterval,
monitoredResource,
metricNamePrefix,
displayNamePrefix,
constantLabels);
}
}
// Initialize MetricServiceClient inside lock to avoid creating multiple clients.
@GuardedBy("monitor")
@VisibleForTesting
static MetricServiceClient createMetricServiceClient(
@Nullable Credentials credentials, Duration deadline) throws IOException {
MetricServiceSettings.Builder settingsBuilder =
MetricServiceSettings.newBuilder()
.setTransportChannelProvider(
InstantiatingGrpcChannelProvider.newBuilder()
.setHeaderProvider(OPENCENSUS_USER_AGENT_HEADER_PROVIDER)
.build());
if (credentials != null) {
settingsBuilder.setCredentialsProvider(FixedCredentialsProvider.create(credentials));
}
org.threeten.bp.Duration stackdriverDuration =
org.threeten.bp.Duration.ofMillis(deadline.toMillis());
// We use createMetricDescriptor and createTimeSeries APIs in this exporter.
settingsBuilder.createMetricDescriptorSettings().setSimpleTimeoutNoRetries(stackdriverDuration);
settingsBuilder.createTimeSeriesSettings().setSimpleTimeoutNoRetries(stackdriverDuration);
return MetricServiceClient.create(settingsBuilder.build());
}
/**
* Unregisters the {@link StackdriverStatsExporter} and stops metrics exporting.
*
* <p>Unexported data will be flushed before the exporter is stopped.
*
* @since 0.23
*/
public static void unregister() {
synchronized (monitor) {
if (instance != null) {
instance.intervalMetricReader.stop();
}
instance = null;
}
}
}