File: server.go

package info (click to toggle)
golang-github-performancecopilot-speed 4.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 508 kB
  • sloc: makefile: 38
file content (50 lines) | stat: -rw-r--r-- 912 bytes parent folder | download
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
package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/performancecopilot/speed/v4"
)

var metric speed.Counter

func main() {
	var err error
	metric, err = speed.NewPCPCounter(
		0,
		"http.requests",
		"Number of Requests",
	)
	if err != nil {
		log.Fatal("Could not create counter, error: ", err)
	}

	client, err := speed.NewPCPClient("example")
	if err != nil {
		log.Fatal("Could not create client, error: ", err)
	}

	client.MustRegister(metric)

	client.MustStart()
	defer client.MustStop()

	http.HandleFunc("/increment", handleIncrement)
	go func() {
		if err := http.ListenAndServe(":8080", nil); err != nil {
			log.Fatal("Could not listen on port, error: ", err)
		}
	}()

	fmt.Println("To stop the server press enter")
	_, _ = os.Stdin.Read(make([]byte, 1))
	os.Exit(0)
}

func handleIncrement(w http.ResponseWriter, r *http.Request) {
	metric.Up()
	fmt.Fprintf(w, "incremented\n")
}