google-cloud-node icon indicating copy to clipboard operation
google-cloud-node copied to clipboard

[google-cloud-monitoring] cannot create log-based alerting policy

Open cjroebuck opened this issue 1 year ago • 3 comments

Environment details

  • which product (packages/*): google-cloud-monitoring
  • OS: mac / not relevant
  • Node.js version: 18
  • npm version: 9.5.1
  • google-cloud-node version: 3.0.4

Steps to reproduce

const createAlertInGcp = async () => {
  const alertPolicy: google.monitoring.v3.AlertPolicy = {
    displayName: "Timed out (api)",
    documentation: {
      content:
        "${log.extracted_label.user}'s request timed out.\n\nIt happened in ${resource.labels.cluster_name}. The url was ${log.extracted_label.url}",
      mimeType: "text/markdown",
    },
    conditions: [
      {
        displayName: "Timed out (api)",
        conditionMatchedLog: {
          filter:
            'resource.type="k8s_container"\nresource.labels.namespace_name="default"\nresource.labels.container_name="engine"\njsonPayload.message=~"Timed out"\njsonPayload.msg="Returning a 500 error"',
          labelExtractors: {
            user: "EXTRACT(jsonPayload.options.user)",
            url: "EXTRACT(jsonPayload.options.url)",
          },
        },
      },
    ],
    alertStrategy: {
      notificationRateLimit: {
        period: "3600s",
      },
      autoClose: "1800s",
    },
    combiner: "OR",
    enabled: { value: true },
    notificationChannels: [
      "projects/project/notificationChannels/1209803...",
    ],
  };
  const alertPolicyRequest: google.monitoring.v3.ICreateAlertPolicyRequest = {
    name: client.projectPath(projectId),
    alertPolicy,
  };
  // client.create
  let res = await client.createAlertPolicy(alertPolicyRequest);
  console.log(res);

The error in the response is:

'Field alert_policy.conditions[0].condition_case had an invalid value of "0": Condition subtype (e.g. "condition_absent","condition_threshold", etc.) is absent or unknown.',

So it seems that the conditionMatchedLog subtype is not supported yet by the nodejs client, only the metric-based alerts seem to be supported.

Following this: https://cloud.google.com/monitoring/alerts/policies-in-api#log-polices

and this: https://cloud.google.com/logging/docs/alerting/log-based-alerts#lba-api-create

it does seem to be supported through the API using the gcloud cli.

cjroebuck avatar Jun 19 '23 13:06 cjroebuck

Having the exact same issue. Are there any updates?

Im using google-cloud-node version: 4.0.0

bayerlse avatar Jan 09 '24 17:01 bayerlse

Have the exact same issue today. Any update? Is it simply not possible currently to create a conditionMatchedLog alert via the NodeJS API ?

I tried getting the JSON from an existing alert, using the API to delete that alert, and then recreate it (removing the fields name, creationRecord, mutationRecord just like the example docs indicate). The existing alert is deleted, but the exact same error as the OP occurs when it tries to create the alert.

Calling CREATE with: {
  name: 'projects/sales-demo-1',
  alertPolicy: {
    alertStrategy: {
      autoClose: '604800s',
      notificationRateLimit: { period: '3600s' }
    },
    combiner: 'OR',
    conditions: [
      {
        displayName: 'Log match condition',
        conditionMatchedLog: { filter: 'textPayload=~"Function execution took ((?:\\d[,.]?)*\\d) ms, finished with status: ' }
      }
    ],
    displayName: 'SALES-DEMO-1 Non-OK Cloud Functions',
    notificationChannels: [
      'projects/sales-demo-1/notificationChannels/4444444444444444444',
      'projects/sales-demo-1/notificationChannels/7666666666666666665',
      'projects/sales-demo-1/notificationChannels/7888888888888888886'
    ]
  }
}
node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

Error: 3 INVALID_ARGUMENT: Field alert_policy.conditions[0].condition_case had an invalid value of "0": Condition subtype (e.g. "condition_absent","condition_threshold", etc.) is absent or unknown.
    at callErrorFromStatus (/Users/greg/work/prod/tick8s/node_modules/@google-cloud/monitoring/node_modules/@grpc/grpc-js/build/src/call.js:31:19)
    at Object.onReceiveStatus (/Users/greg/work/prod/tick8s/node_modules/@google-cloud/monitoring/node_modules/@grpc/grpc-js/build/src/client.js:192:76)
    at Object.onReceiveStatus (/Users/greg/work/prod/tick8s/node_modules/@google-cloud/monitoring/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:360:141)
    at Object.onReceiveStatus (/Users/greg/work/prod/tick8s/node_modules/@google-cloud/monitoring/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:323:181)
    at /Users/greg/work/prod/tick8s/node_modules/@google-cloud/monitoring/node_modules/@grpc/grpc-js/build/src/resolving-call.js:99:78
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
for call at
    at ServiceClientImpl.makeUnaryRequest (/Users/greg/work/prod/tick8s/node_modules/@google-cloud/monitoring/node_modules/@grpc/grpc-js/build/src/client.js:160:32)
    at ServiceClientImpl.<anonymous> (/Users/greg/work/prod/tick8s/node_modules/@google-cloud/monitoring/node_modules/@grpc/grpc-js/build/src/make-client.js:105:19)
    at /Users/greg/work/prod/tick8s/node_modules/@google-cloud/monitoring/build/src/v3/alert_policy_service_client.js:213:29
    at /Users/greg/work/prod/tick8s/node_modules/@google-cloud/monitoring/node_modules/google-gax/build/src/normalCalls/timeout.js:44:16
    at OngoingCallPromise.call (/Users/greg/work/prod/tick8s/node_modules/@google-cloud/monitoring/node_modules/google-gax/build/src/call.js:67:27)
    at NormalApiCaller.call (/Users/greg/work/prod/tick8s/node_modules/@google-cloud/monitoring/node_modules/google-gax/build/src/normalCalls/normalApiCaller.js:34:19)
    at /Users/greg/work/prod/tick8s/node_modules/@google-cloud/monitoring/node_modules/google-gax/build/src/createApiCall.js:108:30
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 3,
  details: 'Field alert_policy.conditions[0].condition_case had an invalid value of "0": Condition subtype (e.g. "condition_absent","condition_threshold", etc.) is absent or unknown.',
  metadata: Metadata {
    internalRepr: Map(2) {
      'endpoint-load-metrics-bin' => [
        Buffer(27) [Uint8Array] [
           49, 214, 249, 125, 211,  84, 208,
          165,  64,  57, 116, 219, 194,  17,
          248,  31, 154,  64,  73,  27, 157,
           66, 238, 200, 100, 233,  63
        ]
      ],
      'grpc-server-stats-bin' => [
        Buffer(10) [Uint8Array] [
          0, 0, 49, 58, 157,
          9, 0,  0,  0,   0
        ]
      ]
    },
    options: {}
  }
}

Node.js v18.17.1

The code for this is available here, though it is just a tweaked version of an example from the docs.

gregfenton avatar Feb 10 '24 05:02 gregfenton

Note: that same JS definition works as JSON when using

gcloud alpha monitoring policies create --policy-from-file ./test.json

gregfenton avatar Feb 10 '24 06:02 gregfenton