Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Prometheus and docker-compose #11

Merged
merged 10 commits into from
Aug 6, 2024

Conversation

Iajrdev
Copy link
Collaborator

@Iajrdev Iajrdev commented Jul 28, 2024

This PR adds Prometheus and Grafana to the project for monitoring purposes.

  • Added docker-compose.yml for setting up Prometheus and Grafana.
  • Configured Prometheus to scrape metrics from the Go application.
  • Updated documentation with setup instructions.

@Iajrdev Iajrdev self-assigned this Jul 28, 2024
@alipourhabibi
Copy link
Collaborator

I reviewed the PR
I found an error in grafana/provisioning/dashboards/my_dashboard.json
It looks like the json is not valid for dashboard.
I think you should remove the following

 {
-  "dashboard": {
     "id": null,
     "title": "My App Dashboard",
     "tags": [],
@@ -92,7 +91,5 @@
         ]
       }
     ]
-  },
-  "overwrite": false
 }

Apart from that all looks good to me.

@alipourhabibi
Copy link
Collaborator

LGMT

- job_name: 'myapp'
metrics_path: '/v2/metrics'
static_configs:
- targets: ['172.17.0.1:8080']
Copy link
Collaborator

@armineyvazi armineyvazi Aug 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

global:
  scrape_interval: 5s

scrape_configs:

  - job_name: 'myapp'
    metrics_path: '/v2/metrics'
    static_configs:
-     - targets: ['172.17.0.1:8080']
+     - targets: ['host.docker.internal:8080']

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a mistake in prometheus/prometheus.yml. It looks like the target IP is outdated. I think you should change the following

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As i understand the 172.17.0.1 refers to the IP address of the default gateway on the default bridge network and is not outdated. However the host.docker.internal is used in Docker desktop and won't work on Linux systems without additional configuration.
If we want to support Windows and Linux together we can add the following to the docker-compose.yaml for prometheus

    extra_hosts:
      - "host.docker.internal:host-gateway"

and use host.docker.internal as our targets in prometheus/prometheus.yml
What do you think?
@Iajrdev

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for completing this. I think this configuration is okay for Linux, macOS, and Windows OS:

prometheus/prometheus.yml

+     - targets: ['host.docker.internal:8080']

docker-compose.yml

+  extra_hosts:
+    - "host.docker.internal:host-gateway"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, you're suggesting that we should remove '172.17.0.1:8080' from the prometheus.yml file and replace it with 'host.docker.internal:8080'?
@alipourhabibi @armineyvazi

Copy link
Collaborator

@armineyvazi armineyvazi Aug 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, the suggestion is to replace 172.17.0.1:8080 with host.docker.internal:8080 in the prometheus.yml file. Additionally, we should add the following line to the docker-compose.yaml file to ensure compatibility across both Windows/Mac and Linux environments:

extra_hosts:
  - "host.docker.internal:host-gateway"

This configuration will make host.docker.internal resolvable within Docker containers on all platforms.

@alipourhabibi @Iajrdev

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made changes to the files as per your review and pushed them.
Please review them again.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure, but I think it should look like this, not added in Grafana:

  prometheus:
    image: docker.arvancloud.ir/prom/prometheus:latest
    container_name: prometheus
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    ports:
      - "9090:9090"
+  extra_hosts:
+   - "host.docker.internal:host-gateway"
    restart: unless-stopped

If you have a Linux system, please test this configuration to ensure it works correctly. The extra_hosts directive ensures that host.docker.internal is resolvable within the Prometheus container. Add the Grafana service if needed, without extra_hosts unless required for Grafana.

@Iajrdev

Copy link
Collaborator

@armineyvazi armineyvazi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment to Approve:

Approved. The configuration is now compatible with Linux, macOS, and Windows. Adding host.docker.internal:host-gateway to extra_hosts ensures that the Docker container can correctly reference the host machine across different operating systems.

Thanks for the update!

@Iajrdev
Copy link
Collaborator Author

Iajrdev commented Aug 6, 2024

  • Added healthcheck for Prometheus:

    • Implemented a health check for the Prometheus service to ensure it is ready before proceeding with other services.
    • Health check uses the command: ["CMD", "curl", "-f", "http://localhost:9090/-/ready"].
  • Configured depends_on for Grafana:

    • Updated the Grafana service to depend on the Prometheus service being healthy.
    • This guarantees Grafana will only start after Prometheus has successfully passed its health check.
  • Added healthcheck for Grafana:

    • Implemented a health check for the Grafana service to monitor its readiness.
    • Health check uses the command: ["CMD", "curl", "-f", "http://localhost:3000/api/health"].

@alipourhabibi @armineyvazi
Please review again.

docker-compose.yml Outdated Show resolved Hide resolved
Copy link
Collaborator

@alipourhabibi alipourhabibi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alipourhabibi alipourhabibi merged commit 2ab3851 into PwPJ:main Aug 6, 2024
4 checks passed
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.

3 participants