Skip to content

Commit

Permalink
primeira versao
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNinso committed May 27, 2023
1 parent 00a675f commit 1268c52
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 18 deletions.
Empty file added .env
Empty file.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
.idea/
.DS_Store
10 changes: 4 additions & 6 deletions .traefik.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
displayName: StatusD on routers
displayName: Metrics in routers
type: middleware
iconPath: .assets/icon.png

Expand All @@ -7,8 +7,6 @@ import: github.com/MrNinso/statusdonrouters
summary: 'give metrics per router'

testData:
ip: localhost
port: 8000
serverPrefix: 'traefik'
rotuerPrefix: 'test'

ip: 192.168.0.17
port: 1234
serverPrefix: 'traefik'
37 changes: 37 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
version: "3.9"

services:
traefik:
image: "traefik:v2.5"
command:
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
- "--experimental.localPlugins.statusdonrouters.moduleName=github.com/MrNinso/statusdonrouters"
ports:
- "80:80"
- "8080:8080"
labels:
- "traefik.enable=true"
- "traefik.http.middlewares.statusdonrouters.plugin.statusdonrouters.ip=${IP}"
- "traefik.http.middlewares.statusdonrouters.plugin.statusdonrouters.port=${PORT}"
- "traefik.http.middlewares.statusdonrouters.plugin.statusdonrouters.serverPrefix=${SERVER_PREFIX}"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- ".:/plugins-local/src/github.com/MrNinso/statusdonrouters" # change this to the path of your cloned plugin repository

whoami:
image: "traefik/whoami"
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
- "traefik.http.routers.whoami.middlewares=statusdonrouters@docker"
whoami2:
image: "traefik/whoami"
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami2.rule=Host(`127.0.0.1`)"
- "traefik.http.routers.whoami2.entrypoints=web"
- "traefik.http.routers.whoami2.middlewares=statusdonrouters@docker"
61 changes: 49 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package statusdonrouters

import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/httputil"
)
Expand All @@ -13,17 +15,11 @@ type Config struct {
Ip string `json:"ip"`
Port string `json:"port"`
ServerPrefix string `json:"serverPrefix"`
RotuerPrefix string `json:"rotuerPrefix"`
}

// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
Ip: "localhost",
Port: "8000",
ServerPrefix: "traefik",
RotuerPrefix: "test",
}
return &Config{}
}

// Demo a Demo plugin.
Expand All @@ -42,23 +38,64 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
}, nil
}

type Metric struct {
Server string `json:"server"`
Host string `json:"host"`
Method string `json:"method"`
Path string `json:"path"`
RequestSize int `json:"requestSize"`
ResponseSize int `json:"responseSize"`
}

func (p *Plugin) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// create a custom response writer to intercept the response
crw := &customResponseWriter{ResponseWriter: rw}

m := &Metric{}

p.next.ServeHTTP(crw, req)

// dump the request and get its size
dump, err := httputil.DumpRequest(req, true)
if err != nil {
fmt.Println(err)
}

reqSize := len(dump)
m.Server = p.config.ServerPrefix
m.Host = req.Host

p.next.ServeHTTP(crw, req)
m.Method = req.Method
m.Path = req.URL.Path
m.RequestSize = len(dump)
m.ResponseSize = crw.size

go p.send(m)
}

func (p *Plugin) send(metric *Metric) {
v, err := json.Marshal(metric)

if err != nil {
// TODO não sei o que fazer aqui
return
}

h := fmt.Sprint(
p.config.Ip,
":",
p.config.Port,
)

conn, err := net.Dial("udp", h)

if err != nil {
// TODO não sei o que fazer aqui
return
}

resSize := crw.size
defer conn.Close()

fmt.Printf("Request size: %d bytes\n", reqSize)
fmt.Printf("Response size: %d bytes\n", resSize)
fmt.Fprint(conn, string(v))
}

type customResponseWriter struct {
Expand Down

0 comments on commit 1268c52

Please sign in to comment.