Skip to content

Conversation

@andriimredocly
Copy link
Contributor

I want to expose the health and the raft state metrics
Two new gauges are added:
typesense_health: 1 if the target responds with 200 HTTP code and the response is {"ok": true}

# HELP typesense_health Health status of Typesense instance (1 = healthy, 0 = unhealthy)
# TYPE typesense_health gauge

and typesense_raft_state: 1 = leader, 4 = follower, 0 = other

# HELP typesense_raft_state Raft state of Typesense node (1 = leader, 4 = follower, 0 = other)
# TYPE typesense_raft_state gauge

This is useful as I am running Typesense in HA mode with 3 instances, and I need to configure alerts for a missing leader.

Also, I see that the metrics exporter defaults metric if the 401 response is sent:

Define default:

    let mut stats_data: TypesenseStats = TypesenseStats::default();

Update if the HTTP code is 200

        reqwest::StatusCode::OK => {
            match res.json::<TypesenseStats>().await {
                Ok(parsed) => {
                    stats_data = parsed;
                }
                Err(_) => println!("Hm, the response didn't match the shape we expected."),
            };
        }

Return

    Ok(stats_data)

But if the response fails completely or times out, nothing happens
I added the timeout for the request that is disabled by default and added the error handler for failed requests and timeouts

    let res = match client
        .get(url)
        .header("X-TYPESENSE-API-KEY", format!("{}", args.typesense_api_key))
        .send()
        .await
    {
        Ok(res) => res,
        Err(e) => {
            println!("Stats endpoint: request failed (timeout/connection error): {:?}", e);
            return Ok(stats_data);
        }
    };

This is also related to this issue: #2

cyppe added a commit to cyppe/typesense-prometheus-exporter that referenced this pull request Jan 13, 2026
…upport

- Add /health endpoint monitoring (typesense_health metric)
- Add /debug endpoint for raft state (typesense_raft_state metric)
- Add configurable timeout for Typesense API requests
- Improve error handling for connection failures
cyppe added a commit to cyppe/typesense-prometheus-exporter that referenced this pull request Jan 13, 2026
…ements

This PR consolidates and builds upon existing community contributions:

Credits:
- PR imatefx#11 by @berciktymansen - Makes CPU fields optional
- PR imatefx#13 by @berciktymansen - Adds health endpoint and raft state metrics

Additional improvements:
- Dynamic CPU handling using serde flatten (supports any CPU count)
- Swap memory metrics (system_memory_total_swap_bytes, system_memory_used_swap_bytes)
- Cache hit ratio metric from stats endpoint
- Removed verbose "No match found" log spam for non-collection endpoints

All changes are backward compatible with older Typesense versions.
Tested against Typesense v30.0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant