use of com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient in project java-monitoring by googleapis.
the class CreateMetricDescriptor method createMetricDescriptor.
public static void createMetricDescriptor(String projectId, String metricType) throws ApiException, IOException {
// once, and can be reused for multiple requests.
try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) {
ProjectName name = ProjectName.of(projectId);
// Define a metric type and its schema
MetricDescriptor descriptor = MetricDescriptor.newBuilder().setType(metricType).addLabels(LabelDescriptor.newBuilder().setKey("store_id").setValueType(LabelDescriptor.ValueType.STRING)).setDescription("This is a simple example of a custom metric.").setMetricKind(MetricDescriptor.MetricKind.GAUGE).setValueType(MetricDescriptor.ValueType.DOUBLE).build();
// Construct CreateMetricDescriptor request
CreateMetricDescriptorRequest request = CreateMetricDescriptorRequest.newBuilder().setName(name.toString()).setMetricDescriptor(descriptor).build();
// Send the request to create the metric descriptor
MetricDescriptor metricDescriptor = metricServiceClient.createMetricDescriptor(request);
// Process the response
System.out.println("metric descriptor created successfully: " + metricDescriptor.getName());
}
}
use of com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient in project java-monitoring by googleapis.
the class CreateTimeSeries method createTimeSeries.
public static void createTimeSeries(String projectId) throws ApiException, IOException {
// Instantiates a client
try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) {
// Prepares an individual data point
TimeInterval interval = TimeInterval.newBuilder().setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build();
TypedValue value = TypedValue.newBuilder().setDoubleValue(123.45).build();
Point point = Point.newBuilder().setInterval(interval).setValue(value).build();
List<Point> pointList = new ArrayList<>();
pointList.add(point);
// Prepares the metric descriptor
Map<String, String> metricLabels = new HashMap<>();
Metric metric = Metric.newBuilder().setType("custom.googleapis.com/my_metric").putAllLabels(metricLabels).build();
// Prepares the monitored resource descriptor
Map<String, String> resourceLabels = new HashMap<>();
resourceLabels.put("instance_id", "1234567890123456789");
resourceLabels.put("zone", "us-central1-f");
MonitoredResource resource = MonitoredResource.newBuilder().setType("gce_instance").putAllLabels(resourceLabels).build();
// Prepares the time series request
TimeSeries timeSeries = TimeSeries.newBuilder().setMetric(metric).setResource(resource).addAllPoints(pointList).build();
List<TimeSeries> timeSeriesList = new ArrayList<>();
timeSeriesList.add(timeSeries);
CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder().setName(ProjectName.of(projectId).toString()).addAllTimeSeries(timeSeriesList).build();
// Writes time series data
metricServiceClient.createTimeSeries(request);
System.out.println("Done writing time series value");
}
}
Aggregations