From 1268c52008a7dedf046581ea896c626c89870e74 Mon Sep 17 00:00:00 2001 From: Developer3 Date: Sat, 27 May 2023 16:01:32 -0300 Subject: [PATCH] primeira versao --- .env | 0 .gitignore | 1 + .traefik.yml | 10 +++----- docker-compose.yaml | 37 +++++++++++++++++++++++++++ main.go | 61 ++++++++++++++++++++++++++++++++++++--------- 5 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 .env create mode 100644 docker-compose.yaml diff --git a/.env b/.env new file mode 100644 index 0000000..e69de29 diff --git a/.gitignore b/.gitignore index f32e31a..bde8fc5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +.env .idea/ .DS_Store diff --git a/.traefik.yml b/.traefik.yml index b08885d..464383e 100644 --- a/.traefik.yml +++ b/.traefik.yml @@ -1,4 +1,4 @@ -displayName: StatusD on routers +displayName: Metrics in routers type: middleware iconPath: .assets/icon.png @@ -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' \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..45b4a2a --- /dev/null +++ b/docker-compose.yaml @@ -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" \ No newline at end of file diff --git a/main.go b/main.go index e9ccb3b..1286986 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,9 @@ package statusdonrouters import ( "context" + "encoding/json" "fmt" + "net" "net/http" "net/http/httputil" ) @@ -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. @@ -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 {