Skip to content

Commit 1fb3ba2

Browse files
Add Zap as logger (#17)
* Add Zap as logger Use Zap lib for logging Clean up useless loop in GetConstraints Remove client logging * Speed up linter * Bump version
1 parent e750741 commit 1fb3ba2

File tree

15 files changed

+265
-110
lines changed

15 files changed

+265
-110
lines changed

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
.github
2+
.gitignore
3+
.golangci.yaml
24
.task
35
charts
46
test
57
tmp
8+
CONTRIBUTING.md
9+
LICENSE
10+
README.md

Dockerfile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# syntax = docker/dockerfile:1-experimental
2+
13
FROM golang:1.18.3 AS build
24

35
ENV TASK_VER 3.13.0
@@ -11,11 +13,16 @@ RUN echo "Fetching dev dependencies" && \
1113

1214
COPY . .
1315

14-
RUN echo "Building" && \
16+
RUN \
17+
--mount=type=cache,target=/root/.cache/go-build \
18+
echo "Building" && \
1519
task build-bin && \
1620
echo
1721

18-
RUN echo "Linting" && \
22+
RUN \
23+
--mount=type=cache,target=/root/.cache/go-build \
24+
--mount=type=cache,target=/root/.cache/golangci-lint \
25+
echo "Linting" && \
1926
task lint && \
2027
echo
2128

Taskfile.yaml

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,38 @@ tasks:
5757
CGO_ENABLED: "0"
5858
GOARCH: "{{ ARCH }}"
5959
sources:
60-
- main.go
61-
- pkg
60+
- cmd/**/*
61+
- internal/**/*
62+
- pkg/**/*
6263
- go.mod
6364
- go.sum
6465
generates:
6566
- "{{ .BIN_NAME }}"
6667

68+
# TODO: Unit tests
6769
# test:
6870
# desc: Run tests.
6971
# cmds:
70-
# # TODO: Unit tests
71-
# # - go test ./... -v -short {{ .CLI_ARGS }}
72+
# - go test ./... -v -short {{ .CLI_ARGS }}
7273
# sources:
73-
# - main.go
74-
# - pkg
74+
# - cmd/**/*
75+
# - internal/**/*
76+
# - pkg/**/*
7577
# - go.mod
7678
# - go.sum
7779

7880
lint:
7981
desc: Run the linter.
8082
cmds:
81-
- golangci-lint run ./... --timeout {{ .TIMEOUT }} {{ .CLI_ARGS }}
83+
- GOGC=100 golangci-lint run ./... --timeout {{ .TIMEOUT }} {{ .CLI_ARGS }}
8284
vars:
8385
TIMEOUT: 1m
8486
sources:
85-
- main.go
86-
- pkg
87-
- .golangci.yaml
87+
- cmd/**/*
88+
- internal/**/*
89+
- pkg/**/*
90+
- go.mod
91+
- go.sum
8892

8993
tidy:
9094
desc: Tidy up go deps.
@@ -99,14 +103,7 @@ tasks:
99103
build:
100104
desc: Build the docker image and generate a unique tag to force redeployment.
101105
cmds:
102-
- docker build --tag {{ .IMAGE_NAME }}:{{ .IMAGE_TAG }} --progress plain .
103-
sources:
104-
- Dockerfile
105-
- cmd/**/*
106-
- internal/**/*
107-
- pkg/**/*
108-
- go.mod
109-
- go.sum
106+
- DOCKER_BUILDKIT=1 docker build --tag {{ .IMAGE_NAME }}:{{ .IMAGE_TAG }} .
110107

111108
template:
112109
desc: Provide the opa-exporter template and invoke it with a variable kubectl command.
@@ -119,6 +116,8 @@ tasks:
119116
--values charts/opa-exporter/values.yaml \
120117
--set image.tag={{ .IMAGE_TAG }} \
121118
--set exporter.interval=10s \
119+
--set exporter.logLevel=debug \
120+
--set exporter.logMode=development \
122121
charts/opa-exporter \
123122
> {{ .TMP_DIR }}/opa-exporter.yaml
124123
- kubectl --context {{ .CONTEXT }} {{ .ACTION }} --wait --filename {{ .TMP_DIR }}/opa-exporter.yaml

charts/opa-exporter/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ apiVersion: v2
22
name: opa-exporter
33
description: Helm chart for opa-exporter, a prometheus exporter for OPA Gatekeeper.
44
type: application
5-
version: 0.1.2
6-
appVersion: 0.2.0
5+
version: 0.1.3
6+
appVersion: 0.3.0
77
home: https://github.com/csullivanupgrade/opa-exporter

charts/opa-exporter/templates/configmap.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ data:
88
configuration-file: |
99
inCluster: {{ .Values.exporter.inCluster }}
1010
interval: {{ .Values.exporter.interval }}
11+
logMode: {{ .Values.exporter.logMode }}
12+
logLevel: {{ .Values.exporter.logLevel }}
1113
namespace: {{ .Values.exporter.namespace }}
1214
path: {{ .Values.exporter.path }}
1315
port: {{ .Values.exporter.port }}

charts/opa-exporter/values.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ exporter:
66
interval: "60s"
77
# -- namespace the exporter is running in (does not affect operations - used for metrics FQDN)
88
namespace: default
9+
# -- log level to use, uses Zap levels
10+
logLevel: info
11+
# -- log mode to use:
12+
# ---- `development` adds extra stacktrace/codepath fields
13+
# ---- `nop` disables the logger
14+
logMode: production
915
# -- path for metrics server to listen
1016
path: "metrics"
1117
# -- port for the server to listen

cmd/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56

67
"github.com/csullivanupgrade/opa-exporter/internal/config"
8+
"github.com/csullivanupgrade/opa-exporter/internal/log"
79
"github.com/csullivanupgrade/opa-exporter/internal/server"
810
)
911

1012
func main() {
1113
configFile := flag.String("config", "", "The path for the config file")
1214
flag.Parse()
1315
cfg := config.New(*configFile)
14-
server.Run(*cfg)
16+
17+
logger := log.NewLogger(cfg.LogLevel, cfg.LogMode)
18+
19+
ctx, cancel := context.WithCancel(log.SetContext(context.Background(), logger))
20+
21+
server.Run(ctx, *cfg)
22+
defer cancel()
1523
}

config.yaml

Whitespace-only changes.

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ require (
5050
github.com/spf13/pflag v1.0.5 // indirect
5151
github.com/spf13/viper v1.12.0 // indirect
5252
github.com/subosito/gotenv v1.3.0 // indirect
53+
go.uber.org/atomic v1.7.0 // indirect
54+
go.uber.org/multierr v1.6.0 // indirect
55+
go.uber.org/zap v1.21.0 // indirect
5356
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
5457
golang.org/x/oauth2 v0.0.0-20220622183110-fd043fe589d2 // indirect
5558
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
549549
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
550550
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
551551
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
552+
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
552553
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
553554
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
554555
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
@@ -558,6 +559,8 @@ go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
558559
go.uber.org/zap v1.19.0/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=
559560
go.uber.org/zap v1.19.1 h1:ue41HOKd1vGURxrmeKIgELGb3jPW9DMUDGtsinblHwI=
560561
go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI=
562+
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
563+
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
561564
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
562565
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
563566
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

0 commit comments

Comments
 (0)