18
18
19
19
// [START monitoring_quickstart]
20
20
import com .google .api .Metric ;
21
+ import com .google .api .MetricDescriptor ;
21
22
import com .google .api .MonitoredResource ;
22
23
23
24
// Imports the Google Cloud client library
24
25
import com .google .cloud .monitoring .spi .v3 .MetricServiceClient ;
25
26
27
+ import com .google .monitoring .v3 .CreateMetricDescriptorRequest ;
26
28
import com .google .monitoring .v3 .CreateTimeSeriesRequest ;
27
29
import com .google .monitoring .v3 .Point ;
28
30
import com .google .monitoring .v3 .ProjectName ;
@@ -49,6 +51,17 @@ public static void main(String... args) throws Exception {
49
51
// Instantiates a client
50
52
MetricServiceClient metricServiceClient = MetricServiceClient .create ();
51
53
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
+
52
65
// Prepares an individual data point
53
66
TimeInterval interval = TimeInterval .newBuilder ()
54
67
.setEndTime (Timestamps .fromMillis (System .currentTimeMillis ()))
@@ -66,7 +79,14 @@ public static void main(String... args) throws Exception {
66
79
67
80
ProjectName name = ProjectName .create (projectId );
68
81
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
70
90
Map <String , String > metricLabels = new HashMap <String , String >();
71
91
metricLabels .put ("store_id" , "Pittsburg" );
72
92
Metric metric = Metric .newBuilder ()
0 commit comments