Skip to content

Commit de19217

Browse files
examples: improve simple main.go example (#1999)
Signed-off-by: dhanudhanushree <[email protected]>
1 parent 20355eb commit de19217

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

‎examples/simple/main.go‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,39 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
// A minimal example of how to include Prometheus instrumentation.
14+
// A minimal example of exposing Prometheus metrics from a Go application.
1515
package main
1616

1717
import (
1818
"flag"
1919
"log"
2020
"net/http"
2121

22-
"github.com/prometheus/client_golang/prometheus/collectors"
23-
2422
"github.com/prometheus/client_golang/prometheus"
23+
"github.com/prometheus/client_golang/prometheus/collectors"
2524
"github.com/prometheus/client_golang/prometheus/promhttp"
2625
)
2726

27+
// Address for exposing Prometheus metrics.
2828
var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
2929

3030
func main() {
3131
flag.Parse()
3232

33-
// Create non-global registry.
33+
// Create a custom registry instead of using the global default registry.
34+
// This helps isolate application metrics from automatically registered metrics.
3435
reg := prometheus.NewRegistry()
3536

36-
// Add go runtime metrics and process collectors.
37+
// Register Go runtime metrics (such as goroutine count
38+
// and GC statistics) and process-level metrics (such as
39+
// CPU and memory usage).
3740
reg.MustRegister(
3841
collectors.NewGoCollector(),
3942
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
4043
)
4144

42-
// Expose /metrics HTTP endpoint using the created custom registry.
45+
// Expose the registered metrics at /metrics for Prometheus scraping.
4346
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
47+
4448
log.Fatal(http.ListenAndServe(*addr, nil))
4549
}

0 commit comments

Comments
 (0)