Custom metrics exporter source code

Source code for custom Cloud Monitoring exporter.

Code sample

Go

To authenticate to GKE, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  func 
  
 exportMetric 
 ( 
 client 
  
 * 
 monitoring 
 . 
 MetricClient 
 , 
  
 metricName 
  
 string 
 , 
  
 metricValue 
  
 int64 
 , 
  
 metricLabels 
  
 map 
 [ 
 string 
 ] 
 string 
 , 
  
 monitoredResource 
  
 string 
 , 
  
 resourceLabels 
  
 map 
 [ 
 string 
 ] 
 string 
 ) 
  
 error 
  
 { 
  
 dataPoint 
  
 := 
  
& monitoringpb 
 . 
 Point 
 { 
  
 Interval 
 : 
  
& monitoringpb 
 . 
 TimeInterval 
 { 
  
 EndTime 
 : 
  
 timestamppb 
 . 
 New 
 ( 
 time 
 . 
 Now 
 ()), 
  
 }, 
  
 Value 
 : 
  
& monitoringpb 
 . 
 TypedValue 
 { 
  
 Value 
 : 
  
& monitoringpb 
 . 
 TypedValue_Int64Value 
 { 
 Int64Value 
 : 
  
 metricValue 
 }, 
  
 }, 
  
 } 
  
 // Write time series data. 
  
 projectName 
  
 := 
  
 fmt 
 . 
 Sprintf 
 ( 
 "projects/%s" 
 , 
  
 resourceLabels 
 [ 
 "project_id" 
 ]) 
  
 request 
  
 := 
  
& monitoringpb 
 . 
 CreateTimeSeriesRequest 
 { 
  
 Name 
 : 
  
 projectName 
 , 
  
 TimeSeries 
 : 
  
 [] 
 * 
 monitoringpb 
 . 
 TimeSeries 
 { 
  
 { 
  
 Metric 
 : 
  
& metric 
 . 
 Metric 
 { 
  
 Type 
 : 
  
 "custom.googleapis.com/" 
  
 + 
  
 metricName 
 , 
  
 Labels 
 : 
  
 metricLabels 
 , 
  
 }, 
  
 Resource 
 : 
  
& monitoredres 
 . 
 MonitoredResource 
 { 
  
 Type 
 : 
  
 monitoredResource 
 , 
  
 Labels 
 : 
  
 resourceLabels 
 , 
  
 }, 
  
 Points 
 : 
  
 [] 
 * 
 monitoringpb 
 . 
 Point 
 { 
  
 dataPoint 
 , 
  
 }, 
  
 }, 
  
 }, 
  
 } 
  
 err 
  
 := 
  
 client 
 . 
 CreateTimeSeries 
 ( 
 context 
 . 
 Background 
 (), 
  
 request 
 ) 
  
 return 
  
 err 
 } 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Design a Mobile Site
View Site in Mobile | Classic
Share by: