-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ilham Syahid S <[email protected]>
- Loading branch information
1 parent
9f41262
commit 2377187
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} |