Skip to content

Commit

Permalink
Address PR review comments
Browse files Browse the repository at this point in the history
- Add path pattern matching to PrometheusCodePathResponseInspector
- Move common Dockerfile steps into separate common Dockerfile
- Update Makefile to build common Dockerfile
  • Loading branch information
darinkrauss committed Jan 20, 2025
1 parent c941e7f commit 4db0134
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 52 deletions.
7 changes: 0 additions & 7 deletions Dockerfile.auth
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# Build
FROM golang:1.23.4-alpine AS build
RUN apk --no-cache update && apk --no-cache upgrade && apk --no-cache add ca-certificates tzdata git make
WORKDIR /go/src/github.com/tidepool-org/platform
COPY . .
RUN make init plugins-visibility build

# Production
FROM alpine:latest AS production
RUN apk --no-cache update && apk --no-cache upgrade && apk --no-cache add ca-certificates tzdata
Expand Down
7 changes: 0 additions & 7 deletions Dockerfile.blob
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# Build
FROM golang:1.23.4-alpine AS build
RUN apk --no-cache update && apk --no-cache upgrade && apk --no-cache add ca-certificates tzdata git make
WORKDIR /go/src/github.com/tidepool-org/platform
COPY . .
RUN make init plugins-visibility build

# Production
FROM alpine:latest AS production
RUN apk --no-cache update && apk --no-cache upgrade && apk --no-cache add ca-certificates tzdata
Expand Down
6 changes: 6 additions & 0 deletions Dockerfile.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Build
FROM golang:1.23.4-alpine AS build
RUN apk --no-cache update && apk --no-cache upgrade && apk --no-cache add ca-certificates tzdata git make
WORKDIR /go/src/github.com/tidepool-org/platform
COPY . .
RUN make init plugins-visibility build
7 changes: 0 additions & 7 deletions Dockerfile.data
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# Build
FROM golang:1.23.4-alpine AS build
RUN apk --no-cache update && apk --no-cache upgrade && apk --no-cache add ca-certificates tzdata git make
WORKDIR /go/src/github.com/tidepool-org/platform
COPY . .
RUN make init plugins-visibility build

# Production
FROM alpine:latest AS production
RUN apk --no-cache update && apk --no-cache upgrade && apk --no-cache add ca-certificates tzdata
Expand Down
7 changes: 0 additions & 7 deletions Dockerfile.migrations
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# Build
FROM golang:1.23.4-alpine AS build
RUN apk --no-cache update && apk --no-cache upgrade && apk --no-cache add ca-certificates tzdata git make
WORKDIR /go/src/github.com/tidepool-org/platform
COPY . .
RUN make init plugins-visibility build

# Production
FROM alpine:latest AS production
RUN apk --no-cache update && apk --no-cache upgrade && apk --no-cache add ca-certificates tzdata
Expand Down
7 changes: 0 additions & 7 deletions Dockerfile.prescription
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# Build
FROM golang:1.23.4-alpine AS build
RUN apk --no-cache update && apk --no-cache upgrade && apk --no-cache add ca-certificates tzdata git make
WORKDIR /go/src/github.com/tidepool-org/platform
COPY . .
RUN make init plugins-visibility build

# Production
FROM alpine:latest AS production
RUN apk --no-cache update && apk --no-cache upgrade && apk --no-cache add ca-certificates tzdata
Expand Down
7 changes: 0 additions & 7 deletions Dockerfile.task
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# Build
FROM golang:1.23.4-alpine AS build
RUN apk --no-cache update && apk --no-cache upgrade && apk --no-cache add ca-certificates tzdata git make
WORKDIR /go/src/github.com/tidepool-org/platform
COPY . .
RUN make init plugins-visibility build

# Production
FROM alpine:latest AS production
RUN apk --no-cache update && apk --no-cache upgrade && apk --no-cache add ca-certificates tzdata
Expand Down
7 changes: 0 additions & 7 deletions Dockerfile.tools
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# Build
FROM golang:1.23.4-alpine AS build
RUN apk --no-cache update && apk --no-cache upgrade && apk --no-cache add ca-certificates tzdata git make
WORKDIR /go/src/github.com/tidepool-org/platform
COPY . .
RUN make init plugins-visibility build

