Skip to content

Commit

Permalink
Merge pull request #31 from safe2008/master
Browse files Browse the repository at this point in the history
Update auto add prometheus to grafana and change golang port
  • Loading branch information
TannerGabriel authored Aug 26, 2021
2 parents ad71e8c + d2dc7b1 commit 906679f
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 32 deletions.
4 changes: 2 additions & 2 deletions advanced-programs/PrometheusHTTPServer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ COPY . .
# Build the application
RUN go build -o main .

# Expose port 9000 to the outside world
EXPOSE 9000
# Expose port 8080 to the outside world
EXPOSE 8080

# Command to run the executable
CMD ["./main"]
8 changes: 5 additions & 3 deletions advanced-programs/PrometheusHTTPServer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ docker-compose up -d

### Generating load on HTTP Server

You can either generate load manually by visiting the HTTP page on localhost:9000 or generate load using some load generator. I use [Hey](https://github.com/rakyll/hey), which is an easy to use command-line tool.
You can either generate load manually by visiting the HTTP page on localhost:8080 or generate load using some load generator. I use [Hey](https://github.com/rakyll/hey), which is an easy to use command-line tool.

```
hey -z 5m -q 5 -m GET -H "Accept: text/html" http://127.0.0.1:9000
hey -z 5m -q 5 -m GET -H "Accept: text/html" http://127.0.0.1:8080
hey -n 10000 -c 100 -m GET -H "Accept: text/html" http://127.0.0.1:8080
```

You can now see the metrics of your application on localhost:9000/Prometheus.
You can now see the metrics of your application on http://localhost:8080/metrics.

### Importing Grafana dashboard

Expand Down
41 changes: 24 additions & 17 deletions advanced-programs/PrometheusHTTPServer/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
version: '3.1'

services:
grafana:
image: grafana/grafana:latest
container_name: grafana
ports:
- "3000:3000"
volumes:
- grafana-storage:/var/lib/grafana
golang:
build:
context: ./
dockerfile: Dockerfile
container_name: golang
restart: always
ports:
- '9000:9000'
prometheus:
image: prom/prometheus:v2.24.0
image: prom/prometheus:latest
container_name: prometheus
volumes:
- ./prometheus/:/etc/prometheus/
- prometheus_data:/prometheus
Expand All @@ -29,7 +15,28 @@ services:
ports:
- 9090:9090
restart: always
grafana:
image: grafana/grafana:latest
container_name: grafana
volumes:
- grafana_data:/var/lib/grafana
- ./grafana/provisioning:/etc/grafana/provisioning
environment:
- GF_AUTH_DISABLE_LOGIN_FORM=false
- GF_AUTH_ANONYMOUS_ENABLED=false
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
ports:
- 3000:3000
golang:
container_name: golang
build:
context: ./
dockerfile: Dockerfile
container_name: golang
restart: always
ports:
- 8080:8080

volumes:
grafana-storage:
grafana_data:
prometheus_data:
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: 1

providers:
- name: 'Prometheus'
orgId: 1
folder: ''
type: file
disableDeletion: false
editable: true
options:
path: /etc/grafana/provisioning/dashboards
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
},
{
"aliasColors": {
"response_status{instance=\"golang:9000\", job=\"golang\", status=\"304\"}": "red"
"response_status{instance=\"golang:8080\", job=\"golang\", status=\"304\"}": "red"
},
"bars": true,
"dashLength": 10,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: 1

datasources:
- name: Prometheus
type: prometheus
access: proxy
orgId: 1
url: http://prometheus:9090
basicAuth: false
isDefault: true
editable: true
13 changes: 7 additions & 6 deletions advanced-programs/PrometheusHTTPServer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package main

import (
"fmt"
"log"
"net/http"
"strconv"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
"strconv"
)

type responseWriter struct {
Expand Down Expand Up @@ -75,12 +76,12 @@ func main() {
router.Use(prometheusMiddleware)

// Prometheus endpoint
router.Path("/prometheus").Handler(promhttp.Handler())
router.Path("/metrics").Handler(promhttp.Handler())

// Serving static files
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))

fmt.Println("Serving requests on port 9000")
err := http.ListenAndServe(":9000", router)
fmt.Println("Serving requests on port 8080")
err := http.ListenAndServe(":8080", router)
log.Fatal(err)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ scrape_configs:
static_configs:
- targets: ['localhost:9090']
- job_name: golang
metrics_path: /prometheus
metrics_path: /metrics
static_configs:
- targets:
- golang:9000
- targets: ['golang:8080']

0 comments on commit 906679f

Please sign in to comment.