More than one struct field to channel


(Jose) #1

I have a struct like so

type collector struct {
TopActiveID1     				   *prometheus.Desc
TopActiveID2     				   *prometheus.Desc
TopActiveID3     				   *prometheus.Desc
TopActiveID4     				   *prometheus.Desc
TopActiveID5     				   *prometheus.Desc
TopActiveID6     				   *prometheus.Desc
TopActiveID7     				   *prometheus.Desc
TopActiveID8     				   *prometheus.Desc
TopActiveID9     				   *prometheus.Desc
TopActiveID10    				   *prometheus.Desc
}

What i need to do is pass each field to a channel. Is there a way to do it all at once without having to do the following?

func (collector *collector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.TopActiveID1
ch <- collector.TopActiveID2
ch <- collector.TopActiveID3
ch <- collector.TopActiveID4
ch <- collector.TopActiveID5
ch <- collector.TopActiveID6
ch <- collector.TopActiveID7
ch <- collector.TopActiveID8
ch <- collector.TopActiveID9
ch <- collector.TopActiveID10
}

(Johan Dahl) #2

If you define the field as an array of 10 values instead can you iterate over them.


(Jose) #3

Ok so went ahead an did this

type collector struct{
TopActive     []*prometheus.Desc
}

now before i even send it to channel i need to actually use it which i should have also included in my initial post but i am also doing this

func newCollector() *collector {
return &collector{
TopActiveID1:                       prometheus.NewDesc("topactiveid_1", "Shows top active item number 1", nil, nil),
TopActiveID2:                       prometheus.NewDesc("topactiveid_2", "Shows top active item number 2", nil, nil),
TopActiveID3:                       prometheus.NewDesc("topactiveid_3", "Shows top active item number 3", nil, nil),
TopActiveID4:                       prometheus.NewDesc("topactiveid_4", "Shows top active item number 4", nil, nil),
TopActiveID5:                       prometheus.NewDesc("topactiveid_5", "Shows top active item number 5", nil, nil),
TopActiveID6:                       prometheus.NewDesc("topactiveid_6", "Shows top active item number 6", nil, nil),
TopActiveID7:                       prometheus.NewDesc("topactiveid_7", "Shows top active item number 7", nil, nil),
TopActiveID8:                       prometheus.NewDesc("topactiveid_8", "Shows top active item number 8", nil, nil),
TopActiveID9:                       prometheus.NewDesc("topactiveid_9", "Shows top active item number 9", nil, nil),
TopActiveID10:                      prometheus.NewDesc("topactiveid_10", "Shows top active item number 10", nil, nil),
}
}

how would i go about adding all of these prometheus.NewDesc() to the array?


(Norbert Melzer) #4

Roughly like this:

func newCollector() *collector {
  return &collector{
    TopActive: []*prometheus.Desc{
      prometheus.NewDesc("topactiveid_1", "Shows top active item number 1", nil, nil),
      prometheus.NewDesc("topactiveid_2", "Shows top active item number 2", nil, nil),
      prometheus.NewDesc("topactiveid_3", "Shows top active item number 3", nil, nil),
      // and so on
    }
  }
}

(Jose) #5

Beautiful!!!

Thank you very much for that. I will attempt to figure out the rest on my own, but will return if i get stuck again.


(Jose) #6

Question, is there a way to add those prometheus.NewDesc in a loop somehow?


(Norbert Melzer) #7

If you can easily construct the strings you pass in the call it should be doable in a way similar to this:

func newCollector() *collector {
  n := 10
  descs := make([]*prometheus.Desc, n)

  for i := 1; i <= n; i++ {
    descs[i-1] = prometheus.NewDesc(
      fmt.Sprintf("topactiveid_%v", i),
      fmt.Sprintf("Shows top active item number %v", i),
      nil,
      nil,
    )
  }

  return &collector{ TopAcitve: descs }
}

(Jose) #8

Perfect!

Thank you very much for all your help! Learning a lot