Skip to content

Commit

Permalink
feat: add response size metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Ilham Syahid S <[email protected]>
  • Loading branch information
ilhamsyahids committed Oct 13, 2024
1 parent 9f41262 commit 2377187
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions metrics/response_size.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package metrics

import (
"context"
"fmt"

"github.com/riandyrn/otelchi"
otelmetric "go.opentelemetry.io/otel/metric"
)

// [NewResponseSizeBytes] creates a new instance of [responseSizeBytes].
func NewResponseSizeBytes() otelchi.MetricsRecorder {
return &responseSizeBytes{}
}

// [responseSizeBytes] is a metrics recorder for recording response size in bytes.
type responseSizeBytes struct {
responseSizeBytes otelmetric.Int64Histogram
}

func (r *responseSizeBytes) RegisterMetric(ctx context.Context, cfg otelchi.RegisterMetricConfig) {
httpResponseSizeBytes, err := cfg.Meter.Int64Histogram("response_size_bytes")
if err != nil {
panic(fmt.Sprintf("unable to create response_size_bytes histogram: %v", err))
}
r.responseSizeBytes = httpResponseSizeBytes
}

func (r *responseSizeBytes) StartMetric(ctx context.Context, opts otelchi.MetricOpts) {}

func (r *responseSizeBytes) EndMetric(ctx context.Context, opts otelchi.MetricOpts) {
r.responseSizeBytes.Record(ctx,
int64(opts.ResponseData.WrittenBytes),
opts.Measurement,
)
}

0 comments on commit 2377187

Please sign in to comment.