Skip to content

Instantly share code, notes, and snippets.

@moertel
Created August 23, 2023 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moertel/32ed043d77268d17c28c4c285136bff9 to your computer and use it in GitHub Desktop.
Save moertel/32ed043d77268d17c28c4c285136bff9 to your computer and use it in GitHub Desktop.
Send OpenTelemetry traces via OTLP to Grafana Cloud directly from Google Cloud Function
from firebase_functions import https_fn
from firebase_admin import initialize_app
from typing import Any
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
resource = Resource(attributes={"service.name": "my-service"})
trace.set_tracer_provider(TracerProvider(resource=resource))
tracer = trace.get_tracer(__name__)
otlp_exporter = OTLPSpanExporter(
# Obtain the cluster location from your Grafana Cloud account page
# See also: https://grafana.com/docs/grafana-cloud/monitor-infrastructure/otlp/send-data-otlp/
endpoint="https://otlp-gateway-prod-eu-west-2.grafana.net/otlp/v1/traces",
# Instead of hardcoding credentials, consider using Cloud Secret Manager
# Base64-encode your credentials via your console like this:
# echo -n "{GRAFANA_INSTANCE_ID}:{CLOUD_API_KEY}" | base64
headers={"Authorization": "Basic YOUR_BASE64_ENCODED_CREDENTIALS"}
)
span_processor = SimpleSpanProcessor(otlp_exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
initialize_app()
@https_fn.on_call()
def hello(request: https_fn.CallableRequest) -> Any:
with tracer.start_as_current_span("Say hello"):
return {"response": "hello!"}
firebase-functions==0.1.1
opentelemetry-api==1.19.0
opentelemetry-exporter-otlp-proto-http==1.19.0
opentelemetry-sdk==1.19.0
opentelemetry-semantic-conventions==0.40b0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment