Google Cloud Functions
Monitor Google Cloud Functions using OpenTelemetry instrumentation with Last9
Use OpenTelemetry to instrument your Google Cloud Function and send telemetry data to Last9.
You can develop and test functions locally using Functions Framework. Developing a function locally can help you test your code without having to rebuild your function container. This can save time and make it easier to test your function.
For more details refer the link.
Prerequisites
Before setting up Google Cloud Functions monitoring, ensure you have:
- Google Cloud account with Cloud Functions enabled
- Python 3.8+ for function runtime
- Last9 account with integration credentials
-
Install the functions-framework
Install the required packages:
pip install functions-framework opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp opentelemetry-distroAfter installing the required packages, run the following command to install the required instrumentation packages for your application:
opentelemetry-bootstrap -a installYou can install these packages one by one or add them to
requirements.txtand runpip install -r requirements.txt. -
Instrument the Python Application
Create a new file
main.pyand add the following code:import asyncioimport functions_frameworkfrom flask import Request, jsonifyfrom opentelemetry import tracefrom opentelemetry.sdk.trace import TracerProviderfrom opentelemetry.sdk.trace.export import BatchSpanProcessorfrom opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporterfrom opentelemetry.sdk.resources import Resourcefrom opentelemetry.semconv.resource import ResourceAttributesfrom opentelemetry.instrumentation.asyncio import AsyncioInstrumentor# Initialize OpenTelemetry componentsdef initialize_tracing():# Create a resource with service informationresource = Resource.create({ResourceAttributes.SERVICE_NAME: "cloud-function", # Replace this with your service nameResourceAttributes.SERVICE_VERSION: "0.1.0",ResourceAttributes.DEPLOYMENT_ENVIRONMENT: "dev" # Replace with environment})# Set up tracer provider with the resourceprovider = TracerProvider(resource=resource)trace.set_tracer_provider(provider)# Configure the OTLP exporterotlp_exporter = OTLPSpanExporter(endpoint="{{ .Logs.WriteURL }}",headers={"authorization":"{{ .Logs.AuthValue }}",})# For debugging# span_processor = BatchSpanProcessor(ConsoleSpanExporter())span_processor = BatchSpanProcessor((otlp_exporter))provider.add_span_processor(span_processor)AsyncioInstrumentor().instrument()return trace.get_tracer(__name__)tracer = initialize_tracing()async def process_request(request_data):with tracer.start_as_current_span("process_request") as span:span.set_attribute("request.data_size", len(str(request_data)))await asyncio.sleep(1)result = {"message": "Processed asynchronously", "data": request_data}return result@functions_framework.httpdef http_handler(request: Request):with tracer.start_as_current_span("http_handler", kind=trace.SpanKind.SERVER) as span:span.set_attribute("http.method", request.method)span.set_attribute("http.url", request.url)span.set_attribute("http.route", request.path)if request.is_json:request_data = request.get_json()else:request_data = request.form.to_dict() if request.form else request.args.to_dict()with tracer.start_as_current_span("extract_headers") as header_span:headers = dict(request.headers)header_span.set_attribute("request.header_count", len(headers))if 'traceparent' in headers:header_span.set_attribute("trace.parent_id", headers['traceparent'])try:loop = asyncio.new_event_loop()asyncio.set_event_loop(loop)result = loop.run_until_complete(process_request(request_data))loop.close()return jsonify(result)except Exception as e:with tracer.start_as_current_span("error_handler") as error_span:error_span.set_attribute("error.type", str(type(e).__name__))error_span.set_attribute("error.message", str(e))error_span.record_exception(e)return jsonify({"error": str(e)}), 500# For local testingif __name__ == "__main__":import os# Run the app locally with functions-framework# Use: functions-framework --target http_handler --debugprint("Run with: functions-framework --target http_handler --debug") -
Deploy the Function
Refer this link to deploy the function on Google Cloud Platform.
Verification
To verify the setup is working:
- Test your function locally using Functions Framework
- Deploy the function to Google Cloud Platform
- Log into your Last9 account to confirm function traces are being received
Need Help?
If you encounter any issues or have questions:
- Join our Discord community for real-time support
- Contact our support team at support@last9.io