prometheus / docs

Prometheus documentation: content and static site generator
https://prometheus.io
Apache License 2.0
665 stars 1.02k forks source link

Added GraphQL exporter #2509

Closed ricardbejarano closed 2 months ago

ricardbejarano commented 2 months ago
juliusv commented 2 months ago

Thanks! Do you have an example /metrics output to look at (I realize this would vary based on the GraphQL endpoint and query)?

When linking to an exporter, we just want to ensure that metric names etc. roughly stick to our guidelines (https://prometheus.io/docs/practices/naming/ and https://prometheus.io/docs/instrumenting/writing_exporters/).

ricardbejarano commented 2 months ago

They're generated based on the response schema of the GraphQL API you're querying. This is to make it generic and allow querying any GraphQL API with any schema.

A query like this:

scrape_configs:
  - job_name: "cloudflare-analytics"
    metrics_path: "/query"
    bearer_token: "<YOUR CLOUDFLARE API TOKEN>"
    params:
      endpoint: ["https://api.cloudflare.com/client/v4/graphql"]
      query:
        - |
          {
            viewer {
              zones(filter: {zoneTag: $zoneTag}) {
                httpRequests1hGroups(limit: 1, filter: {datetime_geq: "{{ Now "-1h" }}"}) {
                  dimensions {
                    datetime
                  }
                  sum {
                    bytes
                    cachedBytes
                    cachedRequests
                    requests
                  }
                }
              }
            }
          }
      zoneTag: ["<YOUR CLOUDFLARE ZONE ID>"]
    static_configs:
      - targets: ["127.0.0.1:9199"]

Would produce metrics (all gauges) such as:

query_viewer_zones_httpRequests1hGroups_sum_bytes{...} 10000
query_viewer_zones_httpRequests1hGroups_sum_cachedBytes{...} 9000
query_viewer_zones_httpRequests1hGroups_sum_cachedRequests{...} 900
query_viewer_zones_httpRequests1hGroups_sum_requests{...} 1000

Where the name of the metrics is "query_" plus the keys of JSON path to get to the value, joined by underscores.

Again, this all depends on the API you're querying and its schema.

juliusv commented 2 months ago

I see, thanks! Yeah, I guess it's hard to get metrics that are compliant with our usual naming and labeling best conventions in that case.

:+1: