prometheus/docs

Prometheus as data source in Grafana

miparnisari opened this issue · 2 comments

To expose Prometheus metrics in a Go application, you need to provide a `/metrics` HTTP endpoint. You can use the [`prometheus/promhttp`](https://godoc.org/github.com/prometheus/client_golang/prometheus/promhttp) library's HTTP [`Handler`](https://godoc.org/github.com/prometheus/client_golang/prometheus/promhttp#Handler) as the handler function.

After following this guide, i have Prometheus data at http://localhost:2112/metrics.

However, if I try to add Prometheus as a data source in Grafana, Grafana tries to query api/v1/query, which doesn't have anything.

image

What am I missing?

Sorry, the error above was a netowrking error on my end.

I changed my code so that data is exposed at api/query/v1.

Then, since I'm using Docker to run both Grafana and my app, I changed the data source URL to http://host.docker.internal:2112.

But now I get Error reading Prometheus: bad_response: readObjectStart: expect { or n, but found #, error found in #1 byte of ...|# HELP go_g|..., bigger context ...|# HELP go_gc_duration_seconds A summary of the paus|...

Clearly, it's not able to parse the response.

image

okay i figured it out, i think this is worth documenting.

In my Docker compose file:

  prometheus:
    image: bitnami/prometheus:latest
    ports:
      - "9090:9090"
    volumes:
      - "./prometheus/prometheus.yml:/opt/bitnami/prometheus/conf/prometheus.yml"
    networks:
      - default

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    networks:
      - default
    ports:
      - "3000:3000"

In my app code:

		go func() {
			logger.Info("🔬 starting prometheus on localhost:2112")
			http.Handle("/metrics", promhttp.Handler())
			http.ListenAndServe(":2112", nil)
		}()

In my prometheus.yml file:

scrape_configs:
  - job_name: 'scrape'
    static_configs:
      - targets: ['host.docker.internal:2112']