Skip to content
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

fix(collector): fix the write overwrite caused by multiple collectors sharing a global variable DataSource #1857

Merged
merged 6 commits into from
Jan 18, 2024

Conversation

limowang
Copy link
Collaborator

@limowang limowang commented Jan 17, 2024

empiredan
empiredan previously approved these changes Jan 17, 2024
@@ -53,8 +53,7 @@ var GaugeMetricsMap map[string]prometheus.GaugeVec
var CounterMetricsMap map[string]prometheus.CounterVec
var SummaryMetricsMap map[string]prometheus.Summary

// DataSource 0 meta server, 1 replica server.
var DataSource int
// RoleByDataSource 0 meta server, 1 replica server.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It‘s corresponding to the variables in line 36, it would be better to mention the variable names instead of the magic numbers (0,1) again.

@@ -124,10 +123,10 @@ func getReplicaAddrs() ([]string, error) {
}

// Register all metrics.
func initMetrics() {
func initMetrics(dataSource int) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about making both the initMetrics function (some other functions are the same) and dataSource as members of MetricCollector, so the dataSource would not appear everywhere.

The global variables are the same.
var GaugeMetricsMap map[string]prometheus.GaugeVec
var CounterMetricsMap map[string]prometheus.CounterVec
var SummaryMetricsMap map[string]prometheus.Summary
var RoleByDataSource map[int]string
var TableNameByID map[string]string

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do the refactor in next patches, this PR looks good to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK,i will try.


return &Collector{detectInterval: detectInterval, detectTimeout: detectTimeout}
var role string
if dataSource == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could just change dataSource to role, and certainly change the type of dataSource to string instead of int which means, changing the type of following constants to string:

const (
        MetaServer    int = 0
        ReplicaServer int = 1
)

func updateMetric(metric Metric, endpoint string, level string, title string) {
role := RoleByDataSource[DataSource]
func (collector *Collector) updateMetric(metric Metric, endpoint string, level string, title string) {
role := collector.role
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the variable role be removed ?

@empiredan empiredan merged commit 09dc9e0 into apache:master Jan 18, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants