func NewPrometheusCollector()

in pkg/metrics/prometheus.go [76:188]


func NewPrometheusCollector() Collector {
	if globalCollector != nil {
		return globalCollector
	}
	podName := os.Getenv("POD_NAME")
	podNamespace := os.Getenv("POD_NAMESPACE")
	if podNamespace == "" {
		podNamespace = "default"
	}
	constLabels := prometheus.Labels{
		"controller_pod":       podName,
		"controller_namespace": podNamespace,
	}

	collector := &collector{
		isLeader: prometheus.NewGauge(
			prometheus.GaugeOpts{
				Name:        "is_leader",
				Namespace:   _namespace,
				Help:        "Whether the role of controller instance is leader",
				ConstLabels: constLabels,
			},
		),
		apisixCodes: prometheus.NewGaugeVec(
			prometheus.GaugeOpts{
				Name:        "apisix_status_codes",
				Namespace:   _namespace,
				Help:        "Status codes of requests to APISIX",
				ConstLabels: constLabels,
			},
			[]string{"resource", "status_code"},
		),
		apisixLatency: prometheus.NewSummaryVec(
			prometheus.SummaryOpts{
				Namespace:   _namespace,
				Name:        "apisix_request_latencies",
				Help:        "Request latencies with APISIX",
				ConstLabels: constLabels,
			},
			[]string{"operation"},
		),
		apisixRequests: prometheus.NewCounterVec(
			prometheus.CounterOpts{
				Namespace:   _namespace,
				Name:        "apisix_requests",
				Help:        "Number of requests to APISIX",
				ConstLabels: constLabels,
			},
			[]string{"resource", "op"},
		),
		checkClusterHealth: prometheus.NewCounterVec(
			prometheus.CounterOpts{
				Namespace:   _namespace,
				Name:        "check_cluster_health_total",
				Help:        "Number of cluster health check operations",
				ConstLabels: constLabels,
			},
			[]string{"name"},
		),
		syncOperation: prometheus.NewCounterVec(
			prometheus.CounterOpts{
				Namespace:   _namespace,
				Name:        "sync_operation_total",
				Help:        "Number of sync operations",
				ConstLabels: constLabels,
			},
			[]string{"resource", "result"},
		),
		cacheSyncOperation: prometheus.NewCounterVec(
			prometheus.CounterOpts{
				Namespace:   _namespace,
				Name:        "cache_sync_total",
				Help:        "Number of cache sync operations",
				ConstLabels: constLabels,
			},
			[]string{"result"},
		),
		controllerEvents: prometheus.NewCounterVec(
			prometheus.CounterOpts{
				Namespace:   _namespace,
				Name:        "events_total",
				Help:        "Number of events handled by the controller",
				ConstLabels: constLabels,
			},
			[]string{"operation", "resource"},
		),
	}

	// Since we use the DefaultRegisterer, in test cases, the metrics
	// might be registered duplicately, unregister them before re register.
	prometheus.Unregister(collector.isLeader)
	prometheus.Unregister(collector.apisixCodes)
	prometheus.Unregister(collector.apisixLatency)
	prometheus.Unregister(collector.apisixRequests)
	prometheus.Unregister(collector.checkClusterHealth)
	prometheus.Unregister(collector.syncOperation)
	prometheus.Unregister(collector.cacheSyncOperation)
	prometheus.Unregister(collector.controllerEvents)

	prometheus.MustRegister(
		collector.isLeader,
		collector.apisixCodes,
		collector.apisixLatency,
		collector.apisixRequests,
		collector.checkClusterHealth,
		collector.syncOperation,
		collector.cacheSyncOperation,
		collector.controllerEvents,
	)

	globalCollector = collector
	return collector
}