Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewStatisticsHandler ¶
NewStatisticsHandler returns new StatisticsHandler.
Example ¶
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"github.com/osamingo/gosh"
)
func newJSONEncoder(w io.Writer) gosh.JSONEncoder {
return json.NewEncoder(w)
}
func main() {
sh, err := gosh.NewStatisticsHandler(newJSONEncoder)
if err != nil {
fmt.Println(err)
return
}
srv := httptest.NewServer(sh)
defer srv.Close()
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL, nil)
if err != nil {
fmt.Println(err)
return
}
resp, err := srv.Client().Do(req)
if err != nil {
fmt.Println(err)
return
}
defer func() {
if err := resp.Body.Close(); err != nil {
fmt.Println(err)
return
}
}()
if resp.StatusCode != http.StatusOK {
fmt.Println("unexpect status code:", resp.StatusCode)
return
}
var stats gosh.Statistics
if err := json.NewDecoder(resp.Body).Decode(&stats); err != nil {
fmt.Println(err)
return
}
fmt.Printf("status_code: %d, has_gorutine: %t", resp.StatusCode, stats.GoroutineNum > 0)
}
Output: status_code: 200, has_gorutine: true
Types ¶
type JSONEncoder ¶ added in v1.1.0
type JSONEncoder interface {
Encode(v interface{}) error
}
A JSONEncoder provides Encode method. The Encode method writes the JSON encoding of v to the stream, followed by a newline character.
type Statistics ¶
type Statistics struct {
Timestamp int64 `json:"timestamp"`
GoVersion string `json:"go_version"`
GoOS string `json:"go_os"`
GoArch string `json:"go_arch"`
CPUNum int `json:"cpu_num"`
GoroutineNum int `json:"goroutine_num"`
Gomaxprocs int `json:"gomaxprocs"`
CgoCallNum int64 `json:"cgo_call_num"`
MemoryAlloc uint64 `json:"memory_alloc"`
MemoryTotalAlloc uint64 `json:"memory_total_alloc"`
MemorySys uint64 `json:"memory_sys"`
MemoryLookups uint64 `json:"memory_lookups"`
MemoryMallocs uint64 `json:"memory_mallocs"`
MemoryFrees uint64 `json:"memory_frees"`
StackInuse uint64 `json:"stack_inuse"`
HeapAlloc uint64 `json:"heap_alloc"`
HeapSys uint64 `json:"heap_sys"`
HeapIdle uint64 `json:"heap_idle"`
HeapInuse uint64 `json:"heap_inuse"`
HeapReleased uint64 `json:"heap_released"`
HeapObjects uint64 `json:"heap_objects"`
GCNext uint64 `json:"gc_next"`
GCLast uint64 `json:"gc_last"`
GCNum uint32 `json:"gc_num"`
GCPerSecond float64 `json:"gc_per_second"`
GCPausePerSecond float64 `json:"gc_pause_per_second"`
GCPause []float64 `json:"gc_pause"`
}
A Statistics has runtime information.
type StatisticsHandler ¶
type StatisticsHandler struct {
NewJSONEncoder func(w io.Writer) JSONEncoder
// contains filtered or unexported fields
}
A StatisticsHandler provides runtime information handler.
func (*StatisticsHandler) MeasureRuntime ¶
func (sh *StatisticsHandler) MeasureRuntime() Statistics
MeasureRuntime accesses runtime information.
func (*StatisticsHandler) ServeHTTP ¶
func (sh *StatisticsHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request)
ServeHTTP implements http.Handler interface.
Click to show internal directories.
Click to hide internal directories.