1818
1919// [START monitoring_quickstart]
2020import com .google .api .Metric ;
21+ import com .google .api .MetricDescriptor ;
2122import com .google .api .MonitoredResource ;
2223
2324// Imports the Google Cloud client library
2425import com .google .cloud .monitoring .spi .v3 .MetricServiceClient ;
2526
27+ import com .google .monitoring .v3 .CreateMetricDescriptorRequest ;
2628import com .google .monitoring .v3 .CreateTimeSeriesRequest ;
2729import com .google .monitoring .v3 .Point ;
2830import com .google .monitoring .v3 .ProjectName ;
@@ -49,6 +51,17 @@ public static void main(String... args) throws Exception {
4951 // Instantiates a client
5052 MetricServiceClient metricServiceClient = MetricServiceClient .create ();
5153
54+ final String metricType = "custom.googleapis.com/stores/daily_sales" ;
55+
56+ // Create the metric descriptor
57+ MetricDescriptor descriptor = MetricDescriptor .newBuilder ()
58+ .setType (metricType )
59+ .setDescription ("This is a simple example of a custom metric." )
60+ .setMetricKind (MetricDescriptor .MetricKind .GAUGE )
61+ .setValueType (MetricDescriptor .ValueType .DOUBLE )
62+ .build ();
63+
64+
5265 // Prepares an individual data point
5366 TimeInterval interval = TimeInterval .newBuilder ()
5467 .setEndTime (Timestamps .fromMillis (System .currentTimeMillis ()))
@@ -66,7 +79,14 @@ public static void main(String... args) throws Exception {
6679
6780 ProjectName name = ProjectName .create (projectId );
6881
69- // Prepares the metric descriptor
82+ CreateMetricDescriptorRequest request = CreateMetricDescriptorRequest .newBuilder ()
83+ .setNameWithProjectName (name )
84+ .setMetricDescriptor (descriptor )
85+ .build ();
86+
87+ metricServiceClient .createMetricDescriptor (request );
88+
89+ // Prepares the metric
7090 Map <String , String > metricLabels = new HashMap <String , String >();
7191 metricLabels .put ("store_id" , "Pittsburg" );
7292 Metric metric = Metric .newBuilder ()
0 commit comments