uber-go / tally

A Go metrics interface with fast buffered metrics and third party reporters
MIT License
850 stars 116 forks source link

Export baseReporter from Scope #174

Open yoonsio opened 2 years ago

yoonsio commented 2 years ago

Prometheus CachedReporter provides a handler for metrics endpoint. It is useful to have the ability to access baseReporter from Scope so that the handler method can be called from the top level.

func getScope() (tally.Scope, io.Closer, error) {
    r := promreporter.NewReporter(promreporter.Options{})
    scope, closer := tally.NewRootScope(tally.ScopeOptions{
        Prefix:         "my_service",
        Tags:           map[string]string{},
        CachedReporter: r,
        Separator:      promreporter.DefaultSeparator,
    }, 1*time.Second)
    return scope, closer, nil
}

func main() {
    scope, closer, _ := getScope()

    // TODO: get the http handler from the scope
    r, _, := scope.BaseReporter.(promreporter.Reporter)
    http.Handle("/metrics", r.HTTPHandler())

    fmt.Printf("Serving :8080/metrics\n")
    fmt.Printf("%v\n", http.ListenAndServe(":8080", nil))
}