I have several GCP project that are all monitored by one central project. I want to add a Log-based metric to each of the sub-projects. The structure of the metric is identical across all the projects, with the exception that the name of the project is appended to the name of the metric. To do this, I have a Python Script that can use the GCP API to create the Metric. The script, however, requires the JSON description of the Metric. I cannot, however, find out where to export a metric's JSON. Does anyone know how to export, fetch, or GET a metric in JSON form? I have attempted to use the API call outlined here, but it lacks the filter and lableExtractors fields for the metric.
Asked
Active
Viewed 441 times
0
-
Please include code and more details.Are you trying to understand how to structure the metric descriptor for your log-based metric? If so see [Configure counter (log-based) metrics](https://cloud.google.com/logging/docs/logs-based-metrics/counter-metrics#api) and [Configure distribution (log-based) metrics](https://cloud.google.com/logging/docs/logs-based-metrics/distribution-metrics#api) – DazWilkin Oct 13 '22 at 22:08
-
... or the [`projects.metrics.create`](https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/create) method which links to [MetricDescriptor](https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics#MetricDescriptor) type. – DazWilkin Oct 13 '22 at 22:08
-
Google provides libraries for all its services, e.g. [Python Client for Cloud Logging](https://googleapis.dev/python/logging/latest/index.html) and this includes [`google-cloud-logging`](https://pypi.org/project/google-cloud-logging/) and this supports [Metrics](https://googleapis.dev/python/logging/latest/metric.html) albeit the methods are somewhat confusing. Is that an issue? – DazWilkin Oct 13 '22 at 22:19
-
1I would encourage you to consider including the Project (ID) as a label (if possible?) rather than part of the name as this will enable you to more homogeneously compose measurements. – DazWilkin Oct 13 '22 at 22:23
-
The code I have for making the metric via the API is fine. That is not the issue. I have a single instance of the Metric that I want duplicated across all the projects. I also have a script that will automatically create a metric in several projects, as long as it has the a JSON to base the metric on. What I am looking for is a way to get the JSON of the existing metric to use as the template. – AKASynthose Oct 14 '22 at 14:59
-
It would be better for you to replicate the creation of the log-based metric using the script across Projects. Since the script creates a log-based metric in one Project and you want to create the log-based metric in another Project, you should just reuse it. – DazWilkin Oct 14 '22 at 16:29
-
1However, if you need to `GET` the metric, since it was created as a log-based metric, you can either use the Logging service to retrieve it i.e. Logging [`projects.metrics.get`](https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/get) returns a `LogMetric` including the `MetricDescriptor` **or** Cloud Monitoring [`projects.metricDescriptors.get`](https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors/get) which returns the `MetricDescriptor`. The former should include everything. – DazWilkin Oct 14 '22 at 16:30
-
@DazWilkin Projects.metrics.get was exactly what I was looking for! Thank you! – AKASynthose Oct 14 '22 at 16:39
1 Answers
1
Since it was created as a log-based metric, you can:
- either use the Logging service to retrieve it i.e. Logging
projects.metrics.get
which returns aLogMetric
including theMetricDescriptor
- or Cloud Monitoring
projects.metricDescriptors.get
which returns theMetricDescriptor
.

DazWilkin
- 32,823
- 5
- 47
- 88