-
Notifications
You must be signed in to change notification settings - Fork 1.3k
build proxy with conditional logging and jsonselect encoders #4864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f254afc
build(components/proxy): with conditional logging and jsonselect enco…
leodido 1be5f35
update(components/proxy/conf): log in stackdriver format IFF status i…
leodido c56f37b
update(components/proxy/conf): stackdriver format for access logs
leodido ad7f5a6
chore: attempt to trigger a leeway rebuild
leodido 4d817d5
update(components/proxy/conf): do not log websocket upgrade requests …
leodido e7092fb
update(components/proxy/conf): log IFF status is not 0, 200, and 304
leodido 3e2a7c1
update(components/proxy/conf): emit request path in access logs
leodido 5229f8f
chore: force proxy container image rebuild
leodido 531ebf0
update(components/proxy): invalidate container image cache if plugins…
leodido 4946447
new(components/proxy/plugins): jsonselect log encoder plugin
leodido a8729fc
new(components/proxy/plugins): logif log encoder plugin
leodido 3640877
update(components/proxy/plugins): move existing plugins to their own …
leodido c0b41f5
update(components/proxy): update caddy plugins in the dockerfile
leodido ecbfbd4
doc(components/proxy/plugins): licenses
leodido 47e8912
deps(components/proxy/plugins/logif): go modules for logif log encoder
leodido a70461f
chore: fix todo
leodido File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
File renamed without changes.
This file contains hidden or 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,8 @@ | ||
module github.com/gitpod-io/gitpod/proxy/plugins/corsorigin | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/caddyserver/caddy/v2 v2.4.3 | ||
github.com/rs/cors v1.8.0 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,53 @@ | ||
// Copyright (c) 2021 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
package jsonselect | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" | ||
) | ||
|
||
func (e *JSONSelectEncoder) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { | ||
for d.Next() { | ||
args := d.RemainingArgs() | ||
switch len(args) { | ||
case 0: | ||
return d.Errf("%s (%T) requires an argument", moduleID, e) | ||
default: | ||
e.Selector = strings.Join(args, " ") | ||
} | ||
|
||
for n := d.Nesting(); d.NextBlock(n); { | ||
subdir := d.Val() | ||
var arg string | ||
if !d.AllArgs(&arg) { | ||
return d.ArgErr() | ||
} | ||
switch subdir { | ||
case "message_key": | ||
e.MessageKey = &arg | ||
case "level_key": | ||
e.LevelKey = &arg | ||
case "time_key": | ||
e.TimeKey = &arg | ||
case "name_key": | ||
e.NameKey = &arg | ||
case "caller_key": | ||
e.CallerKey = &arg | ||
case "stacktrace_key": | ||
e.StacktraceKey = &arg | ||
case "line_ending": | ||
e.LineEnding = &arg | ||
case "time_format": | ||
e.TimeFormat = arg | ||
case "level_format": | ||
e.LevelFormat = arg | ||
default: | ||
return d.Errf("unrecognized subdirective %s", subdir) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains hidden or 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,9 @@ | ||
module github.com/gitpod-io/gitpod/proxy/plugins/jsonselect | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/buger/jsonparser v1.1.1 | ||
github.com/caddyserver/caddy/v2 v2.4.3 | ||
go.uber.org/zap v1.18.1 | ||
) |
616 changes: 304 additions & 312 deletions
616
components/proxy/plugins/go.sum → components/proxy/plugins/jsonselect/go.sum
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,137 @@ | ||
// Copyright (c) 2021 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package jsonselect | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/buger/jsonparser" | ||
"github.com/caddyserver/caddy/v2" | ||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" | ||
"github.com/caddyserver/caddy/v2/modules/logging" | ||
"go.uber.org/zap/buffer" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
const ( | ||
moduleName = "jsonselect" | ||
moduleID = "caddy.logging.encoders." + moduleName | ||
) | ||
|
||
func init() { | ||
caddy.RegisterModule(JSONSelectEncoder{}) | ||
} | ||
|
||
type JSONSelectEncoder struct { | ||
logging.LogEncoderConfig | ||
zapcore.Encoder `json:"-"` | ||
Selector string `json:"selector,omitempty"` | ||
|
||
getters [][]string | ||
setters [][]string | ||
} | ||
|
||
func (JSONSelectEncoder) CaddyModule() caddy.ModuleInfo { | ||
return caddy.ModuleInfo{ | ||
ID: moduleID, | ||
New: func() caddy.Module { | ||
return &JSONSelectEncoder{ | ||
Encoder: new(logging.JSONEncoder), | ||
} | ||
}, | ||
} | ||
} | ||
|
||
func (e *JSONSelectEncoder) Provision(ctx caddy.Context) error { | ||
if e.Selector == "" { | ||
return fmt.Errorf("selector is mandatory") | ||
} | ||
|
||
e.setters = [][]string{} | ||
e.getters = [][]string{} | ||
r := caddy.NewReplacer() | ||
r.Map(func(sel string) (interface{}, bool) { | ||
var set, get string | ||
|
||
parts := strings.Split(sel, ":") | ||
if len(parts) == 1 { | ||
set = parts[0] | ||
get = set | ||
} else if len(parts) == 2 { | ||
set = parts[0] | ||
get = parts[1] | ||
} else { | ||
// todo > error out - how? | ||
return nil, false | ||
} | ||
|
||
e.setters = append(e.setters, strings.Split(set, ">")) | ||
e.getters = append(e.getters, strings.Split(get, ">")) | ||
return nil, false | ||
}) | ||
r.ReplaceAll(e.Selector, "") | ||
|
||
if len(e.setters) != len(e.getters) { | ||
return fmt.Errorf("selector must have the same number of setters and getters") | ||
} | ||
|
||
e.Encoder = zapcore.NewJSONEncoder(e.ZapcoreEncoderConfig()) | ||
return nil | ||
} | ||
|
||
func (e JSONSelectEncoder) Clone() zapcore.Encoder { | ||
return JSONSelectEncoder{ | ||
LogEncoderConfig: e.LogEncoderConfig, | ||
Encoder: e.Encoder.Clone(), | ||
Selector: e.Selector, | ||
getters: e.getters, | ||
setters: e.setters, | ||
} | ||
} | ||
|
||
func (e JSONSelectEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) { | ||
buf, err := e.Encoder.EncodeEntry(entry, fields) | ||
if err != nil { | ||
return buf, err | ||
} | ||
|
||
res := []byte{'{', '}'} | ||
// Temporary workaround the bug https://github.com/buger/jsonparser/issues/232 | ||
// TODO(leo): switch back to EachKey (see git history) for perf reasons when fixed | ||
for idx, paths := range e.getters { | ||
val, typ, _, err := jsonparser.Get(buf.Bytes(), paths...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
switch typ { | ||
case jsonparser.NotExist: | ||
// path not found, skip | ||
case jsonparser.String: | ||
res, _ = jsonparser.Set(res, append(append([]byte{'"'}, val...), '"'), e.setters[idx]...) | ||
default: | ||
res, _ = jsonparser.Set(res, val, e.setters[idx]...) | ||
} | ||
} | ||
|
||
// Reset the buffer to output our own content | ||
buf.Reset() | ||
// Insert the new content | ||
nl := []byte("\n") | ||
if !bytes.HasSuffix(res, nl) { | ||
res = append(res, nl...) | ||
} | ||
buf.Write(res) | ||
|
||
return buf, err | ||
} | ||
|
||
// Interface guards | ||
var ( | ||
_ zapcore.Encoder = (*JSONSelectEncoder)(nil) | ||
_ caddy.Provisioner = (*JSONSelectEncoder)(nil) | ||
_ caddyfile.Unmarshaler = (*JSONSelectEncoder)(nil) | ||
) |
This file contains hidden or 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,61 @@ | ||
// Copyright (c) 2021 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package logif | ||
|
||
import ( | ||
"github.com/caddyserver/caddy/v2/caddyconfig" | ||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// UnmarshalCaddyfile sets up the module form Caddyfile tokens. | ||
// | ||
// Syntax: | ||
// if { | ||
// "<expression>" | ||
// } [<encoder>] | ||
// | ||
// The <expression> must be on a single line. | ||
// Refer to `lang.Lang` for its syntax. | ||
// | ||
// The <encoder> can be one of `json`, `jsonselector`, `console`. | ||
// In case no <encoder> is specified, one between `json` and `console` is set up depending | ||
// on the current environment. | ||
func (ce *ConditionalEncoder) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { | ||
if d.Next() { | ||
if d.Val() != moduleName { | ||
return d.Errf("expecting %s (%T) subdirective", moduleID, ce) | ||
} | ||
var expression string | ||
if !d.Args(&expression) { | ||
return d.Errf("%s (%T) requires an expression", moduleID, ce) | ||
} | ||
|
||
ce.Expr = expression | ||
} | ||
|
||
if !d.Next() { | ||
return nil | ||
} | ||
|
||
// Delegate the parsing of the encoder to the encoder itself | ||
nextDispenser := d.NewFromNextSegment() | ||
if nextDispenser.Next() { | ||
moduleName := nextDispenser.Val() | ||
moduleID := "caddy.logging.encoders." + moduleName | ||
mod, err := caddyfile.UnmarshalModule(nextDispenser, moduleID) | ||
if err != nil { | ||
return err | ||
} | ||
enc, ok := mod.(zapcore.Encoder) | ||
if !ok { | ||
return d.Errf("module %s (%T) is not a zapcore.Encoder", moduleID, mod) | ||
} | ||
ce.EncRaw = caddyconfig.JSONModuleObject(enc, "format", moduleName, nil) | ||
ce.Formatter = moduleName | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or 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,14 @@ | ||
module github.com/gitpod-io/gitpod/proxy/plugins/logif | ||
|
||
go 1.16 | ||
|
||
replace github.com/gitpod-io/gitpod/proxy/plugins/jsonselect => ../jsonselect | ||
|
||
require ( | ||
github.com/PaesslerAG/gval v1.1.1 | ||
github.com/buger/jsonparser v1.1.1 | ||
github.com/caddyserver/caddy/v2 v2.4.3 | ||
github.com/gitpod-io/gitpod/proxy/plugins/jsonselect v0.0.0-00010101000000-000000000000 | ||
go.uber.org/zap v1.18.1 | ||
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.