Skip to content

Commit

Permalink
Merge pull request #38 from zapier/clean-up
Browse files Browse the repository at this point in the history
some refactor and cleanup
  • Loading branch information
djeebus authored Dec 12, 2022
2 parents 0652947 + 6b643ed commit 45afc22
Show file tree
Hide file tree
Showing 18 changed files with 272 additions and 174 deletions.
1 change: 1 addition & 0 deletions .earthlyignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/dist
/Earthfile
40 changes: 34 additions & 6 deletions aggregate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"fmt"
"io"
"log"
"net/http"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -129,27 +131,53 @@ func (a *aggregate) parseAndMerge(r io.Reader, job string) error {
return nil
}

func (a *aggregate) handler(c *gin.Context) {
func (a *aggregate) handleRender(c *gin.Context) {
contentType := expfmt.Negotiate(c.Request.Header)
c.Header("Content-Type", string(contentType))
enc := expfmt.NewEncoder(c.Writer, contentType)

a.familiesLock.RLock()
defer a.familiesLock.RUnlock()

metricNames := []string{}
for name := range a.families {
metricNames = append(metricNames, name)
}
a.familiesLock.RUnlock()
sort.Strings(metricNames)

for _, name := range metricNames {
a.families[name].lock.RLock()
defer a.families[name].lock.RUnlock()
if err := enc.Encode(a.families[name].MetricFamily); err != nil {
log.Printf("An error has occurred during metrics encoding:\n\n%s\n", err.Error())
if a.encodeMetric(name, enc) {
return
}
}

// TODO reset gauges
}

func (a *aggregate) encodeMetric(name string, enc expfmt.Encoder) bool {
a.families[name].lock.RLock()
defer a.families[name].lock.RUnlock()

if err := enc.Encode(a.families[name].MetricFamily); err != nil {
log.Printf("An error has occurred during metrics encoding:\n\n%s\n", err.Error())
return true
}
return false
}

func (a *aggregate) handleInsert(c *gin.Context) {
job := c.Param("job")
// TODO: add logic to verify correct format of job label
if job == "" {
err := fmt.Errorf("must send in a valid job name, sent: %s", job)
log.Println(err)
http.Error(c.Writer, err.Error(), http.StatusBadRequest)
return
}

if err := a.parseAndMerge(c.Request.Body, job); err != nil {
log.Println(err)
http.Error(c.Writer, err.Error(), http.StatusBadRequest)
return
}
}
79 changes: 36 additions & 43 deletions aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/pmezard/go-difflib/difflib"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -155,62 +156,54 @@ counter{a="a",b="b",job="test"} 3
`
)

func TestNewAggregate(t *testing.T) {
_ = newAggregate()

}

func TestAggregate(t *testing.T) {
metricMiddleware := newMetricMiddleware(nil)
for _, c := range []struct {
testName string
a, b string
want string
ignoredLabels []string
err1 error
err2 error
}{
{"simpleGauge", gaugeInput, gaugeInput, gaugeOutput, []string{}, nil, nil},
{"in", in1, in2, want, []string{}, nil, nil},
{"multilabel", multilabel1, multilabel2, multilabelResult, []string{"ignore_label"}, nil, nil},
{"labelFields", labelFields1, labelFields2, labelFieldResult, []string{}, nil, nil},
{"duplicateLabels", duplicateLabels, "", "", []string{}, fmt.Errorf("%s", duplicateError), nil},
{"reorderedLabels", reorderedLabels1, reorderedLabels2, reorderedLabelsResult, []string{}, nil, nil},
{"ignoredLabels", ignoredLabels1, ignoredLabels2, ignoredLabelsResult, []string{"ignore_me"}, nil, nil},
{"simpleGauge", gaugeInput, gaugeInput, gaugeOutput, []string{}},
{"in", in1, in2, want, []string{}},
{"multilabel", multilabel1, multilabel2, multilabelResult, []string{"ignore_label"}},
{"labelFields", labelFields1, labelFields2, labelFieldResult, []string{}},
{"reorderedLabels", reorderedLabels1, reorderedLabels2, reorderedLabelsResult, []string{}},
{"ignoredLabels", ignoredLabels1, ignoredLabels2, ignoredLabelsResult, []string{"ignore_me"}},
} {
rc := &RouterConfig{
MetricsMiddleware: &metricMiddleware,
Aggregate: newAggregate(AddIgnoredLabels(c.ignoredLabels...)),
}
router := setupRouter(rc)
t.Run(c.testName, func(t *testing.T) {
agg := newAggregate(AddIgnoredLabels(c.ignoredLabels...))
router := setupAPIRouter("*", agg)

if err := rc.Aggregate.parseAndMerge(strings.NewReader(c.a), "test"); err != nil {
if c.err1 == nil {
t.Fatalf("Unexpected error: %s", err)
} else if c.err1.Error() != err.Error() {
t.Fatalf("Expected %s, got %s", c.err1, err)
}
}
if err := rc.Aggregate.parseAndMerge(strings.NewReader(c.b), "test"); err != c.err2 {
t.Fatalf("Expected %s, got %s", c.err2, err)
}
err := agg.parseAndMerge(strings.NewReader(c.a), "test")
require.NoError(t, err)

err = agg.parseAndMerge(strings.NewReader(c.b), "test")
require.NoError(t, err)

w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/metrics", nil)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/metrics", nil)

router.ServeHTTP(w, r)
router.ServeHTTP(w, r)

if have := w.Body.String(); have != c.want {
text, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(c.want),
B: difflib.SplitLines(have),
FromFile: "have",
ToFile: "want",
Context: 3,
})
t.Fatalf("%s: %s", c.testName, text)
}
if have := w.Body.String(); have != c.want {
text, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(c.want),
B: difflib.SplitLines(have),
FromFile: "have",
ToFile: "want",
Context: 3,
})
t.Fatalf("%s: %s", c.testName, text)
}
})
}

t.Run("duplicateLabels", func(t *testing.T) {
agg := newAggregate()

err := agg.parseAndMerge(strings.NewReader(duplicateLabels), "test")
require.Equal(t, err.Error(), duplicateError)
})
}

var testMetricTable = []struct {
Expand Down
2 changes: 1 addition & 1 deletion charts/prom-aggregation-gateway/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apiVersion: v2
name: prom-aggregation-gateway
version: 0.4.3
version: 0.5.0
home: https://github.com/zapier/prom-aggregation-gateway
maintainers:
- name: djeebus
Expand Down
25 changes: 17 additions & 8 deletions charts/prom-aggregation-gateway/templates/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,33 @@ spec:
containers:
- name: prom-aggregation-gateway
image: {{ .Values.controller.image.repository }}:{{ .Values.controller.image.tag }}
{{- with .Values.controller.env }}
env:
{{- range $key, $value := . }}
{{- if not (kindIs "invalid" $value) }}
- name: "{{ $key }}"
value: "{{ $value }}"
{{- end }}
{{- end }}
{{- end }}
args:
- --listen
- :8080
- :{{ .Values.controller.apiPort }}
- --metricsListen
- :8888
- :{{ .Values.controller.lifecyclePort }}
ports:
- name: http
containerPort: 8080
- name: metrics
containerPort: 8888
- name: apiPort
containerPort: {{ .Values.controller.apiPort }}
- name: lifecycle
containerPort: {{ .Values.controller.lifecyclePort }}
livenessProbe:
httpGet:
path: /healthy
port: http
port: lifecycle
readinessProbe:
httpGet:
path: /ready
port: http
port: lifecycle
{{- with .Values.controller.resources }}
resources: {{ . | toYaml | nindent 12 }}
{{- end }}
2 changes: 1 addition & 1 deletion charts/prom-aggregation-gateway/templates/podmonitor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ spec:
matchLabels:
{{- include "prom-aggregation-gateway.selectorLabels" . | nindent 6 }}
podMetricsEndpoints:
- port: metrics
- port: lifecycle
{{- end }}
4 changes: 2 additions & 2 deletions charts/prom-aggregation-gateway/templates/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ spec:
selector:
{{- include "prom-aggregation-gateway.selectorLabels" . | nindent 4 }}
ports:
- name: http
- name: apiPort
port: {{ .Values.service.port }}
targetPort: http
targetPort: apiPort
{{- if eq .Values.service.type "NodePort" }}
nodePort: {{ .Values.service.nodePort }}
{{- else if ne (default .Values.controller.nodePort 12345) 12345 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ spec:
matchLabels:
{{- include "prom-aggregation-gateway.selectorLabels" . | nindent 6 }}
endpoints:
- port: http
- port: apiPort
{{- end }}
19 changes: 19 additions & 0 deletions charts/prom-aggregation-gateway/tests/controller_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,22 @@ tests:
cpu: 1
limits:
cpu: 2
- it: can render env vars
set:
controller:
env:
hello: world
asserts:
- equal:
path: spec.template.spec.containers[0].env
value:
- {name: GIN_MODE, value: release}
- {name: hello, value: world}
- it: can remove default env var
set:
controller:
env:
GIN_MODE:
asserts:
- isNull:
path: spec.template.spec.containers[0].env
18 changes: 17 additions & 1 deletion charts/prom-aggregation-gateway/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"DaemonSet"
]
},
"env": {
"type": "object",
"patternProperties": {
".*": {"type": ["string", "null"]}
}
},
"replicaCount": {
"type": "integer"
},
Expand All @@ -30,8 +36,18 @@
},
"resources": {
"type": "object"
},
"lifecyclePort": {
"type": "integer"
},
"apiPort": {
"type": "integer"
}
}
},
"required": [
"lifecyclePort",
"apiPort"
]
},
"nameOverride": {
"type": "string"
Expand Down
6 changes: 6 additions & 0 deletions charts/prom-aggregation-gateway/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ controller:
repository: ghcr.io/zapier/prom-aggregation-gateway
tag: latest

apiPort: 8080
lifecyclePort: 8888

replicaCount: 1

type: Deployment

env:
GIN_MODE: release

podMonitor:
create: true

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module prom-aggregation-gateway
go 1.19

require (
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.8.1
github.com/pmezard/go-difflib v1.0.0
github.com/prometheus/client_golang v1.12.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 45afc22

Please sign in to comment.