-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from section-io/ee-211
Introduce response time p8s metrics for Section Modules
- Loading branch information
Showing
12 changed files
with
347 additions
and
520 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,15 +1,33 @@ | ||
FROM golang:1.19 | ||
FROM golang:1.23 | ||
|
||
ENV CGO_ENABLED=0 | ||
|
||
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.48.0 | ||
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.60.1 | ||
|
||
RUN go env GOCACHE | grep -xF '/root/.cache/go-build' | ||
RUN go env GOMODCACHE | grep -xF '/go/pkg/mod' | ||
|
||
WORKDIR /src | ||
|
||
COPY go.mod go.sum ./ | ||
RUN go mod download | ||
RUN --mount=type=cache,target=/go/pkg/mod/ \ | ||
--mount=type=cache,target=/root/.cache/go-build/ \ | ||
go mod download | ||
|
||
COPY . . | ||
|
||
RUN golangci-lint run ./... | ||
RUN go test -short -v ./... | ||
RUN --mount=type=cache,target=/go/pkg/mod/ \ | ||
--mount=type=cache,target=/root/.cache/go-build/ \ | ||
go build -v ./... | ||
|
||
RUN --mount=type=cache,target=/go/pkg/mod/ \ | ||
--mount=type=cache,target=/root/.cache/go-build/ \ | ||
golangci-lint run ./... | ||
|
||
RUN --mount=type=cache,target=/go/pkg/mod/ \ | ||
--mount=type=cache,target=/root/.cache/go-build/ \ | ||
go test -short -v ./... | ||
|
||
RUN --mount=type=cache,target=/go/pkg/mod/ \ | ||
--mount=type=cache,target=/root/.cache/go-build/ \ | ||
go test -run=BenchmarkOnlyNoTests -bench=. ./... |
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,42 @@ | ||
package metrics | ||
|
||
type SecondsMultiplier float64 | ||
|
||
const ( | ||
LogUnitSeconds = SecondsMultiplier(1.0) | ||
LogUnitMilliseconds = SecondsMultiplier(1.0 / 1000.0) | ||
LogUnitMicroseconds = SecondsMultiplier(1.0 / 1000000.0) | ||
) | ||
|
||
type LogFieldValueFunc func(map[string]interface{}) interface{} | ||
|
||
func FirstLevelLogField(fieldName string) LogFieldValueFunc { | ||
return func(log map[string]interface{}) interface{} { | ||
return log[fieldName] | ||
} | ||
} | ||
|
||
func SecondLevelLogField(firstLevelFieldName, secondLevelFieldName string) LogFieldValueFunc { | ||
return func(log map[string]interface{}) interface{} { | ||
first, ok := log[firstLevelFieldName] | ||
if !ok { | ||
return nil | ||
} | ||
m, ok := first.(map[string]interface{}) | ||
if !ok { | ||
return nil | ||
} | ||
return m[secondLevelFieldName] | ||
} | ||
} | ||
|
||
type Configuration struct { | ||
RequestTimeLogField LogFieldValueFunc | ||
RequestTimeLogUnit SecondsMultiplier | ||
} | ||
|
||
var defaultConfig = &Configuration{} | ||
|
||
func Configure(c *Configuration) { | ||
defaultConfig = c | ||
} |
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 |
---|---|---|
@@ -1,27 +1,28 @@ | ||
module github.com/section-io/module-metrics | ||
|
||
go 1.19 | ||
go 1.23 | ||
|
||
require ( | ||
github.com/mmcloughlin/geohash v0.10.0 | ||
github.com/pkg/errors v0.9.1 | ||
github.com/prometheus/client_golang v1.13.0 | ||
github.com/prometheus/common v0.37.0 | ||
github.com/stretchr/testify v1.8.0 | ||
github.com/prometheus/client_golang v1.20.1 | ||
github.com/prometheus/common v0.55.0 | ||
github.com/stretchr/testify v1.9.0 | ||
github.com/thedevsaddam/gojsonq/v2 v2.5.2 | ||
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e | ||
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa | ||
) | ||
|
||
require ( | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.1.2 // indirect | ||
github.com/cespare/xxhash/v2 v2.3.0 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect | ||
github.com/klauspost/compress v1.17.9 // indirect | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/prometheus/client_model v0.2.0 // indirect | ||
github.com/prometheus/procfs v0.8.0 // indirect | ||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect | ||
google.golang.org/protobuf v1.28.1 // indirect | ||
github.com/prometheus/client_model v0.6.1 // indirect | ||
github.com/prometheus/procfs v0.15.1 // indirect | ||
golang.org/x/sys v0.24.0 // indirect | ||
google.golang.org/protobuf v1.34.2 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
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,93 @@ | ||
package metrics | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
const statusLabel = "status" | ||
const statusBucketLabel = "status_bucket" | ||
const cacheHandlingLabel = "cache_handling" | ||
const varnishHandlingLabel = "varnish_handling" | ||
|
||
type requestTimeProcessor struct { | ||
vec *prometheus.HistogramVec | ||
logField LogFieldValueFunc | ||
logUnit SecondsMultiplier | ||
} | ||
|
||
type noopRequestTimeProcessor struct{} | ||
|
||
func (p *noopRequestTimeProcessor) Observe(_ map[string]string, _ map[string]interface{}) {} | ||
|
||
func newRequestTimeProcessor(vec *prometheus.HistogramVec, logField LogFieldValueFunc, logUnit SecondsMultiplier) *requestTimeProcessor { | ||
return &requestTimeProcessor{ | ||
vec: vec, | ||
logField: logField, | ||
logUnit: logUnit, | ||
} | ||
} | ||
|
||
func (p *requestTimeProcessor) Observe(requestLabels map[string]string, logline map[string]interface{}) { | ||
subject := p.logField(logline) | ||
floatValue, err := strconv.ParseFloat(fmt.Sprintf("%v", subject), 64) | ||
if err != nil { | ||
return | ||
} | ||
floatValue = floatValue * float64(p.logUnit) | ||
|
||
labels := make(map[string]string, len(requestLabels)) | ||
for label, labelValue := range requestLabels { | ||
histogramLabel := translateRequestLabelToHistogramLabel(label) | ||
if histogramLabel == "" { | ||
continue | ||
} | ||
switch histogramLabel { | ||
case statusBucketLabel: | ||
labels[histogramLabel] = statusBucket(labelValue) | ||
case cacheHandlingLabel: | ||
labels[histogramLabel] = translateCacheHandling(labelValue) | ||
} | ||
} | ||
|
||
p.vec.With(labels).Observe(floatValue) | ||
} | ||
|
||
func translateRequestLabelToHistogramLabel(requestLabel string) string { | ||
switch requestLabel { | ||
|
||
// translations | ||
case statusLabel: | ||
return statusBucketLabel | ||
case varnishHandlingLabel: | ||
// Varnish should emit metrics consistent with other caches | ||
return cacheHandlingLabel | ||
|
||
// allow list | ||
case statusBucketLabel: | ||
return requestLabel | ||
case cacheHandlingLabel: | ||
return requestLabel | ||
|
||
} | ||
return "" | ||
} | ||
|
||
func translateCacheHandling(handlingLogValue string) string { | ||
// allow list | ||
switch handlingLogValue { | ||
case "hit": | ||
return handlingLogValue | ||
case "miss": | ||
return handlingLogValue | ||
case "pass": | ||
return handlingLogValue | ||
case "synth": | ||
return handlingLogValue | ||
case "pipe": | ||
return handlingLogValue | ||
} | ||
return "" | ||
} |
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
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
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
Oops, something went wrong.