/
example_web.go
85 lines (73 loc) · 1.83 KB
/
example_web.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"expvar"
"flag"
"io"
"log"
"math/rand"
"net/http"
"runtime"
"time"
"github.com/yvasiyarov/gorelic"
)
var newrelicLicense = flag.String("newrelic-license", "", "Newrelic license")
var numCalls = expvar.NewInt("num_calls")
type WaveMetrica struct {
sawtoothMax int
sawtoothCounter int
}
func (metrica *WaveMetrica) GetName() string {
return "Custom/Wave_Metrica"
}
func (metrica *WaveMetrica) GetUnits() string {
return "Queries/Second"
}
func (metrica *WaveMetrica) GetValue() (float64, error) {
metrica.sawtoothCounter++
if metrica.sawtoothCounter > metrica.sawtoothMax {
metrica.sawtoothCounter = 0
}
return float64(metrica.sawtoothCounter), nil
}
func allocateAndSum(arraySize int) int {
arr := make([]int, arraySize, arraySize)
for i := range arr {
arr[i] = rand.Int()
}
time.Sleep(time.Duration(rand.Intn(3000)) * time.Millisecond)
result := 0
for _, v := range arr {
result += v
}
//log.Printf("Array size is: %d, sum is: %d\n", arraySize, result)
return result
}
func doSomeJob(numRoutines int) {
for i := 0; i < numRoutines; i++ {
go allocateAndSum(rand.Intn(1024) * 1024)
}
log.Printf("All %d routines started\n", numRoutines)
time.Sleep(1000 * time.Millisecond)
runtime.GC()
}
func helloServer(w http.ResponseWriter, req *http.Request) {
doSomeJob(5)
io.WriteString(w, "Did some work")
}
func main() {
flag.Parse()
if *newrelicLicense == "" {
log.Fatalf("Please, pass a valid newrelic license key.\n Use --help to get more information about available options\n")
}
agent := gorelic.NewAgent()
agent.Verbose = true
agent.CollectHTTPStat = true
agent.NewrelicLicense = *newrelicLicense
agent.AddCustomMetric(&WaveMetrica{
sawtoothMax: 10,
sawtoothCounter: 5,
})
agent.Run()
http.HandleFunc("/", agent.WrapHTTPHandlerFunc(helloServer))
http.ListenAndServe(":8080", nil)
}