...

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

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

     1  // Copyright 2015 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  // A minimal example of how to include Prometheus instrumentation.
    15  package main
    16  
    17  import (
    18  	"flag"
    19  	"log"
    20  	"net/http"
    21  
    22  	"github.com/prometheus/client_golang/prometheus/collectors"
    23  
    24  	"github.com/prometheus/client_golang/prometheus"
    25  	"github.com/prometheus/client_golang/prometheus/promhttp"
    26  )
    27  
    28  var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
    29  
    30  func main() {
    31  	flag.Parse()
    32  
    33  	// Create non-global registry.
    34  	reg := prometheus.NewRegistry()
    35  
    36  	// Add go runtime metrics and process collectors.
    37  	reg.MustRegister(
    38  		collectors.NewGoCollector(),
    39  		collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
    40  	)
    41  
    42  	// Expose /metrics HTTP endpoint using the created custom registry.
    43  	http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
    44  	log.Fatal(http.ListenAndServe(*addr, nil))
    45  }
    46  

View as plain text