Use case: I'd like to build a chart of number of requests grouped by x-api-key, using all requests to a Google Cloud Run service (they all have a x-api-key header).
1. Is it something that's doable out of the box, I can't see to find any way to aggregate by anything else than route, response status and such.
2. If not, is it something that can be done if we enriched the information from the service?
Hi @Nlassaux,
Welcome to Google Cloud Community!
This approach lets you track and visualize requests by API key without directly exposing sensitive data in your logs:
Create a Log-Based Metric in Cloud Monitoring
Go to the Cloud Monitoring console in GCP and open the "Logs explorer".
Before creating a log-based metric, try to filter the logs and run a query.
Next, Click “Action” then "Create Metric" and choose "Create log-based metric”
Fill Out the log-based metric
If you would like to use filter that targets your Cloud Run service’s logs and extract X API key here is the sample query command:
resource.type="cloud_run_sample"
logName="projects/[YOUR_PROJECT_ID]/logs/cloudrun.googleapis.com%2Frequests"
jsonPayload.message:"Request received. X API Key:"
Build Your Chart
For this, click on the “Save Chart” button but make sure the it was link to created “log-based metric”
Your saved chart will appear in Monitoring Dashboards. To add more charts with additional logs or data, click “Add Widget”.
Note: Never log raw API keys. Always hash them using a strong, one-way hashing function like SHA256. You can apply similar log-based metrics to visualize data from other custom headers or log entries.
For your reference here is Creating log-based metrics and dashboards.
I hope the above information is helpful.