metrics.go 10.4 KB
Newer Older
Z
zhj-luo 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (c) 2023 OceanBase
 * OCP Express is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

O
ob-robot 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
package stat

import (
	"context"
	"net/http"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	log "github.com/sirupsen/logrus"

	"github.com/oceanbase/obagent/config"
)

var (
	defaultGatherer prometheus.Gatherer = prometheus.DefaultGatherer
)

func RegisterStat(ctx context.Context) {
	registry := prometheus.NewRegistry()
	labels := prometheus.Labels{
		App:     Host,
		Process: config.CurProcess,
		SvrIP:   HostIP,
	}
	log.WithContext(ctx).Infof("registry with labels: %+v", labels)
	registerer := prometheus.WrapRegistererWith(labels, registry)

	registerer.MustRegister(
		prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
		prometheus.NewGoCollector(),
		HttpRequestMillisecondsSummary,
		MonAgentPipelineReportMetricsTotal,
		MonAgentPipelineExecuteTotal,
		MonAgentPipelineExecuteSecondsTotal,
		MonAgentPluginExecuteTotal,
		MonAgentPluginExecuteSecondsTotal,
		MonitorAgentTableInputHistogram,
		InputCollectMetricsTotal,
		ProcessorProcessMetricsTotal,
		OutputWriteMetricsTotal,
		ExporterExportMetricsTotal,
		ExporterExportMetricsBytesTotal,
		HttpOutputPushMetricsBytesTotal,
		HttpOutputPushTotal,
		HttpOutputTaskSize,
		HttpOutputRetryTaskSize,
		HttpOutputTaskDiscardCount,
		HttpOutputSendTaskCount,
		HttpOutputSendFailedCount,
		HttpOutputSendRetryCount,
		HttpOutputSendMillisecondsSummary,
		SqlAuditInputCollectDataJumpedCount,
		MysqlOutputWriteTaskSize,
		MysqlOutputWriteSqlCount,
		MysqlOutputWriteSqlFailedCount,
		MysqlOutputContextTimeoutDiscardMetricsCount,
		MysqlOutputWriteSqlRetryCount,
		MysqlOutputWriteSqlMillisecondsSummary,
		LogTailerCount,
		LogTailerTailLineCount,
		LogTailerReadingFileOffset,
		LogTailerReadingFileId,
		LogTailerProcessQueueSize,
	)

	gatherPtr, _ := defaultGatherer.(*prometheus.Registry)
	*gatherPtr = *registry
}

