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

add constant labels for the metrics (#385) #386

Merged
merged 20 commits into from
Aug 23, 2023
Merged

Conversation

qdongxu
Copy link
Contributor

@qdongxu qdongxu commented Jul 21, 2023

No description provided.

@qdongxu
Copy link
Contributor Author

qdongxu commented Jul 21, 2023

Looks like there's an issue in gopkd.in/yaml.v3@v3.0.1 .

with config:

http: # http probes
  - name: EaseProbe Github
    url: https://github.com/megaease/easeprobe
    labels:
      abc: xx
      def: y
      instance: github.com/megaeas
  - name: 163
    url: https://www.163.com

the lablels of 'EaseProbe Github' binds to '163' as well:

EaseProbe_http_status{abc="xx", def="y", endpoint="https://github.com/megaease/easeprobe", exported_instance="github.com/megaeas", instance="localhost:8181", job="easeprobe", name="EaseProbe Github"}
1
EaseProbe_http_status{abc="xx", def="y", endpoint="https://www.163.com", exported_instance="github.com/megaeas", instance="localhost:8181", job="easeprobe", name="163"}
1

@qdongxu
Copy link
Contributor Author

qdongxu commented Jul 21, 2023

After changing the order:

http: # http probes
  - name: 163
    url: https://www.163.com
  - name: EaseProbe Github
    url: https://github.com/megaease/easeprobe
    labels:
      abc: xx
      def: y
      instance: github.com/megaeas

the labels was overwritten into empty:

EaseProbe_http_status{endpoint="https://github.com/megaease/easeprobe", instance="localhost:8181", job="easeprobe", name="EaseProbe Github"}
1
EaseProbe_http_status{endpoint="https://www.163.com", instance="localhost:8181", job="easeprobe", name="163"}
1

@codecov-commenter
Copy link

codecov-commenter commented Jul 24, 2023

Codecov Report

Patch coverage: 92.42% and project coverage change: -0.26% ⚠️

Comparison is base (5b3a33c) 99.68% compared to head (149c052) 99.42%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #386      +/-   ##
==========================================
- Coverage   99.68%   99.42%   -0.26%     
==========================================
  Files          83       84       +1     
  Lines        5722     5769      +47     
==========================================
+ Hits         5704     5736      +32     
- Misses         12       25      +13     
- Partials        6        8       +2     
Files Changed Coverage Δ
metric/prometheus.go 90.35% <67.64%> (-9.65%) ⬇️
probe/base/base.go 98.02% <73.33%> (-1.98%) ⬇️
conf/constlables.go 100.00% <100.00%> (ø)
probe/base/metrics.go 100.00% <100.00%> (ø)
probe/host/basic.go 100.00% <100.00%> (ø)
probe/host/cpu.go 100.00% <100.00%> (ø)
probe/host/disk.go 100.00% <100.00%> (ø)
probe/host/load.go 100.00% <100.00%> (ø)
probe/host/mem.go 100.00% <100.00%> (ø)
probe/http/http.go 100.00% <100.00%> (ø)
... and 9 more

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@qdongxu
Copy link
Contributor Author

qdongxu commented Jul 24, 2023

This was caused by the strategy of Prometheus, the Registry requires each metric ( identified by the metric name) should have the same label names. prometheus/client_golang#155 .

that is the config should be as below:

http: # http probes
  - name: 163
    url: https://www.163.com
    labels:
      abc: x163
      def: y163
      instance: 163
  - name: EaseProbe Github
    url: https://github.com/megaease/easeprobe
    labels:
      abc: xx
      def: y
      instance: megaease

but since the golang map is unordered, the configuration should be like below:

http: # http probes
  - name: 163
    url: https://www.163.com
    labels:
      - name: abc
         value: x163
     - name: def
       value: y163
    - name: instance
      value: 163
  - name: EaseProbe Github
    url: https://github.com/megaease/easeprobe
    labels:
      - name: abc
         value: xx
     - name: def
       value:  y
    - name: instance
      value: megaease

this looks verbose and error-prone.

@samanhappy
Copy link
Collaborator

Thanks for contributing this PR, there is a week from last commit, is it ready for review now?

@qdongxu
Copy link
Contributor Author

qdongxu commented Aug 10, 2023

@samanhappy , the solution is a little ugly. As per the Prometheus Collector constraint, a metric must have the same set of constant labels, which looks like below, all prober should define the three labels: abc, def, instance:

http: # http probes
  - name: 163
    url: https://www.163.com
    labels:
      - name: abc
         value: x163
     - name: def
       value: y163
    - name: instance
      value: 163
  - name: EaseProbe Github
    url: https://github.com/megaease/easeprobe
    labels:
      - name: abc
         value: xx
     - name: def
       value:  y
    - name: instance
      value: megaease

I've tried to use a collector for each prober but didn't find a way to bind all the collectors to one HTTP handler.

Anyway, this meets my reqirement and I am deploying it to the production environment. The code is ready for review if this solution is acceptable.

@samanhappy
Copy link
Collaborator

samanhappy commented Aug 10, 2023

You're correct. The current solution is overly verbose and susceptible to errors. We should strive to discover a more effective approach.

Is it possible to consolidate these customized labels into a singular Prometheus label field? This would enable us to maintain label consistency.

However, I'm uncertain about the ease of management within Prometheus consumers like Kibana. What are your thoughts on this?

@localvar
Copy link
Collaborator

this solution requires all metrics to have the same set of constant labels, but users may forget this requirement and get an unexpected result.

is it possible that we merge the constant labels of all metrics to one set, and set the value of a label to empty if a metric does not need it while keep the configured value if the metric need it?

@qdongxu
Copy link
Contributor Author

qdongxu commented Aug 15, 2023

This PR is ready for review. the merge solution works fine. it passes the collector's checking and the empty labels are discarded by the exporter.

@qdongxu qdongxu marked this pull request as ready for review August 15, 2023 16:51
docs/Manual.md Outdated
Comment on lines 281 to 285
labels:
- name: team
value: ease
- name: owner
value: megaease
Copy link
Collaborator

Choose a reason for hiding this comment

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

as we merged all constant labels into one set, using a map instead of an array will be more user friendly.

Suggested change
labels:
- name: team
value: ease
- name: owner
value: megaease
labels:
team: ease
owner: megaease

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

@qdongxu
Copy link
Contributor Author

qdongxu commented Aug 17, 2023

@samanhappy @localvar this PR is ready for review .

conf/conf.go Outdated
@@ -436,7 +436,9 @@ func isProbe(t reflect.Type) bool {
// AllProbers return all probers
func (conf *Conf) AllProbers() []probe.Prober {
log.Debugf("--------- Process the probers settings ---------")
return allProbersHelper(*conf)
all := allProbersHelper(*conf)
MergeConstLabels(all)
Copy link
Collaborator

Choose a reason for hiding this comment

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

AllProbers may be called more than once (even it is not currently, the name suggest it could be), which result in MergeConstLabels be called more than once, this is not necessary and may cause a bug in the future.

propose moving MergeConstLabels to somewhere that will only be called once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated.

"github.com/megaease/easeprobe/probe"
)

var constLabels = make(map[string]bool)
Copy link
Collaborator

Choose a reason for hiding this comment

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

why it is a global variable? I think we can make it a local variable by refactor the code below, this could make the code more maintainable and avoid potential bugs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was a digress of an original design. updated.

Comment on lines +199 to +201
for k, v := range constLabels {
labels[k] = v
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

should check if a label exists both in labels and constLabels and report the error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

Copy link
Collaborator

@localvar localvar left a comment

Choose a reason for hiding this comment

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

I'll approve this PR now, but please fix the github workflow errors.

@qdongxu
Copy link
Contributor Author

qdongxu commented Aug 17, 2023

fixed. but I cannot verify fully as some unit tests failed on mac m1 due to the bou.ke/monkey issue. please run a github workflow to verify it.

Copy link
Collaborator

@samanhappy samanhappy left a comment

Choose a reason for hiding this comment

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

LGTM,please fix all the code lint warnings and some verbose comments

Comment on lines 25 to 26
//
// Prometheus requires all metric of the same name have the same set of labels in a collector
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
//
// Prometheus requires all metric of the same name have the same set of labels in a collector
// Prometheus requires all metric of the same name have the same set of labels in a collector

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

func MergeConstLabels(ps []probe.Prober) {
var constLabels = make(map[string]bool)
for _, p := range ps {
for k, _ := range p.LabelMap() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

please fix this and the following code lint warnings

Copy link
Contributor Author

@qdongxu qdongxu Aug 19, 2023

Choose a reason for hiding this comment

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

updated.


// registries[len(registries)-1].MustRegister(m)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// registries[len(registries)-1].MustRegister(m)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated.

@zhao-kun zhao-kun merged commit b44d1ad into megaease:main Aug 23, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants