Skip to content

Commit

Permalink
Use a different struct for scrape target serialization (#3417)
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm authored Nov 4, 2024
1 parent 2389f94 commit ad83cb9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 40 deletions.
13 changes: 11 additions & 2 deletions cmd/otel-allocator/server/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func BenchmarkTargetItemsJSONHandler(b *testing.B) {
},
}
for _, tc := range tests {
data := makeNTargetItems(*random, tc.numTargets, tc.numLabels)
data := makeNTargetJSON(*random, tc.numTargets, tc.numLabels)
b.Run(fmt.Sprintf("%d_targets_%d_labels", tc.numTargets, tc.numLabels), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -242,7 +242,7 @@ func makeNCollectorJSON(random rand.Rand, numCollectors, numItems int) map[strin
for i := 0; i < numCollectors; i++ {
items[randSeq(random, 20)] = collectorJSON{
Link: randSeq(random, 120),
Jobs: makeNTargetItems(random, numItems, 50),
Jobs: makeNTargetJSON(random, numItems, 50),
}
}
return items
Expand All @@ -261,6 +261,15 @@ func makeNTargetItems(random rand.Rand, numItems, numLabels int) []*target.Item
return items
}

func makeNTargetJSON(random rand.Rand, numItems, numLabels int) []*targetJSON {
items := makeNTargetItems(random, numItems, numLabels)
targets := make([]*targetJSON, numItems)
for i := 0; i < numItems; i++ {
targets[i] = targetJsonFromTargetItem(items[i])
}
return targets
}

func makeNNewLabels(random rand.Rand, n int) model.LabelSet {
labels := make(map[model.LabelName]model.LabelValue, n)
for i := 0; i < n; i++ {
Expand Down
50 changes: 40 additions & 10 deletions cmd/otel-allocator/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
promcommconfig "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
promconfig "github.com/prometheus/prometheus/config"
"gopkg.in/yaml.v2"

Expand All @@ -57,8 +58,17 @@ var (
)

type collectorJSON struct {
Link string `json:"_link"`
Jobs []*target.Item `json:"targets"`
Link string `json:"_link"`
Jobs []*targetJSON `json:"targets"`
}

type linkJSON struct {
Link string `json:"_link"`
}

type targetJSON struct {
TargetURL []string `json:"targets"`
Labels model.LabelSet `json:"labels"`
}

type Server struct {
Expand Down Expand Up @@ -263,9 +273,9 @@ func (s *Server) ReadinessProbeHandler(c *gin.Context) {
}

func (s *Server) JobHandler(c *gin.Context) {
displayData := make(map[string]target.LinkJSON)
displayData := make(map[string]linkJSON)
for _, v := range s.allocator.TargetItems() {
displayData[v.JobName] = target.LinkJSON{Link: v.Link.Link}
displayData[v.JobName] = linkJSON{Link: fmt.Sprintf("/jobs/%s/targets", url.QueryEscape(v.JobName))}
}
s.jsonHandler(c.Writer, displayData)
}
Expand Down Expand Up @@ -294,16 +304,16 @@ func (s *Server) TargetsHandler(c *gin.Context) {
if len(q) == 0 {
displayData := GetAllTargetsByJob(s.allocator, jobId)
s.jsonHandler(c.Writer, displayData)

} else {
tgs := s.allocator.GetTargetsForCollectorAndJob(q[0], jobId)
targets := GetAllTargetsByCollectorAndJob(s.allocator, q[0], jobId)
// Displays empty list if nothing matches
if len(tgs) == 0 {
if len(targets) == 0 {
s.jsonHandler(c.Writer, []interface{}{})
return
}
s.jsonHandler(c.Writer, tgs)
s.jsonHandler(c.Writer, targets)
}

}

func (s *Server) errorHandler(w http.ResponseWriter, err error) {
Expand All @@ -323,12 +333,25 @@ func (s *Server) jsonHandler(w http.ResponseWriter, data interface{}) {
func GetAllTargetsByJob(allocator allocation.Allocator, job string) map[string]collectorJSON {
displayData := make(map[string]collectorJSON)
for _, col := range allocator.Collectors() {
items := allocator.GetTargetsForCollectorAndJob(col.Name, job)
displayData[col.Name] = collectorJSON{Link: fmt.Sprintf("/jobs/%s/targets?collector_id=%s", url.QueryEscape(job), col.Name), Jobs: items}
targets := GetAllTargetsByCollectorAndJob(allocator, col.Name, job)
displayData[col.Name] = collectorJSON{
Link: fmt.Sprintf("/jobs/%s/targets?collector_id=%s", url.QueryEscape(job), col.Name),
Jobs: targets,
}
}
return displayData
}

// GetAllTargetsByCollector returns all the targets for a given collector and job.
func GetAllTargetsByCollectorAndJob(allocator allocation.Allocator, collectorName string, jobName string) []*targetJSON {
items := allocator.GetTargetsForCollectorAndJob(collectorName, jobName)
targets := make([]*targetJSON, len(items))
for i, item := range items {
targets[i] = targetJsonFromTargetItem(item)
}
return targets
}

// registerPprof registers the pprof handlers and either serves the requested
// specific profile or falls back to index handler.
func registerPprof(g *gin.RouterGroup) {
Expand All @@ -348,3 +371,10 @@ func registerPprof(g *gin.RouterGroup) {
}
})
}

func targetJsonFromTargetItem(item *target.Item) *targetJSON {
return &targetJSON{
TargetURL: item.TargetURL,
Labels: item.Labels,
}
}
28 changes: 14 additions & 14 deletions cmd/otel-allocator/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestServer_TargetsHandler(t *testing.T) {
allocator allocation.Allocator
}
type want struct {
items []*target.Item
items []*targetJSON
errString string
}
tests := []struct {
Expand All @@ -91,7 +91,7 @@ func TestServer_TargetsHandler(t *testing.T) {
allocator: leastWeighted,
},
want: want{
items: []*target.Item{},
items: []*targetJSON{},
},
},
{
Expand All @@ -105,7 +105,7 @@ func TestServer_TargetsHandler(t *testing.T) {
allocator: leastWeighted,
},
want: want{
items: []*target.Item{
items: []*targetJSON{
{
TargetURL: []string{"test-url"},
Labels: map[model.LabelName]model.LabelValue{
Expand All @@ -127,7 +127,7 @@ func TestServer_TargetsHandler(t *testing.T) {
allocator: leastWeighted,
},
want: want{
items: []*target.Item{
items: []*targetJSON{
{
TargetURL: []string{"test-url"},
Labels: map[model.LabelName]model.LabelValue{
Expand All @@ -149,7 +149,7 @@ func TestServer_TargetsHandler(t *testing.T) {
allocator: leastWeighted,
},
want: want{
items: []*target.Item{
items: []*targetJSON{
{
TargetURL: []string{"test-url"},
Labels: map[model.LabelName]model.LabelValue{
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestServer_TargetsHandler(t *testing.T) {
assert.EqualError(t, err, tt.want.errString)
return
}
var itemResponse []*target.Item
var itemResponse []*targetJSON
err = json.Unmarshal(bodyBytes, &itemResponse)
assert.NoError(t, err)
assert.ElementsMatch(t, tt.want.items, itemResponse)
Expand Down Expand Up @@ -555,27 +555,27 @@ func TestServer_JobHandler(t *testing.T) {
description string
targetItems map[string]*target.Item
expectedCode int
expectedJobs map[string]target.LinkJSON
expectedJobs map[string]linkJSON
}{
{
description: "nil jobs",
targetItems: nil,
expectedCode: http.StatusOK,
expectedJobs: make(map[string]target.LinkJSON),
expectedJobs: make(map[string]linkJSON),
},
{
description: "empty jobs",
targetItems: map[string]*target.Item{},
expectedCode: http.StatusOK,
expectedJobs: make(map[string]target.LinkJSON),
expectedJobs: make(map[string]linkJSON),
},
{
description: "one job",
targetItems: map[string]*target.Item{
"targetitem": target.NewItem("job1", "", model.LabelSet{}, ""),
},
expectedCode: http.StatusOK,
expectedJobs: map[string]target.LinkJSON{
expectedJobs: map[string]linkJSON{
"job1": newLink("job1"),
},
},
Expand All @@ -588,7 +588,7 @@ func TestServer_JobHandler(t *testing.T) {
"d": target.NewItem("job3", "", model.LabelSet{}, ""),
"e": target.NewItem("job3", "", model.LabelSet{}, "")},
expectedCode: http.StatusOK,
expectedJobs: map[string]target.LinkJSON{
expectedJobs: map[string]linkJSON{
"job1": newLink("job1"),
"job2": newLink("job2"),
"job3": newLink("job3"),
Expand All @@ -609,7 +609,7 @@ func TestServer_JobHandler(t *testing.T) {
assert.Equal(t, tc.expectedCode, result.StatusCode)
bodyBytes, err := io.ReadAll(result.Body)
require.NoError(t, err)
jobs := map[string]target.LinkJSON{}
jobs := map[string]linkJSON{}
err = json.Unmarshal(bodyBytes, &jobs)
require.NoError(t, err)
assert.Equal(t, tc.expectedJobs, jobs)
Expand Down Expand Up @@ -737,6 +737,6 @@ func TestServer_ScrapeConfigRespose(t *testing.T) {
}
}

func newLink(jobName string) target.LinkJSON {
return target.LinkJSON{Link: fmt.Sprintf("/jobs/%s/targets", url.QueryEscape(jobName))}
func newLink(jobName string) linkJSON {
return linkJSON{Link: fmt.Sprintf("/jobs/%s/targets", url.QueryEscape(jobName))}
}
18 changes: 4 additions & 14 deletions cmd/otel-allocator/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
package target

import (
"fmt"
"net/url"

"github.com/prometheus/common/model"
)

Expand All @@ -34,17 +31,11 @@ var (
endpointSliceTargetNameLabel model.LabelName = "__meta_kubernetes_endpointslice_address_target_name"
)

// LinkJSON This package contains common structs and methods that relate to scrape targets.
type LinkJSON struct {
Link string `json:"_link"`
}

type Item struct {
JobName string `json:"-"`
Link LinkJSON `json:"-"`
TargetURL []string `json:"targets"`
Labels model.LabelSet `json:"labels"`
CollectorName string `json:"-"`
JobName string
TargetURL []string
Labels model.LabelSet
CollectorName string
hash string
}

Expand Down Expand Up @@ -73,7 +64,6 @@ func (t *Item) GetNodeName() string {
func NewItem(jobName string, targetURL string, label model.LabelSet, collectorName string) *Item {
return &Item{
JobName: jobName,
Link: LinkJSON{Link: fmt.Sprintf("/jobs/%s/targets", url.QueryEscape(jobName))},
hash: jobName + targetURL + label.Fingerprint().String(),
TargetURL: []string{targetURL},
Labels: label,
Expand Down

0 comments on commit ad83cb9

Please sign in to comment.