docs icon indicating copy to clipboard operation
docs copied to clipboard

Prometheus as data source in Grafana

Open miparnisari opened this issue 3 years ago • 2 comments

https://github.com/prometheus/docs/blob/8d0bcf464f6275a544b15b7c214e9092fcb145b0/content/docs/guides/go-application.md?plain=1#L23

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?

miparnisari avatar Nov 17 '22 01:11 miparnisari

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

miparnisari avatar Nov 17 '22 02:11 miparnisari

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']

miparnisari avatar Nov 17 '22 07:11 miparnisari