1616
1717package io .opencensus .exporter .stats .stackdriver ;
1818
19- import static com .google .api . client . util .Preconditions .checkArgument ;
20- import static com .google .api . client . util .Preconditions .checkNotNull ;
19+ import static com .google .common . base .Preconditions .checkArgument ;
20+ import static com .google .common . base .Preconditions .checkNotNull ;
2121import static com .google .common .base .Preconditions .checkState ;
2222
23+ import com .google .api .MonitoredResource ;
2324import com .google .api .gax .core .FixedCredentialsProvider ;
2425import com .google .auth .Credentials ;
2526import com .google .auth .oauth2 .GoogleCredentials ;
@@ -57,21 +58,25 @@ public final class StackdriverStatsExporter {
5758 private static StackdriverStatsExporter exporter = null ;
5859
5960 private static final Duration ZERO = Duration .create (0 , 0 );
61+ private static final MonitoredResource DEFAULT_RESOURCE =
62+ MonitoredResource .newBuilder ().setType ("global" ).build ();
6063
6164 @ VisibleForTesting
6265 StackdriverStatsExporter (
6366 String projectId ,
6467 MetricServiceClient metricServiceClient ,
6568 Duration exportInterval ,
66- ViewManager viewManager ) {
69+ ViewManager viewManager ,
70+ MonitoredResource monitoredResource ) {
6771 checkArgument (exportInterval .compareTo (ZERO ) > 0 , "Duration must be positive" );
6872 this .workerThread =
6973 new StackdriverExporterWorkerThread (
70- projectId , metricServiceClient , exportInterval , viewManager );
74+ projectId , metricServiceClient , exportInterval , viewManager , monitoredResource );
7175 }
7276
7377 /**
74- * Creates a StackdriverStatsExporter for an explicit project ID and using explicit credentials.
78+ * Creates a StackdriverStatsExporter for an explicit project ID and using explicit credentials,
79+ * with default Monitored Resource.
7580 *
7681 * <p>Only one Stackdriver exporter can be created.
7782 *
@@ -85,11 +90,12 @@ public static void createAndRegisterWithCredentialsAndProjectId(
8590 checkNotNull (credentials , "credentials" );
8691 checkNotNull (projectId , "projectId" );
8792 checkNotNull (exportInterval , "exportInterval" );
88- createInternal (credentials , projectId , exportInterval );
93+ createInternal (credentials , projectId , exportInterval , null );
8994 }
9095
9196 /**
92- * Creates a Stackdriver Stats exporter for an explicit project ID.
97+ * Creates a Stackdriver Stats exporter for an explicit project ID, with default Monitored
98+ * Resource.
9399 *
94100 * <p>Only one Stackdriver exporter can be created.
95101 *
@@ -111,11 +117,11 @@ public static void createAndRegisterWithProjectId(String projectId, Duration exp
111117 throws IOException {
112118 checkNotNull (projectId , "projectId" );
113119 checkNotNull (exportInterval , "exportInterval" );
114- createInternal (null , projectId , exportInterval );
120+ createInternal (null , projectId , exportInterval , null );
115121 }
116122
117123 /**
118- * Creates a Stackdriver Stats exporter.
124+ * Creates a Stackdriver Stats exporter with default Monitored Resource .
119125 *
120126 * <p>Only one Stackdriver exporter can be created.
121127 *
@@ -135,12 +141,65 @@ public static void createAndRegisterWithProjectId(String projectId, Duration exp
135141 */
136142 public static void createAndRegister (Duration exportInterval ) throws IOException {
137143 checkNotNull (exportInterval , "exportInterval" );
138- createInternal (null , ServiceOptions .getDefaultProjectId (), exportInterval );
144+ createInternal (null , ServiceOptions .getDefaultProjectId (), exportInterval , null );
145+ }
146+
147+ /**
148+ * Creates a Stackdriver Stats exporter with an explicit project ID and a custom Monitored
149+ * Resource.
150+ *
151+ * <p>Only one Stackdriver exporter can be created.
152+ *
153+ * <p>Please refer to cloud.google.com/monitoring/custom-metrics/creating-metrics#which-resource
154+ * for a list of valid {@code MonitoredResource}s.
155+ *
156+ * <p>This uses the default application credentials. See {@link
157+ * GoogleCredentials#getApplicationDefault}.
158+ *
159+ * @param projectId the cloud project id.
160+ * @param exportInterval the interval between pushing stats to StackDriver.
161+ * @param monitoredResource the Monitored Resource used by exporter.
162+ * @throws IllegalStateException if a Stackdriver exporter is already created.
163+ */
164+ public static void createAndRegisterWithProjectIdAndMonitoredResource (
165+ String projectId , Duration exportInterval , MonitoredResource monitoredResource )
166+ throws IOException {
167+ checkNotNull (projectId , "projectId" );
168+ checkNotNull (exportInterval , "exportInterval" );
169+ checkNotNull (monitoredResource , "monitoredResource" );
170+ createInternal (null , projectId , exportInterval , monitoredResource );
171+ }
172+
173+ /**
174+ * Creates a Stackdriver Stats exporter with a custom Monitored Resource.
175+ *
176+ * <p>Only one Stackdriver exporter can be created.
177+ *
178+ * <p>Please refer to cloud.google.com/monitoring/custom-metrics/creating-metrics#which-resource
179+ * for a list of valid {@code MonitoredResource}s.
180+ *
181+ * <p>This uses the default application credentials. See {@link
182+ * GoogleCredentials#getApplicationDefault}.
183+ *
184+ * <p>This uses the default project ID configured see {@link ServiceOptions#getDefaultProjectId}.
185+ *
186+ * @param exportInterval the interval between pushing stats to StackDriver.
187+ * @param monitoredResource the Monitored Resource used by exporter.
188+ * @throws IllegalStateException if a Stackdriver exporter is already created.
189+ */
190+ public static void createAndRegisterWithMonitoredResource (
191+ Duration exportInterval , MonitoredResource monitoredResource ) throws IOException {
192+ checkNotNull (exportInterval , "exportInterval" );
193+ checkNotNull (monitoredResource , "monitoredResource" );
194+ createInternal (null , ServiceOptions .getDefaultProjectId (), exportInterval , monitoredResource );
139195 }
140196
141197 // Use createInternal() (instead of constructor) to enforce singleton.
142198 private static void createInternal (
143- @ Nullable Credentials credentials , String projectId , Duration exportInterval )
199+ @ Nullable Credentials credentials ,
200+ String projectId ,
201+ Duration exportInterval ,
202+ @ Nullable MonitoredResource monitoredResource )
144203 throws IOException {
145204 synchronized (monitor ) {
146205 checkState (exporter == null , "Stackdriver stats exporter is already created." );
@@ -155,9 +214,16 @@ private static void createInternal(
155214 .setCredentialsProvider (FixedCredentialsProvider .create (credentials ))
156215 .build ());
157216 }
217+ if (monitoredResource == null ) {
218+ monitoredResource = DEFAULT_RESOURCE ;
219+ }
158220 exporter =
159221 new StackdriverStatsExporter (
160- projectId , metricServiceClient , exportInterval , Stats .getViewManager ());
222+ projectId ,
223+ metricServiceClient ,
224+ exportInterval ,
225+ Stats .getViewManager (),
226+ monitoredResource );
161227 exporter .workerThread .start ();
162228 }
163229 }
0 commit comments