Setting up Open Telemetry (OTel, Prometheus, Grafana and Jaeger) with OCaml
I missed last week updates, my apologies. This week the items on my todo were to set up OTel for chaufr so I can monitor http requests, database connections, database queries and ocaml memory allocation using Prometheus, Grafana, and Jaeger all exported to Otel Collector for monitoring and tracing. In this blog post, I'll share the steps I took to achieve this.
Prerequisites
You need to have following libraries installed in your OCaml project:
- opentelemetry
- opentelemetry-client-ocurl
- opentelemetry-lwt
- opentelemetry.trace
- opentelemetry-cohttp-lwt
- logs
Step 1: Setting up OpenTelemetry in OCaml
First, I created a folder called config where I created a file called telemetry.ml
to configure OpenTelemetry.
(** Resource attributes for telemetry *)
type resource_attributes = {
service_name : string;
service_version : string;
service_namespace : string option;
service_instance_id : string;
deployment_environment : string;
host_name : string;
host_type : string option;
cloud_provider : string option;
cloud_region : string option;
container_name : string option;
container_id : string option;
custom_attributes : (string * string) list;
}
(** telemetry configuration *)
type telemetry_config = {
enabled : bool;
environment : environment;
resource : resource_attributes;
tracing : tracing_config;
metrics : metrics_config;
logging : logging_config;
azure_monitor : azure_monitor_config;
}
I followed the instructions from this repo to set up OpenTelemetry in my OCaml project with the assistance of claude-sonnet 4.5 I was able to set up tracing and metrics for my http requests and database queries.
I later then imported the telemetry configuration in my main.ml
and initialized the OpenTelemetry SDK.
But I encountered some issues when deploying on my gh actions and the weekend is over.
I have to say, using ai chatbot has its shortcomings as I had to do a lot of trial and error to get things working.
Also a fraction of learning took place as I just wanted to get things working.
I aim to add more details to this draft once I have my latest changes working on deployment.
Cheers!