I am trying to launch a basic function on google cloud build that will count the number of users every time a new user is added to a firestore database. Here is a basic layout of my code
import time
from cloudevents.http import CloudEvent
import functions_framework
from google.cloud import monitoring_v3
from firebase_admin import db
from google.api import metric_pb2 as ga_metric
@functions_framework.cloud_event
my_function(cloud_event: CloudEvent):
db = firestore.client()
users_ref = db.collection('Users')
user_count = len(list(users_ref.stream()))
descriptor = ga_metric.MetricDescriptor()
descriptor.type = "custom.googleapis.com/firestore/total_user_count"
descriptor.metric_kind = ga_metric.MetricDescriptor.MetricKind.GAUGE
descriptor.value_type = ga_metric.MetricDescriptor.ValueType.INT64
descriptor = client.create_metric_descriptor(name=project_name, metric_descriptor=descriptor)
series = monitoring_v3.TimeSeries()
series.metric.type = "custom.googleapis.com/total_user_count"
series.resource.type = "global"
series.metric.labels["TestLabel"] = Total User Count
now = time.time()
seconds = int(now)
nanos = int((now - seconds) * 10**9)
interval = monitoring_v3.TimeInterval(
{"end_time": {"seconds": seconds, "nanos": nanos}}
)
point = monitoring_v3.Point({"interval": interval, "value": {"double_value": user_count}})
series.points = [point]
client.create_time_series(name=project_name, time_series=[series])
record_change_metric("new_user_count", 1)
I have been having some issues so far with trying to actually deploy it though. I keep getting an issue that says "The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout. This can happen when the container port is misconfigured or if the timeout is too short. The health check timeout can be extended. Logs for this revision might contain more information." and when I go to check the logs of what is happening I just find the error I mentioned above with no other context of what's causing the issue. For context, my requirements.txt file looks like this
firebase-admin
google-cloud-monitoring
google-api-core
functions-framework
cloudevents
google-events
and I am running the gen2 cloud build on runtie python312. Any help would be appreaciated!
I have tried different things like removing the specific version of each import from the requirements file as some other sources aid, but it didn't really work.