package metrics import ( "fmt" "io" ) // PrometheusExporter 将内部指标导出为 Prometheus 文本格式。 type PrometheusExporter struct { metrics Metrics } // NewPrometheusExporter 创建 Prometheus 导出器。 func NewPrometheusExporter(m Metrics) *PrometheusExporter { return &PrometheusExporter{ metrics: m, } } // Export 将指标写入指定的 io.Writer。 func (e *PrometheusExporter) Export(w io.Writer) error { // 连接数 fmt.Fprintf(w, "# HELP nnet_connections Current number of connections\n") fmt.Fprintf(w, "# TYPE nnet_connections gauge\n") fmt.Fprintf(w, "nnet_connections %d\n", e.metrics.GetConnections()) // 请求数 fmt.Fprintf(w, "# HELP nnet_requests Total number of requests\n") fmt.Fprintf(w, "# TYPE nnet_requests counter\n") fmt.Fprintf(w, "nnet_requests %d\n", e.metrics.GetRequests()) // 错误数 fmt.Fprintf(w, "# HELP nnet_errors Total number of errors\n") fmt.Fprintf(w, "# TYPE nnet_errors counter\n") fmt.Fprintf(w, "nnet_errors %d\n", e.metrics.GetErrors()) // 接收字节数 fmt.Fprintf(w, "# HELP nnet_bytes_received Total bytes received\n") fmt.Fprintf(w, "# TYPE nnet_bytes_received counter\n") fmt.Fprintf(w, "nnet_bytes_received %d\n", e.metrics.GetBytesReceived()) // 发送字节数 fmt.Fprintf(w, "# HELP nnet_bytes_sent Total bytes sent\n") fmt.Fprintf(w, "# TYPE nnet_bytes_sent counter\n") fmt.Fprintf(w, "nnet_bytes_sent %d\n", e.metrics.GetBytesSent()) return nil }