MCPcopy Index your code
hub / github.com/coder/coder / collectMetrics

Method collectMetrics

agent/metrics.go:65–113  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

63}
64
65func (a *agent) collectMetrics(ctx context.Context) []*proto.Stats_Metric {
66 var collected []*proto.Stats_Metric
67
68 // Tailscale internal metrics
69 metrics := clientmetric.Metrics()
70 for _, m := range metrics {
71 if isIgnoredMetric(m.Name()) {
72 continue
73 }
74
75 collected = append(collected, &proto.Stats_Metric{
76 Name: m.Name(),
77 Type: asMetricType(m.Type()),
78 Value: float64(m.Value()),
79 })
80 }
81
82 metricFamilies, err := a.prometheusRegistry.Gather()
83 if err != nil {
84 a.logger.Error(ctx, "can't gather agent metrics", slog.Error(err))
85 return collected
86 }
87
88 for _, metricFamily := range metricFamilies {
89 for _, metric := range metricFamily.GetMetric() {
90 labels := toAgentMetricLabels(metric.Label)
91
92 switch {
93 case metric.Counter != nil:
94 collected = append(collected, &proto.Stats_Metric{
95 Name: metricFamily.GetName(),
96 Type: proto.Stats_Metric_COUNTER,
97 Value: metric.Counter.GetValue(),
98 Labels: labels,
99 })
100 case metric.Gauge != nil:
101 collected = append(collected, &proto.Stats_Metric{
102 Name: metricFamily.GetName(),
103 Type: proto.Stats_Metric_GAUGE,
104 Value: metric.Gauge.GetValue(),
105 Labels: labels,
106 })
107 default:
108 a.logger.Error(ctx, "unsupported metric type", slog.F("type", metricFamily.Type.String()))
109 }
110 }
111 }
112 return collected
113}
114
115func toAgentMetricLabels(metricLabels []*prompb.LabelPair) []*proto.Stats_Metric_Label {
116 if len(metricLabels) == 0 {

Callers 1

CollectMethod · 0.95

Calls 10

isIgnoredMetricFunction · 0.85
asMetricTypeFunction · 0.85
toAgentMetricLabelsFunction · 0.85
NameMethod · 0.65
TypeMethod · 0.65
ValueMethod · 0.45
ErrorMethod · 0.45
GetNameMethod · 0.45
GetValueMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected