...

Source file src/github.com/prometheus/client_golang/examples/gocollector/main.go

Documentation: github.com/prometheus/client_golang/examples/gocollector

     1  // Copyright 2022 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  //go:build go1.17
    15  // +build go1.17
    16  
    17  // A minimal example of how to include Prometheus instrumentation.
    18  package main
    19  
    20  import (
    21  	"flag"
    22  	"fmt"
    23  	"log"
    24  	"net/http"
    25  	"regexp"
    26  
    27  	"github.com/prometheus/client_golang/prometheus"
    28  	"github.com/prometheus/client_golang/prometheus/collectors"
    29  	"github.com/prometheus/client_golang/prometheus/promhttp"
    30  )
    31  
    32  var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
    33  
    34  func main() {
    35  	flag.Parse()
    36  
    37  	// Create a new registry.
    38  	reg := prometheus.NewRegistry()
    39  
    40  	// Add Go module build info.
    41  	reg.MustRegister(collectors.NewBuildInfoCollector())
    42  	reg.MustRegister(collectors.NewGoCollector(
    43  		collectors.WithGoCollectorRuntimeMetrics(collectors.GoRuntimeMetricsRule{Matcher: regexp.MustCompile("/.*")}),
    44  	))
    45  
    46  	// Expose the registered metrics via HTTP.
    47  	http.Handle("/metrics", promhttp.HandlerFor(
    48  		reg,
    49  		promhttp.HandlerOpts{
    50  			// Opt into OpenMetrics to support exemplars.
    51  			EnableOpenMetrics: true,
    52  		},
    53  	))
    54  	fmt.Println("Hello world from new Go Collector!")
    55  	log.Fatal(http.ListenAndServe(*addr, nil))
    56  }
    57  

View as plain text