akoutmos / prom_ex

An Elixir Prometheus metrics collection library built on top of Telemetry with accompanying Grafana dashboards
MIT License
577 stars 96 forks source link

[BUG] Empty Grafana Dashboards #212

Closed conradwt closed 9 months ago

conradwt commented 10 months ago

Describe the bug

After going through the prom_ex.ex file, I'm seeing empty dashboards for a Phoenix application.

Screenshot 2023-09-04 at 2 20 55 AM

Next, I verified that I was using the same Grafana data source name in both the Phoenix app and within Grafana UI.

config/config.exs:

# Configures prom_ex
config :zero_phoenix, ZeroPhoenix.PromEx,
  disabled: false,
  manual_metrics_start_delay: :no_delay,
  drop_metrics_groups: [],
  grafana: [
    host: System.get_env("GRAFANA_CLOUD_URL", "http://localhost:3000"),
    username: System.get_env("GRAFANA_USERNAME", "admin"),
    password: System.get_env("GRAFANA_PASSWORD", "admin"),
    # This is an optional setting and will default to `true`
    folder_name: "zero-to-graphql-using-elixir",
    upload_dashboards_on_start: true
  ]

config :zero_phoenix, ZeroPhoenix.PromEx,
  grafana_datasource_id: System.get_env("GRAFANA_DATASOURCE_ID", "Local Prometheus")

docker-compose.yaml:

version: '3.8'

services:
  grafana:
    # image: grafana/grafana-enterprise
    image: grafana/grafana-oss
    container_name: grafana
    restart: unless-stopped
    environment:
      - GF_SERVER_ROOT_URL=http://localhost:3000
      - GF_FEATURE_TOGGLES_ENABLE=publicDashboards
    #  - GF_INSTALL_PLUGINS=grafana-clock-panel
    ports:
      - '3000:3000'
    volumes:
      - 'grafana_storage:/var/lib/grafana'

  prometheus:
    image: prom/prometheus:v2.46.0
    container_name: prometheus
    ports:
      - 9090:9090
    volumes:
      - ./prometheus:/etc/prometheus/
      - prometheus-data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--web.enable-lifecycle'

volumes:
  grafana_storage: {}
  prometheus-data: {}

prom_ex.ex:

defmodule ZeroPhoenix.PromEx do
  use PromEx, otp_app: :zero_phoenix

  alias PromEx.Plugins

  @impl true
  def plugins do
    [
      # PromEx built in plugins
      Plugins.Application,
      Plugins.Beam,
      {Plugins.Phoenix, router: ZeroPhoenixWeb.Router, endpoint: ZeroPhoenixWeb.Endpoint},
      Plugins.Ecto,
      # Plugins.Oban,
      Plugins.PhoenixLiveView,
      Plugins.Absinthe
      # Plugins.Broadway,

      # Add your own PromEx metrics plugins
      # ZeroPhoenix.Users.PromExPlugin
    ]
  end

  @impl true
  def dashboard_assigns do
    [
      # datasource_id: "Local Prometheus",
      datasource_id: Application.get_env(:zero_phoenix, :grafana_datasource_id),
      default_selected_interval: "30s"
    ]
  end

  @impl true
  def dashboards do
    [
      # PromEx built in Grafana dashboards
      {:prom_ex, "application.json"},
      {:prom_ex, "beam.json"},
      {:prom_ex, "phoenix.json"},
      {:prom_ex, "ecto.json"},
      # {:prom_ex, "oban.json"},
      {:prom_ex, "phoenix_live_view.json"},
      {:prom_ex, "absinthe.json"}
      # {:prom_ex, "broadway.json"},

      # Add your dashboard definitions here with the format: {:otp_app, "path_in_priv"}
      # {:zero_phoenix, "/grafana_dashboards/user_metrics.json"}
    ]
  end
end

To Reproduce Steps to reproduce the behavior:

  1. Running both Grafana and Prometheus via Docker Compose
  2. Dashboards are uploaded to Grafana
  3. curl http://localhost:4000/metrics
  4. open http://localhost:3000/dashboards

Expected behavior

I would expect to see some data in each one of the dashboards.

Environment

Additional context

I'm not sure what's the ideal setup for PromEx. The application is being run on http://localhost:4000/graphiql (UI) or http://localhost:4000/graphql (API). The Phoenix app doesn't use Docker and simply runs on macOS 13.5.1 (Ventura). However, I'm using Docker Compose to run both Grafana (http://localhost:3000) and Prometheus (http://localhost:9090) within my local development environment. Finally, I'm using Bandit instead of Cowboy for the webserver.

conradwt commented 9 months ago

My issue was that I was using the wrong URL when using Docker Desktop. Thus, I needed to do the following:

change

  - job_name: 'phoenix_app'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:4000']

to

  - job_name: 'phoenix_app'
    scrape_interval: 5s
    static_configs:
      - targets: ['host.docker.internal:4000']

Now, I'm seeing the expected data within all the Grafana dashboards. Thus, I'm going to close this issue.