-
Notifications
You must be signed in to change notification settings - Fork 436
feat: support dynamic label values and global metrics record. #2268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4463dfd
support global metric record and dynamic labels
shunjiazhu c160174
support global metric record and dynamic labels
shunjiazhu a3876f8
add runner label value for global metrics record
shunjiazhu a27b900
split metrics to different maps if prev or next has empty tags
shunjiazhu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,51 +31,179 @@ type MetricsRecord struct { | |
| MetricCollectors []MetricCollector | ||
| } | ||
|
|
||
| func (m *MetricsRecord) insertLabels(record map[string]string) { | ||
| labels := map[string]string{} | ||
| for _, label := range m.Labels { | ||
| labels[label.Key] = label.Value | ||
| } | ||
| labelsStr, _ := json.Marshal(labels) | ||
| record[MetricLabelPrefix] = string(labelsStr) | ||
| } | ||
|
|
||
| // RegisterMetricCollector is used for registering metric collector (vector). | ||
| func (m *MetricsRecord) RegisterMetricCollector(collector MetricCollector) { | ||
| m.Lock() | ||
| defer m.Unlock() | ||
| m.MetricCollectors = append(m.MetricCollectors, collector) | ||
| } | ||
|
|
||
| // ExportMetricRecords is used for exporting metrics records. | ||
| // It will replace Serialize in the future. | ||
| func (m *MetricsRecord) ExportMetricRecords() map[string]string { | ||
| // ExportMetricRecords exports all metrics bound to this metric record. | ||
| // The results may be a list of map[string]string, each map[string]string is a set of measurements has the same labels. | ||
| // for example: | ||
| // []{ | ||
| // {"counters":"{\"http_flusher_dropped_events\":\"3.0000\",\"http_flusher_flush_failure_count\":\"6.0000\",\"http_flusher_matched_events\":\"1.0000\",\"http_flusher_retry_count\":\"5.0000\",\"http_flusher_unmatched_events\":\"2.0000\"}","gauges":"{\"http_flusher_flush_latency_ns\":\"7.0000\"}","labels":"{\"PluginId\":\"13\",\"PluginType\":\"flusher_http\",\"RemoteURL\":\"http://localhost:8081\"}"} | ||
| // {"counters":"{\"http_flusher_status_code_count\":\"8.0000\"}","gauges":"{}","labels":"{\"PluginId\":\"13\",\"PluginType\":\"flusher_http\",\"RemoteURL\":\"http://localhost:8081\",\"status_code\":\"200\"}"} | ||
| // {"counters":"{\"http_flusher_status_code_count\":\"9.0000\"}","gauges":"{}","labels":"{\"PluginId\":\"13\",\"PluginType\":\"flusher_http\",\"RemoteURL\":\"http://localhost:8081\",\"status_code\":\"400\"}"} | ||
| // {"counters":"{\"http_flusher_error_count\":\"10.0000\"}","gauges":"{}","labels":"{\"PluginId\":\"13\",\"PluginType\":\"flusher_http\",\"RemoteURL\":\"http://localhost:8081\",\"level\":\"error\",\"reason\":\"timeout\"}"} | ||
| // {"counters":"{\"http_flusher_error_count\":\"11.0000\"}","gauges":"{}","labels":"{\"PluginId\":\"13\",\"PluginType\":\"flusher_http\",\"RemoteURL\":\"http://localhost:8081\",\"level\":\"warn\",\"reason\":\"retry\"}"} | ||
| // {"counters":"{\"http_flusher_error_count\":\"12.0000\"}","gauges":"{}","labels":"{\"PluginId\":\"13\",\"PluginType\":\"flusher_http\",\"RemoteURL\":\"http://localhost:8081\",\"level\":\"error\",\"reason\":\"dropped\"}"} | ||
| // } | ||
| // Note: | ||
| // A metric may have three levels of labels | ||
| // 1. MetricsRecord Level Const Labels, like PluginType=flusher_http, PluginId=1 | ||
| // 2. Metric Level Const Labels, for example, flusher_http may have a const label: RemoteURL=http://aliyun.com/write | ||
| // 3. Metric Level Dynamic Labels, like status_code=200, status_code=204 | ||
| func (m *MetricsRecord) ExportMetricRecords() []map[string]string { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 方便给一个前后对比吗?先前有问题的指标和修改之后的样子
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我晚点补一下 |
||
| m.RLock() | ||
| defer m.RUnlock() | ||
|
|
||
| record := map[string]string{} | ||
| counters := map[string]string{} | ||
| gauges := map[string]string{} | ||
| m.insertLabels(record) | ||
| res := []map[string]string{} | ||
|
|
||
| curCounters := map[string]string{} | ||
| currGauges := map[string]string{} | ||
| commonLabels := m.getConstLabels() | ||
| currLabels := m.getConstLabels() | ||
| currSeriesCount := 0 | ||
|
|
||
| for _, metricCollector := range m.MetricCollectors { | ||
| metrics := metricCollector.Collect() | ||
| for _, metric := range metrics { | ||
| singleMetric := metric.Export() | ||
| if len(singleMetric) == 0 { | ||
| metricVectors := metricCollector.Collect() | ||
|
|
||
| for _, metric := range metricVectors { | ||
| measurement := metric.Export() | ||
| if len(measurement) == 0 { | ||
| continue | ||
| } | ||
|
|
||
| if currSeriesCount > 0 && hasDifferentLabels(currLabels, commonLabels, measurement) { | ||
| res = append(res, buildRecord(currLabels, currGauges, curCounters)) | ||
| currLabels = m.getConstLabels() | ||
| curCounters = map[string]string{} | ||
| currGauges = map[string]string{} | ||
| currSeriesCount = 0 | ||
| } | ||
|
|
||
| currName := getMetriName(measurement) | ||
| if currName == "" { | ||
| continue | ||
| } | ||
| valueName := singleMetric[SelfMetricNameKey] | ||
| valueValue := singleMetric[valueName] | ||
|
|
||
| currValue := getMetricValue(measurement) | ||
| if currValue == "" { | ||
| continue | ||
| } | ||
|
|
||
| currMeasurementLabels := getLabels(measurement) | ||
| for key, value := range currMeasurementLabels { | ||
| currLabels[key] = value | ||
| } | ||
|
|
||
| if metric.Type() == CounterType { | ||
| counters[valueName] = valueValue | ||
| curCounters[currName] = currValue | ||
| currSeriesCount++ | ||
| } | ||
| if metric.Type() == GaugeType { | ||
| gauges[valueName] = valueValue | ||
| currGauges[currName] = currValue | ||
| currSeriesCount++ | ||
| } | ||
|
|
||
| } | ||
| } | ||
| countersStr, _ := json.Marshal(counters) | ||
| record[MetricCounterPrefix] = string(countersStr) | ||
|
|
||
| if currSeriesCount > 0 { | ||
| res = append(res, buildRecord(currLabels, currGauges, curCounters)) | ||
| } | ||
| return res | ||
| } | ||
|
|
||
| func (m *MetricsRecord) getConstLabels() map[string]string { | ||
| labels := map[string]string{} | ||
| for _, label := range m.Labels { | ||
| labels[label.Key] = label.Value | ||
| } | ||
| return labels | ||
| } | ||
|
|
||
| // hasDifferentLabels checks if the next measurement has different labels compared to current labels. | ||
| // 1. currLabels: Labels from the current metric instance | ||
| // 2. commonLabels: Shared labels that should be present in all metrics | ||
| // 3. nextMeasurement: The next measurement to check | ||
| func hasDifferentLabels(currLabels map[string]string, commonLabels map[string]string, nextMeasurement map[string]string) bool { | ||
| // Check if next measurement's dynamic labels differ from current labels | ||
|
|
||
| newLabelCount := 0 | ||
| for key, value := range nextMeasurement { | ||
| if !isLabel(key, nextMeasurement) { | ||
| continue | ||
| } | ||
|
|
||
| // If label exists but value differs, labels are different | ||
| currValue, exists := currLabels[key] | ||
| if !exists || currValue != value { | ||
| return true | ||
| } | ||
| newLabelCount++ | ||
| } | ||
|
|
||
| // Check if common labels differ from current labels. | ||
| // This only happens if the current metrics override the common labels. | ||
| for key, value := range commonLabels { | ||
| if value != currLabels[key] { | ||
| return true | ||
| } | ||
| if _, ok := nextMeasurement[key]; !ok { | ||
| newLabelCount++ | ||
| } | ||
| } | ||
|
|
||
| return newLabelCount != len(currLabels) | ||
| } | ||
|
|
||
| // getMetriName returns the name of the metric. | ||
| // singleMeasurement is a single measurement of a metric. | ||
| // for example, a measurement of "flusher_http_flush_count" may looks like: | ||
| // {__name__="flusher_http_flush_count", flusher_http_flush_count=1024, remote_url=http://localhost:8080/write, status_code=200} | ||
| // and its name is "flusher_http_flush_count". | ||
| func getMetriName(singleMeasurement map[string]string) string { | ||
| return singleMeasurement[SelfMetricNameKey] | ||
| } | ||
|
|
||
| // getMetricValue returns the value of the metric. | ||
| // for example, a measurement of "flusher_http_flush_count" may looks like: | ||
| // {__name__="flusher_http_flush_count", flusher_http_flush_count=1024, remote_url=http://localhost:8080/write, status_code=200} | ||
| // and its value is "1024". | ||
| func getMetricValue(singleMeasurement map[string]string) string { | ||
| return singleMeasurement[getMetriName(singleMeasurement)] | ||
| } | ||
|
|
||
| // getLabels returns the labels of the metric. | ||
| // for example, a measurement of "flusher_http_flush_count" may looks like: | ||
| // {__name__="flusher_http_flush_count", flusher_http_flush_count=1024, remote_url=http://localhost:8080/write, status_code=200} | ||
| // and its labels is {"remote_url": "http://localhost:8080/write", "status_code": "200"} | ||
| func getLabels(singleMeasurement map[string]string) map[string]string { | ||
| labels := map[string]string{} | ||
| for key, value := range singleMeasurement { | ||
| if isLabel(key, singleMeasurement) { | ||
| labels[key] = value | ||
| } | ||
| } | ||
| return labels | ||
| } | ||
|
|
||
| // isLabel returns true if the key is a label. | ||
| func isLabel(key string, singleMetric map[string]string) bool { | ||
| return key != SelfMetricNameKey && key != getMetriName(singleMetric) | ||
| } | ||
|
|
||
| func buildRecord(labels, gauges, counters map[string]string) map[string]string { | ||
| records := make(map[string]string, 3) | ||
| labelsStr, _ := json.Marshal(labels) | ||
| records[MetricLabelPrefix] = string(labelsStr) | ||
|
|
||
| gaugesStr, _ := json.Marshal(gauges) | ||
| record[MetricGaugePrefix] = string(gaugesStr) | ||
| return record | ||
| records[MetricGaugePrefix] = string(gaugesStr) | ||
|
|
||
| countersStr, _ := json.Marshal(counters) | ||
| records[MetricCounterPrefix] = string(countersStr) | ||
| return records | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是不是可以这样理解:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
指标是有类型的,go这边key是metric_category,value是agent、runner、这些。https://observability.cn/project/loongcollector/internal-metrics-description/#_top
具有单独label的Metric,是不是需要定义为plugin_source级?这块有考虑过吗