В Python Я записываю результаты запросов (показывает, сколько запросов выполняется в рамках проекта) в таблицу, которая после вставки будет принимать введенные значения и отправлять их в Stackdriver в виде пользовательского временного ряда, метри c , При создании временных рядов возвращается сообщение об ошибке google.api_core.exceptions.InvalidArgument: 400 One or more TimeSeries could not be written: Metrics cannot be written to bigquery_project. See https://cloud.google.com/monitoring/custom-metrics/creating-metrics#which-resource for a list of writable resource types.: timeSeries[0].
Используемый мной тип ресурса - bigquery_project
, но я не верю, что это является причиной моих ошибок. Я думаю, что в одной точке слишком много значений для каждого запроса (например, ответ на запрос возвращает 20 запросов в 12: 00), что вызывает ошибку, но если это является причиной, как мне отложить время для каждого ответа на запрос.
Вот код:
def get_time_series(result):
series = monitoring_v3.types.TimeSeries()
series.metric.type = 'custom.googleapis.com/Blah/Blah_blah_blah'
series.resource.type = 'bigquery_project'
series.resource.labels['project_id'] = "project-name"
series.resource.labels['region'] = 'us-central1'
point = series.points.add()
point.interval.end_time.seconds = int(time.time())
try:
point.value.int64_value = result
except TypeError as type_err:
print(f"Something went wrong ..")
now = datetime.datetime.utcnow()
point.value.int64_value = int(
(now - now.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() * 1000)
return series
def query_stackoverflow():
client_bq = bigquery.Client(project="project-name")
client = monitoring_v3.MetricServiceClient()
query_job = client_bq.query("""
SELECT
job_id,
creation_time,
query,
total_bytes_processed
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE project_id ='project-name'
AND creation_time BETWEEN TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 3 DAY)
AND CURRENT_TIMESTAMP()
ORDER BY creation_time DESC
LIMIT 100""")
results = query_job.result() # Waits for job to complete.
rows = list(results)
response =[]
for row in rows:
if row.total_bytes_processed is not None:
cost_dollars = (row.total_bytes_processed / 1024 ** 4) * 5
response.append(f"Creation_Time :{row.creation_time} | Estimated_Cost : {cost_dollars}".format(row.creation_time, cost_dollars))
else:
cost_dollars = (row.total_bytes_processed / 1024 ** 4) * 5
response.append(f"Creation_Time :{row.creation_time} | Estimated_Cost : {cost_dollars}".format(row.creation_time, cost_dollars))
# print(response)
timeseries = []
for x in range(0, len(list(response))):
timeseries.append(get_time_series(response))
print(timeseries)
client.create_time_series("projects/project-name", timeseries)
print("Blah metrics are successfully sent")
print("FINISHED")
StackTrace:
Traceback (most recent call last):
File "C:\Users\MalCode\PycharmProjects\Vulnerabilities\venv\lib\site-packages\google\api_core\grpc_helpers.py", line 57, in error_remapped_callable
return callable_(*args, **kwargs)
File "C:\Users\MalCode\PycharmProjects\Vulnerabilities\venv\lib\site-packages\grpc\_channel.py", line 690, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "C:\Users\MalCode\PycharmProjects\Vulnerabilities\venv\lib\site-packages\grpc\_channel.py", line 592, in _end_unary_response_blocking
raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
status = StatusCode.INVALID_ARGUMENT
details = "One or more TimeSeries could not be written: Metrics cannot be written to bigquery_project. See https://cloud.google.com/monitoring/custom-metrics/creating-metrics#which-resource for a list of writable resource types.: timeSeries[0]"
debug_error_string = "{"created":"@1585841627.232000000","description":"Error received from peer ipv4:172.217.10.138:443","file":"src/core/lib/surface/call.cc","file_line":1055,"grpc_message":"One or more TimeSeries could not be written: Metrics cannot be written to bigquery_project. See https://cloud.google.com/monitoring/custom-metrics/creating-metrics#which-resource for a list of writable resource types.: timeSeries[0]","grpc_status":3}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/Users/MalCode/PycharmProjects/Vulnerabilities/test.py", line 83, in <module>
query_stackoverflow()
File "C:/Users/MalCode/PycharmProjects/Vulnerabilities/test.py", line 76, in query_stackoverflow
client.create_time_series("projects/project-name", timeseries)
File "C:\Users\MalCode\PycharmProjects\Vulnerabilities\venv\lib\site-packages\google\cloud\monitoring_v3\gapic\metric_service_client.py", line 1039, in create_time_series
request, retry=retry, timeout=timeout, metadata=metadata
File "C:\Users\MalCode\PycharmProjects\Vulnerabilities\venv\lib\site-packages\google\api_core\gapic_v1\method.py", line 143, in __call__
return wrapped_func(*args, **kwargs)
File "C:\Users\MalCode\PycharmProjects\Vulnerabilities\venv\lib\site-packages\google\api_core\retry.py", line 286, in retry_wrapped_func
on_error=on_error,
File "C:\Users\MalCode\PycharmProjects\Vulnerabilities\venv\lib\site-packages\google\api_core\retry.py", line 184, in retry_target
return target()
File "C:\Users\MalCode\PycharmProjects\Vulnerabilities\venv\lib\site-packages\google\api_core\timeout.py", line 214, in func_with_timeout
return func(*args, **kwargs)
File "C:\Users\MalCode\PycharmProjects\Vulnerabilities\venv\lib\site-packages\google\api_core\grpc_helpers.py", line 59, in error_remapped_callable
six.raise_from(exceptions.from_grpc_error(exc), exc)
File "<string>", line 3, in raise_from
google.api_core.exceptions.InvalidArgument: 400 One or more TimeSeries could not be written: Metrics cannot be written to bigquery_project. See https://cloud.google.com/monitoring/custom-metrics/creating-metrics#which-resource for a list of writable resource types.: timeSeries[0]