Cloud Function Metrics

Overview

This document provides a detailed walkthrough on how to send Google Cloud Functions metrics to SigNoz. By the end of this guide, you will have a setup that sends your Cloud Function metrics to SigNoz.

Here's a quick summary of what we will be doing in this guide

  • Create and configure a Cloud Function to generate the metrics.
  • Invoke the Cloud Function.
  • Deploy Telegraf to fetch the metrics from Google Cloud monitoring.
  • Deploy OpenTelemetry to scrape the metrics from Telegraf.
  • Send and Visualize the metrics obtained by OpenTelemetry in SigNoz

Prerequisites

  • A Google Cloud account with administrative privileges or Cloud Functions Admin privileges.
  • A SigNoz Cloud account (used for this demonstration). You'll need the ingestion details. To obtain your Ingestion Key and Ingestion URL, log in to your SigNoz Cloud account and navigate to Settings >> Ingestion Settings.
  • Access to a project in Google Cloud Platform (GCP).
  • The Google Cloud Functions APIs must be enabled. You can follow this guide to learn how to enable an API in Google Cloud.

Setup

Get started with Cloud Function Configuration

Follow these steps to create the Cloud Function:

Step 1: Go to your GCP console and search for Cloud Functions, go to Functions, and click on CREATE FUNCTION.

Create Cloud Function

Create Cloud Functions

Step 2: Enter the following information to create a Cloud Function:

  1. Environment: Select "2nd gen."
  2. Function name: Enter a name for your Cloud Function.
  3. Region: The default region of your GCP account will be used.
  4. Trigger: Specify how the Cloud Function will be triggered.
  5. Trigger Type: Select "HTTPS" to enable triggering the Cloud Function via a URL.
  6. Authentication: Decide if the invocation needs to be authenticated or unauthenticated. For this demonstration, we have chosen unauthenticated invocation.
Configure Cloud Function

Configure Cloud Function

Step 3: Click on the NEXT button, which will bring us to the page where we can add our code. Here we are using a simple python Hello World! script.

Set Entrypoint and source code

Set entrypoint and source code

Add code to the Google Cloud Function

For this demonstration, we will be using python 3.11.

Here is the complete code for the main.py file, along with a summary of its functionality.

import functions_framework

@functions_framework.http
def hello_http(request):
    """HTTP Cloud Function.
    Args:
        request (flask.Request): The request object.
        <https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
    Returns:
        The response text, or any set of values that can be turned into a
        Response object using `make_response`
        <https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
    """
    request_json = request.get_json(silent=True)
    request_args = request.args

    if request_json and 'name' in request_json:
        name = request_json['name']
    elif request_args and 'name' in request_args:
        name = request_args['name']
    else:
        name = 'World'
    return 'Hello {}!'.format(name)

  1. Import the Framework: The functions_framework module is imported to handle HTTP requests.

  2. Define the Function: The hello_http function is defined to process HTTP requests. This function is triggered whenever the Cloud Function receives an HTTP request.

  3. Handle the Request:

    • The function checks if the incoming request contains JSON data (request_json).
    • It also checks for URL query parameters (request_args).
  4. Determine the Name to Greet:

    • If the JSON body contains a key name, it uses its value.
    • If the query parameters contain a key name, it uses its value.
    • If neither is provided, it defaults to 'World'.
  5. Return the Response:

    • The function returns a greeting message: 'Hello {name}!', where '{name}'' is replaced with the determined name.

Testing your cloud function

Step 1: After completing the deployment, navigate to the TRIGGER section to obtain the URL to invoke the function.

Deploying Function

Deploying Cloud Function

Step 2: Hit the URL that you have obtained, and you should see the function output.

Cloud Function URL

Trigger Cloud function.

Deploy Telegraf to fetch the metrics from Google Cloud Monitoring.

Step 1: Install telegraf which will collect metrics from Google Cloud Monitoring for Cloud Function, see the available metrics for Cloud Function. The installation process for the respective operating system can be found in official documentation.

After successful installation, the telegraf status should be active and running.

Telegraf running status

telegraf running status

The configuration file for telegraf can be found here:

/etc/telegraf/telegraf.conf

Step 2: Configure the Telegraf input and output plugin by adding configurations to the telegraf.conf file.

# Gather timeseries from Google Cloud Platform v3 monitoring API

[[inputs.stackdriver]]

  ## GCP Project

  project = "omni-new"

  ## Include timeseries that start with the given metric type.

  metric_type_prefix_include = [

    "cloudfunctions.googleapis.com",

  ]

  ## Most metrics are updated no more than once per minute; it is recommended

  ## to override the agent level interval with a value of 1m or greater.

  interval = "1m"

# Send OpenTelemetry metrics over gRPC

[[outputs.opentelemetry]]

  ## Override the default (localhost:4317) OpenTelemetry gRPC service

  ## address:port

  service_address = "localhost:4317"

Deploy OpenTelemetry to scrape the metrics from Telegraf

Step 1: Install and configure OpenTelemetry for scraping the metrics from Telegraf. Follow OpenTelemetry Binary Usage in Virtual Machine guide for detailed instructions.

Step 2: After successful configuration start the OTel Collector using following command:

./otelcol-contrib --config ./config.yaml &> otelcol-output.log & echo "$!" > otel-pid

Step 3: Restart the Telegraf service

Step 4: If the configurations are configured correctly with Telegraf and we can see the output logs from OpenTelemtry as follows:

OTel Collector Logs

Viewing OTel Collector Logs

Send and Visualize the metrics obtained by OpenTelemetry in SigNoz

Step 1: Go to the SigNoz Cloud URL and head over to the dashboard.

Step 2: If not already created, create a new dashboard and select Time Series.

Creating new panel

Creating new panel in dashboard

Step 3: Select metric for cloud function

For example- cloudfunctions_googleapis_com_function_execution_times_bucket is one of the metrics which is collected.

Plot metrics

Metrics for the selected function.

Troubleshooting

If you run into any problems while setting up monitoring for your Cloud Function's metrics with SigNoz, consider these troubleshooting steps:

  • Verify Configuration: Double-check your config.yaml file to ensure all settings, including the ingestion key and endpoint, are correct.
  • Review Logs: Look at the logs of both your Cloud Function and the OpenTelemetry Collector to identify any error messages or warnings that might provide insights into what’s going wrong.
  • Update Dependencies: Ensure all relevant packages and dependencies are up-to-date to avoid compatibility issues.
  • Consult Documentation: Review the SigNoz, OpenTelemetry, and Telegraf documentation for any additional troubleshooting of the common issues.s

Was this page helpful?