Last active
July 10, 2019 16:54
-
-
Save pitlv2109/d29b1b9dd819eb8b8fb814053b709521 to your computer and use it in GitHub Desktop.
Istio OpenCensus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"contrib.go.opencensus.io/exporter/prometheus" | |
"fmt" | |
"go.opencensus.io/stats/view" | |
"io" | |
"istio.io/pkg/monitoring" | |
"log" | |
"net/http" | |
"os" | |
) | |
var ( | |
RequestType = monitoring.MustCreateLabel("request_type") | |
outgoingLatency = monitoring.NewSum( | |
"outgoing_latency", | |
"The latency of "+ | |
"outgoing requests (e.g. to a token exchange server, CA, etc.) in milliseconds.", | |
monitoring.WithLabels(RequestType), | |
) | |
) | |
func main() { | |
monitoring.MustRegister(outgoingLatency) | |
// Create the Prometheus exporter. | |
pe, err := prometheus.NewExporter(prometheus.Options{ | |
Namespace: "ocmetricstutorial", | |
}) | |
if err != nil { | |
log.Fatalf("Failed to create the Prometheus stats exporter: %v", err) | |
} | |
// Register the Prometheus exporter. | |
// This step is needed so that metrics can be exported. | |
view.RegisterExporter(pe) | |
// Now finally run the Prometheus exporter as a scrape endpoint. | |
// We'll run the server on port 8888. | |
go func() { | |
mux := http.NewServeMux() | |
mux.Handle("/metrics", pe) | |
if err := http.ListenAndServe(":8888", mux); err != nil { | |
log.Fatalf("Failed to run Prometheus scrape endpoint: %v", err) | |
} | |
}() | |
// In a REPL: | |
// 1. Read input | |
// 2. process input | |
br := bufio.NewReader(os.Stdin) | |
// repl is the read, evaluate, print, loop | |
for { | |
foo() | |
if err := readEvaluateProcess(br); err != nil { | |
if err == io.EOF { | |
return | |
} | |
log.Fatal(err) | |
} | |
} | |
} | |
func foo() { | |
outgoingLatency.With(RequestType.Value("foo")).Increment() | |
rows, err := view.RetrieveData("outgoing_latency") | |
if err != nil { | |
fmt.Println(err.Error()) | |
} | |
for _, row := range rows { | |
fmt.Printf("TAGS: %v\n", row.Tags) | |
fmt.Printf("DATA: %v\n", row.Data) | |
} | |
} | |
// readEvaluateProcess reads a line from the input reader and | |
// then processes it. It returns an error if any was encountered. | |
func readEvaluateProcess(br *bufio.Reader) (terr error) { | |
fmt.Printf("> ") | |
line, _, err := br.ReadLine() | |
if err != nil { | |
if err != io.EOF { | |
return err | |
} | |
log.Fatal(err) | |
} | |
out := line | |
fmt.Printf("< %s\n\n", out) | |
return nil | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment