Skip to content

Instantly share code, notes, and snippets.

@naveensrinivasan
Created September 21, 2023 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naveensrinivasan/594a70577eae8f1436f7d78c4965fd01 to your computer and use it in GitHub Desktop.
Save naveensrinivasan/594a70577eae8f1436f7d78c4965fd01 to your computer and use it in GitHub Desktop.
ObserveHistogram - Name: http_request_duration_seconds, Value: 0.000007, Labels: [handler:HelloWorldHandler code:200]
ObserveHistogram - Name: http_request_duration_seconds, Value: 0.000006, Labels: [handler:HelloWorldHandler code:200]
ObserveHistogram - Name: http_request_duration_seconds, Value: 0.000003, Labels: [handler:HelloWorldHandler code:200]
package main
import (
"context"
"fmt"
"log"
"net/http"
"time"
)
type MetricCollector interface {
ObserveHistogram(ctx context.Context, name string, value float64, labels ...string)
SetGauge(ctx context.Context, name string, value float64, labels ...string)
IncrementCounter(ctx context.Context, name string, value int, labels ...string)
}
type ContextKey string
const MetricCollectorKey ContextKey = "metricCollector"
func WithMetricCollector(ctx context.Context, collector MetricCollector) context.Context {
return context.WithValue(ctx, MetricCollectorKey, collector)
}
func GetMetricCollector(ctx context.Context) MetricCollector {
collector, ok := ctx.Value(MetricCollectorKey).(MetricCollector)
if !ok {
return nil
}
return collector
}
type DemoMetricCollector struct {
counter int// You can add any necessary fields or dependencies here.
}
func NewDemoMetricCollector() MetricCollector {
// Initialize and return an instance of your metric collector.
return &DemoMetricCollector{}
}
func (c *DemoMetricCollector) ObserveHistogram(ctx context.Context, name string, value float64, labels ...string) {
// Implement the ObserveHistogram method to log metrics for demonstration purposes.
fmt.Printf("ObserveHistogram - Name: %s, Value: %f, Labels: %v\n", name, value, labels)
}
func (c *DemoMetricCollector) SetGauge(ctx context.Context, name string, value float64, labels ...string) {
// Implement the SetGauge method to log metrics for demonstration purposes.
fmt.Printf("SetGauge - Name: %s, Value: %f, Labels: %v\n", name, value, labels)
}
func (c *DemoMetricCollector) IncrementCounter(ctx context.Context, name string, value int, labels ...string) {
c.counter += value
// Implement the IncrementCounter method to log metrics for demonstration purposes.
fmt.Println("IncrementCounter")
fmt.Printf("IncrementCounter - Name: %s, Value: %f, Labels: %v\n", name, c.counter, labels)
}
func HelloWorldHandler(w http.ResponseWriter, r *http.Request) {
// Get the metric collector from the context.
ctx := r.Context()
metricCollector := GetMetricCollector(ctx)
defer func(startTime time.Time) {
if metricCollector != nil {
metricCollector.ObserveHistogram(ctx, "http_request_duration_seconds", time.Since(startTime).Seconds(), "handler:HelloWorldHandler", "code:200")
}
}(time.Now())
// Your application logic here.
w.Write([]byte("Hello, World!"))
}
func main() {
// Create a context with a metric collector.
metricCollector := NewDemoMetricCollector()
ctx := WithMetricCollector(context.Background(), metricCollector)
// Create an HTTP server and handle requests.
server := &http.Server{
Addr: ":8080",
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Pass the context with the metric collector to the handler.
HelloWorldHandler(w, r.WithContext(ctx))
}),
}
// Start the HTTP server.
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment