How to Export Custom Metrics from Go Applications

Export custom Go metrics by defining metric names in a Sample slice and reading values via runtime/metrics.Read.

Export custom metrics by defining a metrics.Sample slice with your metric names and calling metrics.Read to retrieve values. You can then format and send these values to your monitoring system.

import "runtime/metrics"

func getCustomMetrics() {
    sample := []metrics.Sample{
        {Name: "/custom/your_metric:bytes"},
    }
    metrics.Read(sample)
    for _, s := range sample {
        // Process s.Value (e.g., send to Prometheus)
    }
}

Note: Custom metrics must be registered in your code using metrics.Register (if supported in your Go version) or rely on existing runtime metrics like /godebug/non-default-behavior/<name>:events for GODEBUG settings.