Skip to content

Commit

Permalink
Merge pull request #77 from riandyrn/fix/make-meter-public
Browse files Browse the repository at this point in the history
Fix: Make Metric Meter Public
  • Loading branch information
riandyrn authored Nov 26, 2024
2 parents 4f3fc39 + 7de8e2f commit 5f3848a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
10 changes: 6 additions & 4 deletions metric/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type BaseConfig struct {
meterProvider otelmetric.MeterProvider

// actual config state
meter otelmetric.Meter
serverName string
Meter otelmetric.Meter
ServerName string
}

// Option specifies instrumentation configuration options.
Expand All @@ -45,15 +45,17 @@ func WithMeterProvider(provider otelmetric.MeterProvider) Option {

func NewBaseConfig(serverName string, opts ...Option) BaseConfig {
// init base config
cfg := BaseConfig{}
cfg := BaseConfig{
ServerName: serverName,
}
for _, opt := range opts {
opt.apply(&cfg)
}

if cfg.meterProvider == nil {
cfg.meterProvider = otel.GetMeterProvider()
}
cfg.meter = cfg.meterProvider.Meter(
cfg.Meter = cfg.meterProvider.Meter(
ScopeName,
otelmetric.WithSchemaURL(semconv.SchemaURL),
otelmetric.WithInstrumentationVersion(Version()),
Expand Down
4 changes: 2 additions & 2 deletions metric/request_duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (

func NewRequestDurationMillis(cfg BaseConfig) func(next http.Handler) http.Handler {
// init metric, here we are using histogram for capturing request duration
histogram, err := cfg.meter.Int64Histogram(
histogram, err := cfg.Meter.Int64Histogram(
metricNameRequestDurationMs,
otelmetric.WithDescription(metricDescRequestDurationMs),
otelmetric.WithUnit(metricUnitRequestDurationMs),
Expand All @@ -40,7 +40,7 @@ func NewRequestDurationMillis(cfg BaseConfig) func(next http.Handler) http.Handl
r.Context(),
int64(duration.Milliseconds()),
otelmetric.WithAttributes(
httpconv.ServerRequest(cfg.serverName, r)...,
httpconv.ServerRequest(cfg.ServerName, r)...,
),
)
})
Expand Down
13 changes: 6 additions & 7 deletions metric/requests_inflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
// [RequestInFlight] is a metrics recorder for recording the number of requests in flight.
func NewRequestInFlight(cfg BaseConfig) func(next http.Handler) http.Handler {
// init metric, here we are using counter for capturing request in flight
counter, err := cfg.meter.Int64UpDownCounter(
counter, err := cfg.Meter.Int64UpDownCounter(
metricNameRequestInFlight,
otelmetric.WithDescription(metricDescRequestInFlight),
otelmetric.WithUnit(metricUnitRequestInFlight),
Expand All @@ -28,18 +28,17 @@ func NewRequestInFlight(cfg BaseConfig) func(next http.Handler) http.Handler {

return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// define metric attributes
attrs := otelmetric.WithAttributes(httpconv.ServerRequest(cfg.ServerName, r)...)

// increase the number of requests in flight
counter.Add(r.Context(), 1, otelmetric.WithAttributes(
httpconv.ServerRequest(cfg.serverName, r)...,
))
counter.Add(r.Context(), 1, attrs)

// execute next http handler
next.ServeHTTP(w, r)

// decrease the number of requests in flight
counter.Add(r.Context(), -1, otelmetric.WithAttributes(
httpconv.ServerRequest(cfg.serverName, r)...,
))
counter.Add(r.Context(), -1, attrs)
})
}
}
4 changes: 2 additions & 2 deletions metric/response_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (

func NewResponseSizeBytes(cfg BaseConfig) func(next http.Handler) http.Handler {
// init metric, here we are using histogram for capturing response size
histogram, err := cfg.meter.Int64Histogram(
histogram, err := cfg.Meter.Int64Histogram(
metricNameResponseSizeBytes,
otelmetric.WithDescription(metricDescResponseSizeBytes),
otelmetric.WithUnit(metricUnitResponseSizeBytes),
Expand All @@ -39,7 +39,7 @@ func NewResponseSizeBytes(cfg BaseConfig) func(next http.Handler) http.Handler {
r.Context(),
int64(rrw.writtenBytes),
otelmetric.WithAttributes(
httpconv.ServerRequest(cfg.serverName, r)...,
httpconv.ServerRequest(cfg.ServerName, r)...,
),
)
})
Expand Down

0 comments on commit 5f3848a

Please sign in to comment.