# Production
FROM mongo:6.0.19 AS production
# this statically set $HOME is non-ideal, but is to combat it being hardcoded to /data/db upstream
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ DOCKER_TAG_CMD ?= docker tag

TIMING_CMD ?=

SERVICES=auth blob data migrations prescription task tools
PLUGINS=redwood

ifdef TRAVIS_COMMIT
Expand Down Expand Up @@ -325,14 +326,16 @@ docker-dump:

docker:
ifdef DOCKER
@cd $(ROOT_DIRECTORY) && for DOCKER_FILE in $(shell ls -1 Dockerfile.*); do $(MAKE) docker-build DOCKER_FILE="$${DOCKER_FILE}" TIMESTAMP="$(TIMESTAMP)";done
@cd $(ROOT_DIRECTORY) && for DOCKER_FILE in $(shell ls -1 Dockerfile.*); do $(MAKE) docker-push DOCKER_FILE="$${DOCKER_FILE}" TIMESTAMP="$(TIMESTAMP)";done
@cd $(ROOT_DIRECTORY) && \
for SERVICE in $(SERVICES); do $(MAKE) docker-build DOCKER_FILE="Dockerfile.$${SERVICE}" TIMESTAMP="$(TIMESTAMP)"; done && \
for SERVICE in $(SERVICES); do $(MAKE) docker-push DOCKER_FILE="Dockerfile.$${SERVICE}" TIMESTAMP="$(TIMESTAMP)"; done
endif

docker-build: docker-dump
ifdef DOCKER
ifdef DOCKER_FILE
@cd $(ROOT_DIRECTORY) && \
$(TIMING_CMD) $(DOCKER_BUILD_CMD) --tag build --file "Dockerfile.build" . && \
$(TIMING_CMD) $(DOCKER_BUILD_CMD) --tag $(DOCKER_REPOSITORY) --file "$(DOCKER_FILE)" .
ifdef TRAVIS_BRANCH
ifdef TRAVIS_COMMIT
Expand Down
38 changes: 37 additions & 1 deletion request/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,50 @@ func (h *HeadersInspector) InspectResponse(res *http.Response) {

type PrometheusCodePathResponseInspector struct {
*prometheus.CounterVec
patternMux *http.ServeMux
}

// When there are only a few discrete paths possible, then no need to simplify via patterns.
//
// For example: /one, /two
func NewPrometheusCodePathResponseInspector(name string, help string) *PrometheusCodePathResponseInspector {
return NewPrometheusCodePathResponseInspectorWithPatterns(name, help)
}

// Where there are numerous discrete paths possible, then must simplify via patterns to prevent overwhelming Prometheus.
// If no patterns are specified, then all paths are recorded as-is. If one or more patterns are specified, but a path
// does not match one of the patterns, then the path is NOT captured by Prometheus. If you wish to match "all other" paths
// and records those paths as-is, then add the pattern PatternAny at the end of your patterns.
//
// Uses standard Go HTTP pattern matching. See https://go.dev/src/net/http/pattern.go.
//
// For example: /one/{id}, /two/{id}
func NewPrometheusCodePathResponseInspectorWithPatterns(name string, help string, patterns ...string) *PrometheusCodePathResponseInspector {
var patternMux *http.ServeMux

if len(patterns) > 0 {
patternMux = http.NewServeMux()
for _, pattern := range patterns {
patternMux.HandleFunc(pattern, func(http.ResponseWriter, *http.Request) {})
}
}

return &PrometheusCodePathResponseInspector{
CounterVec: prometheusPromauto.NewCounterVec(prometheus.CounterOpts{Name: name, Help: help}, []string{"code", "path"}),
patternMux: patternMux,
}
}

func (p *PrometheusCodePathResponseInspector) InspectResponse(res *http.Response) {
p.With(prometheus.Labels{"code": strconv.Itoa(res.StatusCode), "path": res.Request.URL.Path}).Inc()
path := res.Request.URL.Path
if p.patternMux != nil {
if _, pattern := p.patternMux.Handler(res.Request); pattern == "" {
return
} else if pattern != PatternAny {
path = pattern
}
}
p.With(prometheus.Labels{"code": strconv.Itoa(res.StatusCode), "path": path}).Inc()
}

const PatternAny = "/"

0 comments on commit 4db0134

Please sign in to comment.