Skip to content

Commit

Permalink
chore: add proxied-by header
Browse files Browse the repository at this point in the history
  • Loading branch information
smrz2001 committed Nov 26, 2024
1 parent 368539b commit 1bfa322
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 2 additions & 0 deletions common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/3box/go-proxy/common/logging"
)

const ServiceName = "go-proxy"

const (
defaultProxyListenPort = "8080"
defaultMetricsListenPort = "9464"
Expand Down
9 changes: 4 additions & 5 deletions common/metric/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ import (

"github.com/gin-gonic/gin"

"github.com/3box/go-proxy/common/config"
"github.com/3box/go-proxy/common/logging"
)

const serviceName = "go-proxy"

var _ MetricService = &otelMetricService{}

type otelMetricService struct {
Expand All @@ -41,7 +40,7 @@ func NewOTelMetricService() (MetricService, error) {
sdk.WithReader(exporter),
)

meter := provider.Meter(serviceName)
meter := provider.Meter(config.ServiceName)

return &otelMetricService{
meter: meter,
Expand Down Expand Up @@ -80,7 +79,7 @@ func (_this otelMetricService) RecordRequest(ctx context.Context, name, method,
normalizedPath := normalizePath(path)

counter, err := _this.meter.Int64Counter(
fmt.Sprintf("%s_%s_requests_total", serviceName, name),
fmt.Sprintf("%s_%s_requests_total", config.ServiceName, name),
metric.WithDescription("Total number of requests received"),
)
if err != nil {
Expand All @@ -107,7 +106,7 @@ func (_this otelMetricService) RecordDuration(ctx context.Context, name string,
}

histogram, err := _this.meter.Float64Histogram(
fmt.Sprintf("%s_%s_duration_seconds", serviceName, name),
fmt.Sprintf("%s_%s_duration_seconds", config.ServiceName, name),
metric.WithDescription("Duration of operation in seconds"),
)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions controllers/proxy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ func (_this proxyController) handleRequest(reqCtx requestContext) {
reqCtx.ginContext.Header(k, v)
}
}
// Add a header indicating that this request was proxied
reqCtx.ginContext.Header("X-Proxied-By", config.ServiceName)
reqCtx.ginContext.Data(resp.StatusCode, resp.Header.Get("Content-Type"), respBody)
}
}
Expand Down

0 comments on commit 1bfa322

Please sign in to comment.