Skip to content

Commit

Permalink
fix: make server name public since it is also used in downstream metr…
Browse files Browse the repository at this point in the history
…ic middlewares;
  • Loading branch information
riandyrn committed Nov 26, 2024
1 parent ef6d9f4 commit 46aeade
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions metric/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const (
type BaseConfig struct {
// for initialization
meterProvider otelmetric.MeterProvider
serverName string

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

// Option specifies instrumentation configuration options.
Expand All @@ -45,7 +45,9 @@ 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)
}
Expand Down
2 changes: 1 addition & 1 deletion metric/request_duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions metric/requests_inflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ func NewRequestInFlight(cfg BaseConfig) func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// increase the number of requests in flight
counter.Add(r.Context(), 1, otelmetric.WithAttributes(
httpconv.ServerRequest(cfg.serverName, r)...,
httpconv.ServerRequest(cfg.ServerName, r)...,
))

// 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)...,
httpconv.ServerRequest(cfg.ServerName, r)...,
))
})
}
Expand Down
2 changes: 1 addition & 1 deletion metric/response_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 46aeade

Please sign in to comment.