MQL 查询中没有数据

问题描述 投票:0回答:1

我创建了一个执行数据库查询并返回计数的 Google Cloud 函数:

import psycopg2

def my_function(request):
  """Runs a custom SQL query on a Cloud SQL database and returns the result."""

  # Connect to the Cloud SQL database.
  conn = psycopg2.connect(host='<YOUR_DATABASE_HOST>', database='<YOUR_DATABASE_NAME>', user='<YOUR_DATABASE_USER>', password='<YOUR_DATABASE_PASSWORD>')

  # Create a cursor.
  cur = conn.cursor()

  # Run the custom SQL query.
  cur.execute('SELECT count(*) FROM <YOUR_TABLE_NAME> WHERE <YOUR_FIELD> IS NULL OR LTRIM(RTRIM(<YOUR_FIELD>)) = ''')

  # Fetch the result of the query.
  result = str(cur.fetchone()[0])

  # Close the cursor and connection.
  cur.close()
  conn.close()
  print("Some text:" + result)

  # Return the result of the query.
  return result

我测试了它,工作正常,返回正确的值。然后我创建了一个基于日志的分布类型度量,并使用过滤器:

resource.type="type" 
resource.labels.function_name="function" 
resource.labels.region="region"
severity=DEFAULT
"Some text"

textPayload 字段上的正则表达式仅提取值:

\b(\d{8})\d

然后我尝试使用 MQL 查询设置警报:

fetch k8s_container::logging.googleapis.com/user/Name-Of-Metric
| align delta(5m)
| group_by [], [value_Collector_mean: mean(value.Metric)]

如果我运行查询,我会收到消息:

所选时间范围内没有可用数据

如果我使用在日志资源管理器中基于日志的指标中设置的过滤器,我会收到正确的输入。这些是我多次使用功能测试选项生成的。我想设置一个调度程序每 2 小时运行一次此函数,如果该值增加,那么我想收到警报。

如果我可以在日志浏览器中清楚地看到数据,为什么它会说所选时间范围内没有可用数据?

google-cloud-platform alert monitoring
1个回答
0
投票

根据文档和您获得的结果,指标计数可能会延迟。尽管日志浏览器中出现了可计数的日志条目,但更新 Cloud Monitoring 中基于日志的指标可能最多需要 10 分钟。附件是用于对基于日志的指标进行故障排除的链接,可为您的用例提供帮助。[1]

[1]

https://cloud.google.com/logging/docs/logs-based-metrics/troubleshooting#slow-startup

© www.soinside.com 2019 - 2024. All rights reserved.