var (

	// HttpRequestMillisecondsSummary gtto request seconds total
	HttpRequestMillisecondsSummary = prometheus.NewSummaryVec(
		prometheus.SummaryOpts{
			Name: "http_request_milliseconds_summary",
			Help: "The total elapsed time in millisecond of http request",
			Objectives: map[float64]float64{
				0.5:  0.05,
				0.9:  0.01,
				0.99: 0.001,
			},
		}, []string{HttpMethod, HttpStatus, HttpApiPath},
	)

	//MonAgentPipelineBufferMetrics monitor pipeline buffer metrics
	_ = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Name: "monagent_pipeline_buffer_metrics",
		Help: "The number of metrics currently in pipeline output buffer",
	}, []string{PluginNameKey})

	//MonAgentPipelineReportMetricsTotal monitor pipeline report message total
	MonAgentPipelineReportMetricsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "monagent_pipeline_report_metrics_total",
		Help: "The total number of metrics reported from the pipeline",
	}, []string{PluginNameKey})

	//MonAgentPipelineExecuteTotal monitor pipeline execute total
	MonAgentPipelineExecuteTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "monagent_pipeline_execute_total",
		Help: "The total number of a pipeline executed",
	}, []string{PluginNameKey, PluginExecuteStatusKey})

	//MonAgentPipelineExecuteSecondsTotal monitor pipeline execute seconds total
	MonAgentPipelineExecuteSecondsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "monagent_pipeline_execute_seconds_total",
		Help: "The total time in second of a pipeline executed",
	}, []string{PluginNameKey, PluginExecuteStatusKey})

	//MonAgentPluginExecuteTotal monitor plugin execute total
	MonAgentPluginExecuteTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "monagent_plugin_execute_total",
		Help: "The total number of a plugin executed",
	}, []string{PluginNameKey, PluginExecuteStatusKey, PluginTypeKey})

	//MonAgentPluginExecuteSecondsTotal monitor plugin execute seconds total
	MonAgentPluginExecuteSecondsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "monagent_plugin_execute_seconds_total",
		Help: "The total time in second of a plugin executed",
	}, []string{PluginNameKey, PluginExecuteStatusKey, PluginTypeKey})

	MonitorAgentTableInputHistogram = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: "monagent",
			Subsystem: "input",
			Name:      "sql_duration_seconds",
			Help:      "Bucketed histogram of execute time (s) of collecting sql",
			Buckets:   []float64{0.001, 0.005, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1},
		}, []string{PluginNameKey})

	InputCollectMetricsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "input_collect_metrics_total",
		Help: "The total metrics count that input collected",
	}, []string{PluginNameKey, PluginTypeKey})

	ProcessorProcessMetricsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "processor_process_metrics_total",
		Help: "The total metrics count that processor processed",
	}, []string{PluginNameKey, PluginTypeKey})

	OutputWriteMetricsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "output_write_metrics_total",
		Help: "The total metrics count that output written",
	}, []string{PluginNameKey, PluginTypeKey})

	ExporterExportMetricsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "exporter_export_metrics_total",
		Help: "The total metrics that exporter export",
	}, []string{PluginNameKey, PluginTypeKey, HttpApiPath})

	ExporterExportMetricsBytesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "exporter_export_metrics_bytes_total",
		Help: "The total metrics bytes that exporter export",
	}, []string{PluginNameKey, PluginTypeKey, HttpApiPath})

	HttpOutputPushMetricsBytesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "http_output_push_metrics_bytes_total",
		Help: "The total bytes of metrics that http output pushed",
	}, []string{HttpApiPath})

	HttpOutputPushTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "http_output_push_total",
		Help: "http output push count",
	}, []string{HttpApiPath})

	HttpOutputTaskSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Name: "http_output_task_size",
		Help: "http output task size",
	}, []string{HttpApiPath})

	HttpOutputRetryTaskSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Name: "http_output_retry_task_size",
		Help: "http output retry task size",
	}, []string{HttpApiPath})

	HttpOutputTaskDiscardCount = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "http_output_task_discard_total",
		Help: "http output discard task count",
	}, []string{HttpApiPath})

	HttpOutputSendTaskCount = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "http_output_send_task_total",
		Help: "http output send task count",
	}, []string{HttpApiPath})

	HttpOutputSendFailedCount = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "http_output_send_failed_total",
		Help: "http output send failed count",
	}, []string{HttpApiPath})

	HttpOutputSendRetryCount = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "http_output_send_retry_total",
		Help: "http output send retry count",
	}, []string{HttpApiPath})

	// contains retry data
	HttpOutputSendMillisecondsSummary = prometheus.NewSummaryVec(
		prometheus.SummaryOpts{
			Name: "http_output_send_milliseconds_summary",
			Help: "The total elapsed time of http output",
			Objectives: map[float64]float64{
				0.5:  0.05,
				0.9:  0.01,
				0.99: 0.001,
			},
		}, []string{HttpMethod, HttpStatus, HttpApiPath},
	)

	SqlAuditInputCollectDataJumpedCount = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "sql_audit_input_collect_data_jumped_total",
		Help: "sql audit input plugin collect data jumped total",
	}, []string{PluginNameKey})

	MysqlOutputWriteTaskSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Name: "mysql_output_write_task_size",
		Help: "mysql output write task chan size",
	}, []string{MysqlOutputTaskNameKey})

	MysqlOutputWriteSqlCount = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "mysql_output_write_sql_count",
		Help: "mysql output write sql total",
	}, []string{MysqlOutputTableNameKey})

	MysqlOutputWriteSqlFailedCount = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "mysql_output_write_sql_failed_count",
		Help: "mysql output write sql failed total",
	}, []string{MysqlOutputTableNameKey})

	MysqlOutputContextTimeoutDiscardMetricsCount = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "mysql_output_context_timeout_discard_metrics_count",
		Help: "mysql output context timeout discard metrics total",
	}, []string{MysqlOutputMetricName})

	MysqlOutputWriteSqlRetryCount = prometheus.NewCounterVec(prometheus.CounterOpts{
		Name: "mysql_output_write_sql_retry_count",
		Help: "mysql output write sql retry total",
	}, []string{MysqlOutputTableNameKey})

	// MysqlOutputWriteSqlMillisecondsSummary write sql seconds total
	MysqlOutputWriteSqlMillisecondsSummary = prometheus.NewSummaryVec(
		prometheus.SummaryOpts{
			Name: "mysql_output_write_sql_milliseconds_summary",
			Help: "The total elapsed time in millisecond of mysql output write sql",
			Objectives: map[float64]float64{
				0.5:  0.05,
				0.9:  0.01,
				0.99: 0.001,
			},
		}, []string{MysqlOutputTableNameKey},
	)

	LogTailerCount = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Name: "log_tailer_count",
		Help: "log tailer count",
	}, []string{LogFileName})
	LogTailerTailLineCount = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Name: "log_tailer_tail_line_count",
		Help: "log tailer tail line count",
	}, []string{LogFileName})
	LogTailerReadingFileOffset = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Name: "log_tailer_reading_file_offset",
		Help: "log tailer reading file offset",
	}, []string{LogFileName})
	LogTailerReadingFileId = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Name: "log_tailer_reading_file_id",
		Help: "log tailer reading file id",
	}, []string{LogFileName})
	LogTailerProcessQueueSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Name: "log_tailer_processing_queue_size",
		Help: "log tailer processing queue size",
	}, []string{LogFileName})
)

func PromHandler(_ http.Handler) http.Handler {
	return promhttp.HandlerFor(defaultGatherer, promhttp.HandlerOpts{})
}