Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thampiotr committed Jan 30, 2025
1 parent 6ef1050 commit 3aa11a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
17 changes: 9 additions & 8 deletions internal/component/discovery/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,23 @@ func ComponentTargetsToPromTargetGroups(jobName string, tgs []Target) map[string
targetsWithCommonGroupLabels[fp] = append(targetsWithCommonGroupLabels[fp], t)
}

allGroups := make(map[string][]*targetgroup.Group, len(targetsWithCommonGroupLabels))
allGroups := make([]*targetgroup.Group, 0, len(targetsWithCommonGroupLabels))

groupIndex := 0
for _, targetsInGroup := range targetsWithCommonGroupLabels {
groupName := fmt.Sprintf("%s_%d", jobName, groupIndex)
groupTargets := make([]commonlabels.LabelSet, len(targetsInGroup))
sharedLabels := targetsInGroup[0].group // all have the same group labels.
individualLabels := make([]commonlabels.LabelSet, len(targetsInGroup))
for i, target := range targetsInGroup {
groupTargets[i] = target.own
individualLabels[i] = target.own
}
promGroup := &targetgroup.Group{
Source: groupName,
Labels: targetsInGroup[0].group,
Source: fmt.Sprintf("%s_part_%d", jobName, groupIndex),
Labels: sharedLabels,
Targets: individualLabels,
}
allGroups[groupName] = append(allGroups[groupName], promGroup)
allGroups = append(allGroups, promGroup)
}
return allGroups
return map[string][]*targetgroup.Group{jobName: allGroups}
}

var EmptyTarget = Target{
Expand Down
30 changes: 20 additions & 10 deletions internal/component/prometheus/scrape/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,16 +480,26 @@ func (c *Component) DebugInfo() interface{} {
}

func (c *Component) populatePromLabels(targets []discovery.Target, jobName string, args Arguments) []*scrape.Target {
// We need to call scrape.TargetsFromGroup to reuse the rather complex logic of populating labels on targets.
allTargets := make([]*scrape.Target, 0, len(targets))
lb := labels.NewBuilder(labels.EmptyLabels())
promTargets, errs := scrape.TargetsFromGroup(
discovery.ComponentTargetsToPromTargetGroups(jobName, targets)[jobName][0],
getPromScrapeConfigs(c.opts.ID, args),
false, /* noDefaultScrapePort - always false in this component */
make([]*scrape.Target, len(targets)), /* targets slice to reuse */
lb,
)
for _, err := range errs {
level.Warn(c.opts.Logger).Log("msg", "error while populating labels of targets using prom config", "err", err)
groups := discovery.ComponentTargetsToPromTargetGroups(jobName, targets)
for _, tgs := range groups {
for _, tg := range tgs {
promTargets, errs := scrape.TargetsFromGroup(
tg,
getPromScrapeConfigs(jobName, args),
false, /* noDefaultScrapePort - always false in this component */
make([]*scrape.Target, len(targets)), /* targets slice to reuse */
lb,
)
lb.Reset(labels.EmptyLabels())
for _, err := range errs {
level.Warn(c.opts.Logger).Log("msg", "error while populating labels of targets using prom config", "err", err)
}
allTargets = append(allTargets, promTargets...)
}
}
return promTargets

return allTargets
}

0 comments on commit 3aa11a2

Please sign in to comment.