4

I’m struggling to create a chart with stackdriver monitoring with a log-based metric. My metric is a counter one with no unit by default.
Logs are available for my log-based metric but when I create a chart with my metric, it says no data is available for the....
Here’s my metric which does work (called isOperatorAllowed):

resource.type="container"
resource.labels.namespace_id="default"
jsonPayload.message="CaseForOperator flags"
logName="projects/PROJECT-ID/logs/app"
jsonPayload.caseForOperatorFlags.isOperatorAllowed=true

And here’s my JSON config of my chart

{
  "dataSets": [
    {
      "timeSeriesFilter": {
        "filter": "metric.type=\"logging.googleapis.com/user/IsOperatorAllowed\" resource.type=\"gke_container\"",
        "perSeriesAligner": "ALIGN_RATE",
        "crossSeriesReducer": "REDUCE_NONE",
        "secondaryCrossSeriesReducer": "REDUCE_NONE",
        "minAlignmentPeriod": "60s",
        "groupByFields": [],
        "unitOverride": "1"
      },
      "targetAxis": "Y1",
      "plotType": "LINE"
    }
  ],
  "options": {
    "mode": "COLOR"
  },
  "constantLines": [],
  "timeshiftDuration": "0s",
  "y1Axis": {
    "label": "y1Axis",
    "scale": "LINEAR"
  }
}

Does someone know what I’m doing wrong?

questionto42
  • 7,175
  • 4
  • 57
  • 90
DataHearth
  • 484
  • 6
  • 20
  • 1
    Please make sure the logs are generated after the creation of the logs-based metrics. As [the metrics are not populated with data from log entries that are already in Logging](https://cloud.google.com/logging/docs/logs-based-metrics/?hl=en_US&_ga=2.36465061.-42434130.1579798638). I would also recommend trying the GUI option by following [the documentation](https://cloud.google.com/logging/docs/logs-based-metrics/charts-and-alerts?hl=en_US#quick-view), this will help to narrow down the issue. – Mustafiz Mar 04 '20 at 15:11
  • @MussRahman The answer appears to be that logs created BEFORE the creation of the logs-based metric are not counted in the metric. I've found that and the bottom of the documentation. – DataHearth Mar 06 '20 at 14:52
  • glad to hear, my shared document helped. – Mustafiz Mar 06 '20 at 19:14

1 Answers1

5

After discussions and helps, the answer is:
the logs-based metric takes only logs after its creation. So if your logs are older than the metric creation timestamp, they won't appear in the metric count/distribution

Example:

Metric: `03/06/2020 15:55:33`
log 1: `02/06/2020 12:00:00`
log 2: `07/06/2020 12:00:00`

Metric count result: 1 (it will be the log 2)

Here what says the documentation:

New log entries might not match your metric's logs query. A logs-based metric gets data from matching log entries that are received after the metric is created. Logging does not backfill the metric from previous log entries.

DataHearth
  • 484
  • 6
  • 20