-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #14 Inited Glide logging based on Zap * #14 introducing a new telemetry struct * #14: Introduced a new telemetry struct to hold all observability components * #14: Added the router struct * #14: Configured hertz logger to obey the general logging config * #14 Added more static checks
- Loading branch information
1 parent
99ab0dc
commit 23693c7
Showing
11 changed files
with
228 additions
and
18 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 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package pools | ||
|
||
import "glide/pkg/telemetry" | ||
|
||
type Router struct { | ||
telemetry *telemetry.Telemetry | ||
} | ||
|
||
func NewRouter(tel *telemetry.Telemetry) (*Router, error) { | ||
return &Router{ | ||
telemetry: tel, | ||
}, nil | ||
} |
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,106 @@ | ||
package telemetry | ||
|
||
import ( | ||
"github.com/cloudwego/hertz/pkg/common/hlog" | ||
hertzzap "github.com/hertz-contrib/logger/zap" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
type LogConfig struct { | ||
// Level is the minimum enabled logging level. | ||
Level zap.AtomicLevel `yaml:"level"` | ||
|
||
// Encoding sets the logger's encoding. Valid values are "json", "console" | ||
Encoding string `yaml:"encoding"` | ||
|
||
// DisableCaller stops annotating logs with the calling function's file name and line number. | ||
// By default, all logs are annotated. | ||
DisableCaller bool `yaml:"disable_caller"` | ||
|
||
// DisableStacktrace completely disables automatic stacktrace capturing. By | ||
// default, stacktraces are captured for WarnLevel and above logs in | ||
// development and ErrorLevel and above in production. | ||
DisableStacktrace bool `yaml:"disable_stacktrace"` | ||
|
||
// OutputPaths is a list of URLs or file paths to write logging output to. | ||
OutputPaths []string `yaml:"output_paths"` | ||
|
||
// InitialFields is a collection of fields to add to the root logger. | ||
InitialFields map[string]interface{} `yaml:"initial_fields"` | ||
} | ||
|
||
func NewLogConfig() *LogConfig { | ||
return &LogConfig{ | ||
Level: zap.NewAtomicLevelAt(zap.InfoLevel), | ||
Encoding: "json", | ||
DisableCaller: false, | ||
DisableStacktrace: false, | ||
OutputPaths: []string{"stdout"}, | ||
InitialFields: make(map[string]interface{}), | ||
} | ||
} | ||
|
||
func (c *LogConfig) ToZapConfig() *zap.Config { | ||
zapConfig := zap.NewProductionConfig() | ||
|
||
if c.Encoding == "console" { | ||
zapConfig = zap.NewDevelopmentConfig() | ||
|
||
// Human-readable timestamps for console format of logs. | ||
zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | ||
// Colorized plain console logs | ||
zapConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder | ||
} | ||
|
||
zapConfig.Level = c.Level | ||
zapConfig.DisableCaller = c.DisableCaller | ||
zapConfig.DisableStacktrace = c.DisableStacktrace | ||
zapConfig.OutputPaths = c.OutputPaths | ||
zapConfig.InitialFields = c.InitialFields | ||
|
||
return &zapConfig | ||
} | ||
|
||
func NewHertzLogger(zapConfig *zap.Config) (*hertzzap.Logger, error) { | ||
// Both hertzzap and zap have a set of private methods that prevents from leveraging | ||
// their native encoder & sink building functionality | ||
// We had to copy & paste some of those to get it working | ||
var encoder zapcore.Encoder | ||
|
||
if zapConfig.Encoding == "console" { | ||
encoder = zapcore.NewConsoleEncoder(zapConfig.EncoderConfig) | ||
} else { | ||
encoder = zapcore.NewJSONEncoder(zapConfig.EncoderConfig) | ||
} | ||
|
||
sink, _, err := zap.Open(zapConfig.OutputPaths...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return hertzzap.NewLogger( | ||
hertzzap.WithCoreEnc(encoder), | ||
hertzzap.WithCoreWs(sink), | ||
hertzzap.WithCoreLevel(zapConfig.Level), | ||
hertzzap.WithZapOptions(zap.AddCallerSkip(3)), | ||
), nil | ||
} | ||
|
||
func NewLogger(cfg *LogConfig) (*zap.Logger, error) { | ||
zapConfig := cfg.ToZapConfig() | ||
|
||
logger, err := zapConfig.Build() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
hertzLogger, err := NewHertzLogger(zapConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
hlog.SetLogger(hertzLogger) | ||
|
||
return logger, nil | ||
} |
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,26 @@ | ||
package telemetry | ||
|
||
import "go.uber.org/zap" | ||
|
||
type Config struct { | ||
LogConfig *LogConfig `json:"logs" yaml:"logs"` | ||
// TODO: add OTEL config | ||
} | ||
|
||
type Telemetry struct { | ||
Config *Config | ||
Logger *zap.Logger | ||
// TODO: add OTEL meter, tracer | ||
} | ||
|
||
func NewTelemetry(cfg *Config) (*Telemetry, error) { | ||
logger, err := NewLogger(cfg.LogConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Telemetry{ | ||
Config: cfg, | ||
Logger: logger, | ||
}, nil | ||
} |