Metric Name + Labels + Timestamp + Value/Samplehttp_request_total indicates the current total number of HTTP requests received by the system).Metric Name/Labels can contain only ASCII characters, digits, underscores, and colons and must comply with the regular expression [a-zA-Z_:][a-zA-Z0-9_:]*. Counter, Gauge, Histogram, and Summary, as described below. For more information, please see METRIC TYPES.package orderimport ("github.com/prometheus/client_golang/prometheus""github.com/prometheus/client_golang/prometheus/promauto")// Define the counter object to be monitoredvar (opsProcessed = promauto.NewCounterVec(prometheus.CounterOpts{Name: "order_service_processed_orders_total",Help: "The total number of processed orders",}, []string{"status"}) // Processing status)// Process the orderfunc makeOrder() {opsProcessed.WithLabelValues("success").Inc() // Success// opsProcessed.WithLabelValues("fail").Inc() // Failure// Order placement business logic}
rate() function to get the order increase rate:rate(order_service_processed_orders_total[5m])
package orderimport ("github.com/prometheus/client_golang/prometheus""github.com/prometheus/client_golang/prometheus/promauto")// Define the gauge object to be monitoredvar (queueSize = promauto.NewGaugeVec(prometheus.GaugeOpts{Name: "order_service_order_queue_size",Help: "The size of order queue",}, []string{"type"}))type OrderQueue struct {queue chan string}func newOrderQueue() *OrderQueue {return &OrderQueue{queue: make(chan string,100),}}// Produce an order messagefunc (q *OrderQueue)produceOrder() {// Produce an order message// Increase the queue size by 1queueSize.WithLabelValues("make_order").Inc() // Order placement queue// queueSize.WithLabelValues("cancel_order").Inc() // Order cancellation queue}// Consume an order messagefunc (q *OrderQueue)consumeOrder() {// Consume an order message// Reduce the queue size by 1queueSize.WithLabelValues("make_order").Dec()}
order_service_order_queue_size
Bucket to generate a histogram, which can be processed subsequently and is generally used for duration monitoring. For example, you can use a histogram to calculate the latencies of P99, P95, and P50 and monitor the numbers of processed items. With histograms, you don't need to use counters to count items. In addition, you can use histograms to monitor metrics such as API response time and database access time.package orderimport ("net/http""time""github.com/prometheus/client_golang/prometheus""github.com/prometheus/client_golang/prometheus/promauto""github.com/prometheus/client_golang/prometheus/promhttp")// Define the summary object to be monitoredvar (opsProcessCost = promauto.NewSummaryVec(prometheus.SummaryOpts{Name: "order_service_process_order_duration",Help: "The order process duration",}, []string{"status"}))func makeOrder() {start := time.Now().UnixNano()// The order placement logic processing is completed, and the processing duration is recordeddefer opsProcessCost.WithLabelValues("success").Observe((float64)(time.Now().UnixNano() - start))// Order placement business logictime.Sleep(time.Second) // Simulate the processing duration}
order_service_processed_order_duration_sum / order_service_processed_order_duration_count
promhttp.Handler() to expose the metric tracking data to the HTTP service.package mainimport ("net/http""github.com/prometheus/client_golang/prometheus/promhttp")func main() {// Business code// Expose Prometheus metrics in the HTTP servicehttp.Handle("/metrics", promhttp.Handler())// Business code}
Explore to view the monitoring metric data as shown below. You can also customize Grafana monitoring dashboards.

Feedback