MCPcopy Index your code
hub / github.com/cloudquery/cloudquery / newMetricConsumer

Function newMetricConsumer

cli/internal/otel/receiver.go:85–185  ·  view source on GitHub ↗
(metricsFile writeSeekCloser, durationCallback tableDurationSetter, quit chan any, wg *sync.WaitGroup)

Source from the content-addressed store, hash-verified

83type tableDurationSetter func(string, time.Duration)
84
85func newMetricConsumer(metricsFile writeSeekCloser, durationCallback tableDurationSetter, quit chan any, wg *sync.WaitGroup) metricConsumer {
86 tableLock := sync.Mutex{}
87 metricsMap := make(map[string]*tableMetric)
88 ticker := time.NewTicker(20 * time.Second)
89
90 renderTable := func() {
91 tableLock.Lock()
92 metrics := maps.Values(metricsMap)
93 tableLock.Unlock()
94 _, err := metricsFile.Seek(0, 0)
95 if err != nil {
96 return
97 }
98 t := tablepkg.NewWriter()
99 t.SetOutputMirror(metricsFile)
100 t.AppendHeader(tablepkg.Row{"Table", "Duration", "Resources", "Errors", "Panics"})
101 slices.SortStableFunc(metrics, func(a, b *tableMetric) int {
102 if a.EndTime == nil && b.EndTime != nil {
103 return -1
104 }
105
106 if a.EndTime != nil && b.EndTime == nil {
107 return 1
108 }
109
110 return cmp.Compare(a.Table+a.ClientId, b.Table+b.ClientId)
111 })
112 for _, metrics := range metrics {
113 var duration time.Duration
114 switch {
115 case metrics.Duration != nil:
116 duration = time.Duration(*metrics.Duration * int64(time.Millisecond))
117 case metrics.StartTime != nil && metrics.EndTime != nil:
118 duration = metrics.EndTime.Sub(*metrics.StartTime)
119 case metrics.StartTime != nil:
120 duration = time.Since(*metrics.StartTime)
121 }
122 row := tablepkg.Row{
123 metrics.Table,
124 duration,
125 metrics.Resources,
126 metrics.Errors,
127 metrics.Panics,
128 }
129 if duration == 0 {
130 row[4] = "N/A"
131 }
132 t.AppendRow(row)
133 }
134 t.Render()
135 }
136
137 wg.Go(func() {
138 for {
139 select {
140 case <-ticker.C:
141 renderTable()
142 case <-quit:

Callers 1

StartOtelReceiverFunction · 0.85

Calls 2

SeekMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected