Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set 0 for otel metrics during registration #1352

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/refinery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,16 @@ func main() {
"messages_sent": "counter",
"response_decode_errors": "counter",
}

for name, typ := range libhoneyMetricsName {
upstreamMetricsRecorder.Register(name, typ)
peerMetricsRecorder.Register(name, typ)
}

// Register metrics after the metrics object has been created
peerTransmission.RegisterMetrics()
upstreamTransmission.RegisterMetrics()

metricsSingleton.Store("UPSTREAM_BUFFER_SIZE", float64(c.GetUpstreamBufferSize()))
metricsSingleton.Store("PEER_BUFFER_SIZE", float64(c.GetPeerBufferSize()))

Expand Down
10 changes: 10 additions & 0 deletions metrics/otel_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (o *OTelMetrics) Start() error {
if err != nil {
return err
}

o.gauges[name] = g

name = "memory_inuse"
Expand Down Expand Up @@ -185,13 +186,17 @@ func (o *OTelMetrics) Register(name string, metricType string) {
o.lock.Lock()
defer o.lock.Unlock()

ctx := context.Background()
switch metricType {
case "counter":
ctr, err := o.meter.Int64Counter(name)
if err != nil {
o.Logger.Error().WithString("name", name).Logf("failed to create counter")
return
}
// Initialize the counter to 0 so that it will be reported
ctr.Add(ctx, 0)

o.counters[name] = ctr
case "gauge":
var f metric.Float64Callback = func(_ context.Context, result metric.Float64Observer) error {
Expand All @@ -213,20 +218,25 @@ func (o *OTelMetrics) Register(name string, metricType string) {
o.Logger.Error().WithString("name", name).Logf("failed to create gauge")
return
}

o.gauges[name] = g
case "histogram":
h, err := o.meter.Float64Histogram(name)
if err != nil {
o.Logger.Error().WithString("name", name).Logf("failed to create histogram")
return
}

h.Record(ctx, 0)
o.histograms[name] = h
case "updown":
ud, err := o.meter.Int64UpDownCounter(name)
if err != nil {
o.Logger.Error().WithString("name", name).Logf("failed to create updown counter")
return
}

ud.Add(ctx, 0)
o.updowns[name] = ud
default:
o.Logger.Error().WithString("type", metricType).Logf("unknown metric type")
Expand Down
2 changes: 2 additions & 0 deletions transmit/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ func (m *MockTransmission) Flush() {
defer m.Mux.Unlock()
m.Events = m.Events[:0]
}

func (m *MockTransmission) RegisterMetrics() {}
18 changes: 12 additions & 6 deletions transmit/transmit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Transmission interface {
EnqueueSpan(ev *types.Span)
// Flush flushes the in-flight queue of all events and spans
Flush()

RegisterMetrics()
}

const (
Expand Down Expand Up @@ -65,12 +67,6 @@ func (d *DefaultTransmission) Start() error {
libhoney.UserAgentAddition = "refinery/" + d.Version
})

d.Metrics.Register(counterEnqueueErrors, "counter")
d.Metrics.Register(counterResponse20x, "counter")
d.Metrics.Register(counterResponseErrors, "counter")
d.Metrics.Register(updownQueuedItems, "updown")
d.Metrics.Register(histogramQueueTime, "histogram")

processCtx, canceler := context.WithCancel(context.Background())
d.responseCanceler = canceler
go d.processResponses(processCtx, d.LibhClient.TxResponses())
Expand Down Expand Up @@ -141,6 +137,16 @@ func (d *DefaultTransmission) Flush() {
d.LibhClient.Flush()
}

// RegisterMetrics registers the metrics used by the DefaultTransmission.
// it should be called after the metrics object has been created.
func (d *DefaultTransmission) RegisterMetrics() {
d.Metrics.Register(counterEnqueueErrors, "counter")
d.Metrics.Register(counterResponse20x, "counter")
d.Metrics.Register(counterResponseErrors, "counter")
d.Metrics.Register(updownQueuedItems, "updown")
d.Metrics.Register(histogramQueueTime, "histogram")
}

func (d *DefaultTransmission) Stop() error {
// signal processResponses to stop
if d.responseCanceler != nil {
Expand Down
Loading