go-playground / stats

:chart_with_upwards_trend: Monitors Go MemStats + System stats such as Memory, Swap and CPU and sends via UDP anywhere you want for logging etc...
MIT License
170 stars 19 forks source link

undefined: cpu.CPUInfoStat #1

Closed drsect0r closed 8 years ago

drsect0r commented 8 years ago
$ go get gopkg.in/go-playground/stats.v1
# gopkg.in/go-playground/stats.v1
../Go/src/gopkg.in/go-playground/stats.v1/stats.go:60: undefined: cpu.CPUInfoStat
../Go/src/gopkg.in/go-playground/stats.v1/stats.go:61: undefined: cpu.CPUTimesStat
../Go/src/gopkg.in/go-playground/stats.v1/stats.go:62: undefined: cpu.CPUTimesStat
../Go/src/gopkg.in/go-playground/stats.v1/stats.go:63: undefined: cpu.CPUTimesStat
../Go/src/gopkg.in/go-playground/stats.v1/stats.go:64: undefined: cpu.CPUTimesStat
../Go/src/gopkg.in/go-playground/stats.v1/stats.go:69: undefined: host.HostInfoStat
deankarn commented 8 years ago

Thanks @drsect0r

I was aware of the issue, just haven't been able to find time to fix it. I have to vendor a dependant library that has since been updated from which that struct cpu is from.

SIDE NOTE: Everyone says not to vendor your library's dependancies, but this is exactly why you have to...

drsect0r commented 8 years ago

I tried finding the dependency myself but my google-fu failed me. Thanks anyway :+1:

deankarn commented 8 years ago

the dependancy is here https://github.com/shirou/gopsutil if you wanted to/are able to help :)

I also have to add an attribution as I believe the library now uses a BSD license.

drsect0r commented 8 years ago

Oh crap, something else failed as I saw/found this dependency. I will try to make a merge :)

deankarn commented 8 years ago

Thanks!

drsect0r commented 8 years ago

@joeybloggs I haven't been able to allocate any time on this issue. Do you see any opportunity?

deankarn commented 8 years ago

Hey @drsect0r

went at it this morning and believe the build issue should be fixed, please let me know if the fix works for you also.

drsect0r commented 8 years ago

Are the examples still adequate? I have server.go running, when I run client.go neither of the two generate any output or seem to be doing anything at all.

deankarn commented 8 years ago

@drsect0r client.go is exiting before sending any output, I'll adjust the example oh and you have to change the domain of the client...

anyways here is a working client for you, I will update the client .go to be exactly this:

package main

import (
    "fmt"
    "net/http"
    "runtime"

    "gopkg.in/go-playground/stats.v1"
)

var statsClient *stats.ClientStats

func main() {

    serverConfig := &stats.ServerConfig{
        Domain: "localhost",
        Port:   3008,
        Debug:  false,
    }

    clientConfig := &stats.ClientConfig{
        Domain:           "",
        Port:             3009,
        PollInterval:     1000,
        Debug:            false,
        LogHostInfo:      true,
        LogCPUInfo:       true,
        LogTotalCPUTimes: true,
        LogPerCPUTimes:   true,
        LogMemory:        true,
        LogGoMemory:      true,
    }

    client, err := stats.NewClient(clientConfig, serverConfig)
    if err != nil {
        panic(err)
    }

    go client.Run()

    // if you want to capture HTTP requests in a middleware you'll have
    // to have access to the client.
    // see below for middleware example
    statsClient = client

    <-make(chan struct{})
}

// LoggingRecoveryHandler ...
//
//
// Middleware example for capturing HTTP Request info
// NOTE: this is standard go middlware, but could be adapted to other types/styles easily
//
func LoggingRecoveryHandler(next http.Handler) http.Handler {

    fn := func(w http.ResponseWriter, r *http.Request) {

        // log incoming request
        logReq := statsClient.NewHTTPRequest(w, r)

        defer func() {
            if err := recover(); err != nil {
                trace := make([]byte, 1<<16)
                n := runtime.Stack(trace, true)

                // log failure
                logReq.Failure(fmt.Sprintf("%s\n%s", err, trace[:n]))

                http.Error(w, "Friendly error message", 500)
                return
            }
        }()

        next.ServeHTTP(logReq.Writer(), r)

        // log completion
        logReq.Complete()
    }

    return http.HandlerFunc(fn)
}
deankarn commented 8 years ago

ok example updated.

drsect0r commented 8 years ago

Oh, I definitely need my coffee ... Thank you! :+1: