using System;
using System.Collections.Generic;
using Google.Cloud.Monitoring.V3;
using Google.Protobuf.WellKnownTypes;
using Google.Api;
using Google.Api.Gax.ResourceNames;
namespace GoogleCloudSamples
{
public class QuickStart
{
public static void Main(string[] args)
{
// Your Google Cloud Platform project ID.
string projectId = "YOUR-PROJECT-ID";
// Create client.
MetricServiceClient metricServiceClient = MetricServiceClient.Create();
// Initialize request argument(s).
ProjectName name = new ProjectName(projectId);
// Prepare a data point.
TypedValue salesTotal = new TypedValue
{
DoubleValue = 123.45
};
Point dataPoint = new Point
{
Value = salesTotal
};
// Sets data point's interval end time to current time.
DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
Timestamp timeStamp = new Timestamp
{
Seconds = (long)(DateTime.UtcNow - UnixEpoch).TotalSeconds
};
TimeInterval interval = new TimeInterval
{
EndTime = timeStamp
};
dataPoint.Interval = interval;
// Prepare custom metric.
Metric metric = new Metric
{
Type = "custom.googleapis.com/my_metric"
};
metric.Labels.Add("store_id", "Pittsburgh");
// Prepare monitored resource.
MonitoredResource resource = new MonitoredResource
{
Type = "gce_instance"
};
resource.Labels.Add("project_id", projectId);
resource.Labels.Add("instance_id", "1234567890123456789");
resource.Labels.Add("zone", "us-central1-f");
// Create a new time series using inputs.
TimeSeries timeSeriesData = new TimeSeries
{
Metric = metric,
Resource = resource
};
timeSeriesData.Points.Add(dataPoint);
// Add newly created time series to list of time series to be written.
IEnumerable<TimeSeries> timeSeries = new List<TimeSeries> { timeSeriesData };
// Write time series data.
metricServiceClient.CreateTimeSeries(name, timeSeries);
Console.WriteLine("Done writing time series data.");
}
}
}
import com.google.api.Metric;
import com.google.api.MonitoredResource;
import com.google.cloud.monitoring.v3.MetricServiceClient;
import com.google.monitoring.v3.CreateTimeSeriesRequest;
import com.google.monitoring.v3.Point;
import com.google.monitoring.v3.ProjectName;
import com.google.monitoring.v3.TimeInterval;
import com.google.monitoring.v3.TimeSeries;
import com.google.monitoring.v3.TypedValue;
import com.google.protobuf.util.Timestamps;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class QuickstartSample {
public static void main(String... args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
quickstart(projectId);
}
public static void quickstart(String projectId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
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);
ProjectName name = ProjectName.of(projectId);
// Prepares the metric descriptor
Map<String, String> metricLabels = new HashMap<String, String>();
metricLabels.put("store_id", "Pittsburg");
Metric metric =
Metric.newBuilder()
.setType("custom.googleapis.com/stores/daily_sales")
.putAllLabels(metricLabels)
.build();
// Prepares the monitored resource descriptor
Map<String, String> resourceLabels = new HashMap<String, String>();
resourceLabels.put("project_id", projectId);
MonitoredResource resource =
MonitoredResource.newBuilder().setType("global").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(name.toString())
.addAllTimeSeries(timeSeriesList)
.build();
// Writes time series data
metricServiceClient.createTimeSeries(request);
System.out.printf("Done writing time series data.%n");
}
}
}
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
async function quickstart() {
// Creates a client
const client = new monitoring.MetricServiceClient();
// TODO(developer): Uncomment and set the following variables
// const projectId = "PROJECT_ID"
// Prepares an individual data point
const dataPoint = {
interval: {
endTime: {
seconds: Date.now() / 1000,
},
},
value: {
// The amount of sales
doubleValue: 123.45,
},
};
// Prepares the time series request
const request = {
name: client.projectPath(projectId),
timeSeries: [
{
// Ties the data point to a custom metric
metric: {
type: 'custom.googleapis.com/stores/daily_sales',
labels: {
store_id: 'Pittsburgh',
},
},
resource: {
type: 'global',
labels: {
project_id: projectId,
},
},
points: [dataPoint],
},
],
};
// Writes time series data
const [result] = await client.createTimeSeries(request);
console.log('Done writing time series data.', result);
}
quickstart();
# Includes the autoloader for libraries installed with composer
require_once __DIR__ . '/vendor/autoload.php';
# Imports the Google Cloud client library
use Google\Api\Metric;
use Google\Api\MonitoredResource;
use Google\Cloud\Monitoring\V3\MetricServiceClient;
use Google\Cloud\Monitoring\V3\Point;
use Google\Cloud\Monitoring\V3\TimeInterval;
use Google\Cloud\Monitoring\V3\TimeSeries;
use Google\Cloud\Monitoring\V3\TypedValue;
use Google\Protobuf\Timestamp;
// These variables are set by the App Engine environment. To test locally,
// ensure these are set or manually change their values.
$projectId = getenv('GCLOUD_PROJECT') ?: 'YOUR_PROJECT_ID';
$instanceId = '1234567890123456789';
$zone = 'us-central1-f';
try {
$client = new MetricServiceClient();
$formattedProjectName = $client->projectName($projectId);
$labels = [
'instance_id' => $instanceId,
'zone' => $zone,
];
$m = new Metric();
$m->setType('custom.googleapis.com/my_metric');
$r = new MonitoredResource();
$r->setType('gce_instance');
$r->setLabels($labels);
$value = new TypedValue();
$value->setDoubleValue(3.14);
$timestamp = new Timestamp();
$timestamp->setSeconds(time());
$interval = new TimeInterval();
$interval->setStartTime($timestamp);
$interval->setEndTime($timestamp);
$point = new Point();
$point->setValue($value);
$point->setInterval($interval);
$points = [$point];
$timeSeries = new TimeSeries();
$timeSeries->setMetric($m);
$timeSeries->setResource($r);
$timeSeries->setPoints($points);
$client->createTimeSeries($formattedProjectName, [$timeSeries]);
print('Successfully submitted a time series' . PHP_EOL);
} finally {
$client->close();
}