-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update mysqld_exporter to v0.16.0 (#1643)
* Update mysqld_exporter to v0.16.0 From grafana/mysqld_exporter branch `refactor_collector`, including changes in prometheus/mysqld_exporter#774. * update up to 2ef168bf6
- Loading branch information
1 parent
5d47b91
commit 83bbc81
Showing
6 changed files
with
228 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package logging | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
) | ||
|
||
var _ slog.Handler = (*SlogGoKitHandler)(nil) | ||
|
||
// SlogGoKitHandler is an slog.Handler that wraps a go-kit logger. | ||
// This is specific to Alloy's logging system, as we expect the go-kit | ||
// logger to be configured with the correct level. | ||
type SlogGoKitHandler struct { | ||
logger log.Logger | ||
group string | ||
preformatted []any | ||
} | ||
|
||
func NewSlogGoKitHandler(logger log.Logger) *SlogGoKitHandler { | ||
return &SlogGoKitHandler{ | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (h SlogGoKitHandler) Enabled(ctx context.Context, level slog.Level) bool { | ||
// return always true, we expect the underlying logger to handle the level | ||
return true | ||
} | ||
|
||
func (h SlogGoKitHandler) Handle(ctx context.Context, record slog.Record) error { | ||
var logger log.Logger | ||
switch record.Level { | ||
case slog.LevelInfo: | ||
logger = level.Info(h.logger) | ||
case slog.LevelWarn: | ||
logger = level.Warn(h.logger) | ||
case slog.LevelError: | ||
logger = level.Error(h.logger) | ||
default: | ||
logger = level.Debug(h.logger) | ||
} | ||
|
||
pairs := make([]any, 0, 2*record.NumAttrs()) | ||
pairs = append(pairs, "msg", record.Message) | ||
pairs = append(pairs, h.preformatted...) | ||
|
||
record.Attrs(func(attr slog.Attr) bool { | ||
pairs = appendPair(pairs, h.group, attr) | ||
return true | ||
}) | ||
|
||
return logger.Log(pairs...) | ||
} | ||
|
||
func (h SlogGoKitHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
pairs := make([]any, 0, 2*len(attrs)) | ||
for _, attr := range attrs { | ||
pairs = appendPair(pairs, h.group, attr) | ||
} | ||
|
||
if h.preformatted != nil { | ||
pairs = append(h.preformatted, pairs...) | ||
} | ||
|
||
return &SlogGoKitHandler{ | ||
logger: h.logger, | ||
preformatted: pairs, | ||
group: h.group, | ||
} | ||
} | ||
|
||
func (h SlogGoKitHandler) WithGroup(name string) slog.Handler { | ||
if name == "" { | ||
return h | ||
} | ||
|
||
group := name | ||
if h.group != "" { | ||
group = h.group + "." + group | ||
} | ||
|
||
return &SlogGoKitHandler{ | ||
logger: h.logger, | ||
preformatted: h.preformatted, | ||
group: group, | ||
} | ||
} | ||
|
||
func appendPair(pairs []any, groupPrefix string, attr slog.Attr) []any { | ||
if attr.Equal(slog.Attr{}) { | ||
return pairs | ||
} | ||
|
||
switch attr.Value.Kind() { | ||
case slog.KindGroup: | ||
if attr.Key != "" { | ||
groupPrefix = groupPrefix + "." + attr.Key | ||
} | ||
for _, at := range attr.Value.Group() { | ||
pairs = appendPair(pairs, groupPrefix, at) | ||
} | ||
default: | ||
key := attr.Key | ||
if groupPrefix != "" { | ||
key = groupPrefix + "." + key | ||
} | ||
|
||
pairs = append(pairs, key, attr.Value) | ||
} | ||
|
||
return pairs | ||
} |
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,75 @@ | ||
package logging_test | ||
|
||
import ( | ||
"bytes" | ||
"log/slog" | ||
"strings" | ||
"testing" | ||
"testing/slogtest" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-logfmt/logfmt" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/grafana/alloy/internal/runtime/logging" | ||
) | ||
|
||
func TestWithSlogTester(t *testing.T) { | ||
buffer := bytes.NewBuffer(nil) | ||
handler := logging.NewSlogGoKitHandler(log.NewLogfmtLogger(buffer)) | ||
|
||
err := slogtest.TestHandler(handler, func() []map[string]any { | ||
results := []map[string]any{} | ||
|
||
dec := logfmt.NewDecoder(buffer) | ||
for dec.ScanRecord() { | ||
res := map[string]any{} | ||
for dec.ScanKeyval() { | ||
res[string(dec.Key())] = dec.Value() | ||
} | ||
results = append(results, res) | ||
} | ||
|
||
require.NoError(t, dec.Err()) | ||
return results | ||
}) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestUpdateLevel(t *testing.T) { | ||
buffer := bytes.NewBuffer(nil) | ||
baseLogger, err := logging.New(buffer, logging.Options{Level: logging.LevelInfo, Format: logging.FormatLogfmt}) | ||
require.NoError(t, err) | ||
|
||
gkLogger := log.With(baseLogger, "test", "test") | ||
gkLogger.Log("msg", "hello") | ||
require.Contains(t, buffer.String(), "ts=") | ||
noTimestamp := strings.Join(strings.Split(buffer.String(), " ")[1:], " ") | ||
require.Equal(t, "level=info msg=hello test=test\n", noTimestamp) | ||
|
||
sLogger := slog.New(logging.NewSlogGoKitHandler(gkLogger)) | ||
buffer.Reset() | ||
sLogger.Info("hello") | ||
require.Contains(t, buffer.String(), "ts=") | ||
noTimestamp = strings.Join(strings.Split(buffer.String(), " ")[1:], " ") | ||
require.Equal(t, "level=info msg=hello test=test\n", noTimestamp) | ||
|
||
buffer.Reset() | ||
sLogger.Debug("hello") | ||
require.Equal(t, "", buffer.String()) | ||
|
||
err = baseLogger.Update(logging.Options{Level: logging.LevelDebug, Format: logging.FormatLogfmt}) | ||
require.NoError(t, err) | ||
|
||
buffer.Reset() | ||
sLogger.Info("hello") | ||
require.Contains(t, buffer.String(), "ts=") | ||
noTimestamp = strings.Join(strings.Split(buffer.String(), " ")[1:], " ") | ||
require.Equal(t, "level=info msg=hello test=test\n", noTimestamp) | ||
|
||
buffer.Reset() | ||
sLogger.Debug("hello") | ||
require.Contains(t, buffer.String(), "ts=") | ||
noTimestamp = strings.Join(strings.Split(buffer.String(), " ")[1:], " ") | ||
require.Equal(t, "level=debug msg=hello test=test\n", noTimestamp) | ||
} |
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