From db55d00f223160d8b9d9a565a7afb211bbd1a7ad Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Thu, 15 Aug 2024 02:31:32 +0200 Subject: [PATCH 1/4] fix: Matrics API Scaler now can read any prometheus metric Signed-off-by: Jorge Turrado --- go.mod | 5 + go.sum | 17 + pkg/scalers/metrics_api_scaler.go | 79 +- pkg/scalers/metrics_api_scaler_test.go | 16 + vendor/github.com/dennwc/varint/.gitignore | 2 + vendor/github.com/dennwc/varint/.travis.yml | 7 + vendor/github.com/dennwc/varint/LICENSE | 21 + vendor/github.com/dennwc/varint/README.md | 47 + vendor/github.com/dennwc/varint/proto.go | 244 + vendor/github.com/dennwc/varint/varint.go | 270 ++ vendor/github.com/go-kit/log/.gitignore | 15 + vendor/github.com/go-kit/log/LICENSE | 21 + vendor/github.com/go-kit/log/README.md | 156 + vendor/github.com/go-kit/log/doc.go | 116 + vendor/github.com/go-kit/log/json_logger.go | 91 + vendor/github.com/go-kit/log/level/doc.go | 33 + vendor/github.com/go-kit/log/level/level.go | 256 + vendor/github.com/go-kit/log/log.go | 179 + vendor/github.com/go-kit/log/logfmt_logger.go | 62 + vendor/github.com/go-kit/log/nop_logger.go | 8 + vendor/github.com/go-kit/log/staticcheck.conf | 1 + vendor/github.com/go-kit/log/stdlib.go | 151 + vendor/github.com/go-kit/log/sync.go | 113 + vendor/github.com/go-kit/log/value.go | 110 + vendor/github.com/go-logfmt/logfmt/.gitignore | 1 + .../github.com/go-logfmt/logfmt/CHANGELOG.md | 82 + vendor/github.com/go-logfmt/logfmt/LICENSE | 22 + vendor/github.com/go-logfmt/logfmt/README.md | 41 + vendor/github.com/go-logfmt/logfmt/decode.go | 254 + vendor/github.com/go-logfmt/logfmt/doc.go | 6 + vendor/github.com/go-logfmt/logfmt/encode.go | 322 ++ .../github.com/go-logfmt/logfmt/jsonstring.go | 277 ++ vendor/github.com/gogo/protobuf/types/any.go | 140 + .../github.com/gogo/protobuf/types/any.pb.go | 694 +++ .../github.com/gogo/protobuf/types/api.pb.go | 2134 +++++++++ vendor/github.com/gogo/protobuf/types/doc.go | 35 + .../gogo/protobuf/types/duration.go | 100 + .../gogo/protobuf/types/duration.pb.go | 517 ++ .../gogo/protobuf/types/duration_gogo.go | 100 + .../gogo/protobuf/types/empty.pb.go | 462 ++ .../gogo/protobuf/types/field_mask.pb.go | 738 +++ .../gogo/protobuf/types/protosize.go | 34 + .../gogo/protobuf/types/source_context.pb.go | 524 ++ .../gogo/protobuf/types/struct.pb.go | 2271 +++++++++ .../gogo/protobuf/types/timestamp.go | 130 + .../gogo/protobuf/types/timestamp.pb.go | 539 +++ .../gogo/protobuf/types/timestamp_gogo.go | 94 + .../github.com/gogo/protobuf/types/type.pb.go | 3355 +++++++++++++ .../gogo/protobuf/types/wrappers.pb.go | 2703 +++++++++++ .../gogo/protobuf/types/wrappers_gogo.go | 300 ++ vendor/github.com/grafana/regexp/.gitignore | 15 + vendor/github.com/grafana/regexp/LICENSE | 27 + vendor/github.com/grafana/regexp/README.md | 12 + vendor/github.com/grafana/regexp/backtrack.go | 367 ++ vendor/github.com/grafana/regexp/exec.go | 554 +++ vendor/github.com/grafana/regexp/onepass.go | 507 ++ vendor/github.com/grafana/regexp/regexp.go | 1285 +++++ .../grafana/regexp/syntax/compile.go | 296 ++ .../github.com/grafana/regexp/syntax/doc.go | 141 + .../grafana/regexp/syntax/make_perl_groups.pl | 113 + .../grafana/regexp/syntax/op_string.go | 26 + .../github.com/grafana/regexp/syntax/parse.go | 2114 ++++++++ .../grafana/regexp/syntax/perl_groups.go | 134 + .../github.com/grafana/regexp/syntax/prog.go | 347 ++ .../grafana/regexp/syntax/regexp.go | 320 ++ .../grafana/regexp/syntax/simplify.go | 151 + .../github.com/prometheus/prometheus/LICENSE | 201 + .../github.com/prometheus/prometheus/NOTICE | 108 + .../prometheus/model/exemplar/exemplar.go | 65 + .../model/histogram/float_histogram.go | 1175 +++++ .../prometheus/model/histogram/generic.go | 705 +++ .../prometheus/model/histogram/histogram.go | 512 ++ .../prometheus/model/histogram/test_utils.go | 52 + .../prometheus/model/labels/labels.go | 434 ++ .../prometheus/model/labels/labels_common.go | 235 + .../model/labels/labels_stringlabels.go | 645 +++ .../prometheus/model/labels/matcher.go | 120 + .../prometheus/model/labels/regexp.go | 125 + .../prometheus/model/labels/test_utils.go | 87 + .../prometheus/model/metadata/metadata.go | 23 + .../prometheus/model/textparse/README.md | 6 + .../prometheus/model/textparse/interface.go | 127 + .../model/textparse/openmetricslex.l | 80 + .../model/textparse/openmetricslex.l.go | 792 +++ .../model/textparse/openmetricsparse.go | 478 ++ .../prometheus/model/textparse/promlex.l | 103 + .../prometheus/model/textparse/promlex.l.go | 578 +++ .../prometheus/model/textparse/promparse.go | 441 ++ .../model/textparse/promtestdata.nometa.txt | 411 ++ .../model/textparse/promtestdata.txt | 529 ++ .../model/textparse/protobufparse.go | 611 +++ .../prometheus/model/timestamp/timestamp.go | 34 + .../prometheus/model/value/value.go | 34 + .../prompb/io/prometheus/client/metrics.pb.go | 4234 +++++++++++++++++ .../prompb/io/prometheus/client/metrics.proto | 157 + .../prometheus/promql/parser/ast.go | 492 ++ .../prometheus/promql/parser/functions.go | 427 ++ .../promql/parser/generated_parser.y | 919 ++++ .../promql/parser/generated_parser.y.go | 1976 ++++++++ .../prometheus/promql/parser/lex.go | 996 ++++ .../prometheus/promql/parser/parse.go | 984 ++++ .../promql/parser/posrange/posrange.go | 54 + .../prometheus/promql/parser/prettier.go | 166 + .../promql/parser/prettier_rules.md | 16 + .../prometheus/promql/parser/printer.go | 235 + .../prometheus/promql/parser/value.go | 45 + .../prometheus/prometheus/storage/buffer.go | 835 ++++ .../prometheus/prometheus/storage/fanout.go | 233 + .../prometheus/prometheus/storage/generic.go | 143 + .../prometheus/storage/interface.go | 432 ++ .../prometheus/prometheus/storage/lazy.go | 71 + .../prometheus/storage/memoized_iterator.go | 142 + .../prometheus/prometheus/storage/merge.go | 876 ++++ .../prometheus/prometheus/storage/noop.go | 97 + .../prometheus/storage/secondary.go | 109 + .../prometheus/prometheus/storage/series.go | 457 ++ .../prometheus/tsdb/chunkenc/bstream.go | 259 + .../prometheus/tsdb/chunkenc/chunk.go | 382 ++ .../tsdb/chunkenc/float_histogram.go | 940 ++++ .../prometheus/tsdb/chunkenc/histogram.go | 1058 ++++ .../tsdb/chunkenc/histogram_meta.go | 501 ++ .../prometheus/tsdb/chunkenc/varbit.go | 231 + .../prometheus/tsdb/chunkenc/xor.go | 507 ++ .../tsdb/chunks/chunk_write_queue.go | 264 + .../prometheus/tsdb/chunks/chunks.go | 754 +++ .../prometheus/tsdb/chunks/head_chunks.go | 1124 +++++ .../tsdb/chunks/head_chunks_other.go | 22 + .../tsdb/chunks/head_chunks_windows.go | 18 + .../prometheus/tsdb/chunks/queue.go | 141 + .../prometheus/tsdb/chunks/samples.go | 89 + .../prometheus/tsdb/errors/errors.go | 104 + .../prometheus/tsdb/fileutil/dir.go | 33 + .../prometheus/tsdb/fileutil/dir_unix.go | 23 + .../prometheus/tsdb/fileutil/dir_windows.go | 47 + .../prometheus/tsdb/fileutil/fileutil.go | 128 + .../prometheus/tsdb/fileutil/flock.go | 41 + .../prometheus/tsdb/fileutil/flock_js.go | 33 + .../prometheus/tsdb/fileutil/flock_plan9.go | 32 + .../prometheus/tsdb/fileutil/flock_solaris.go | 60 + .../prometheus/tsdb/fileutil/flock_unix.go | 55 + .../prometheus/tsdb/fileutil/flock_windows.go | 36 + .../prometheus/tsdb/fileutil/mmap.go | 72 + .../prometheus/tsdb/fileutil/mmap_386.go | 19 + .../prometheus/tsdb/fileutil/mmap_amd64.go | 19 + .../prometheus/tsdb/fileutil/mmap_arm64.go | 19 + .../prometheus/tsdb/fileutil/mmap_js.go | 30 + .../prometheus/tsdb/fileutil/mmap_unix.go | 31 + .../prometheus/tsdb/fileutil/mmap_windows.go | 46 + .../prometheus/tsdb/fileutil/preallocate.go | 54 + .../tsdb/fileutil/preallocate_darwin.go | 41 + .../tsdb/fileutil/preallocate_linux.go | 48 + .../tsdb/fileutil/preallocate_other.go | 26 + .../prometheus/tsdb/fileutil/sync.go | 25 + .../prometheus/tsdb/fileutil/sync_darwin.go | 28 + .../prometheus/tsdb/fileutil/sync_linux.go | 30 + .../util/annotations/annotations.go | 185 + .../prometheus/util/strutil/quote.go | 255 + .../prometheus/util/strutil/strconv.go | 64 + vendor/modules.txt | 34 + 159 files changed, 55002 insertions(+), 17 deletions(-) create mode 100644 vendor/github.com/dennwc/varint/.gitignore create mode 100644 vendor/github.com/dennwc/varint/.travis.yml create mode 100644 vendor/github.com/dennwc/varint/LICENSE create mode 100644 vendor/github.com/dennwc/varint/README.md create mode 100644 vendor/github.com/dennwc/varint/proto.go create mode 100644 vendor/github.com/dennwc/varint/varint.go create mode 100644 vendor/github.com/go-kit/log/.gitignore create mode 100644 vendor/github.com/go-kit/log/LICENSE create mode 100644 vendor/github.com/go-kit/log/README.md create mode 100644 vendor/github.com/go-kit/log/doc.go create mode 100644 vendor/github.com/go-kit/log/json_logger.go create mode 100644 vendor/github.com/go-kit/log/level/doc.go create mode 100644 vendor/github.com/go-kit/log/level/level.go create mode 100644 vendor/github.com/go-kit/log/log.go create mode 100644 vendor/github.com/go-kit/log/logfmt_logger.go create mode 100644 vendor/github.com/go-kit/log/nop_logger.go create mode 100644 vendor/github.com/go-kit/log/staticcheck.conf create mode 100644 vendor/github.com/go-kit/log/stdlib.go create mode 100644 vendor/github.com/go-kit/log/sync.go create mode 100644 vendor/github.com/go-kit/log/value.go create mode 100644 vendor/github.com/go-logfmt/logfmt/.gitignore create mode 100644 vendor/github.com/go-logfmt/logfmt/CHANGELOG.md create mode 100644 vendor/github.com/go-logfmt/logfmt/LICENSE create mode 100644 vendor/github.com/go-logfmt/logfmt/README.md create mode 100644 vendor/github.com/go-logfmt/logfmt/decode.go create mode 100644 vendor/github.com/go-logfmt/logfmt/doc.go create mode 100644 vendor/github.com/go-logfmt/logfmt/encode.go create mode 100644 vendor/github.com/go-logfmt/logfmt/jsonstring.go create mode 100644 vendor/github.com/gogo/protobuf/types/any.go create mode 100644 vendor/github.com/gogo/protobuf/types/any.pb.go create mode 100644 vendor/github.com/gogo/protobuf/types/api.pb.go create mode 100644 vendor/github.com/gogo/protobuf/types/doc.go create mode 100644 vendor/github.com/gogo/protobuf/types/duration.go create mode 100644 vendor/github.com/gogo/protobuf/types/duration.pb.go create mode 100644 vendor/github.com/gogo/protobuf/types/duration_gogo.go create mode 100644 vendor/github.com/gogo/protobuf/types/empty.pb.go create mode 100644 vendor/github.com/gogo/protobuf/types/field_mask.pb.go create mode 100644 vendor/github.com/gogo/protobuf/types/protosize.go create mode 100644 vendor/github.com/gogo/protobuf/types/source_context.pb.go create mode 100644 vendor/github.com/gogo/protobuf/types/struct.pb.go create mode 100644 vendor/github.com/gogo/protobuf/types/timestamp.go create mode 100644 vendor/github.com/gogo/protobuf/types/timestamp.pb.go create mode 100644 vendor/github.com/gogo/protobuf/types/timestamp_gogo.go create mode 100644 vendor/github.com/gogo/protobuf/types/type.pb.go create mode 100644 vendor/github.com/gogo/protobuf/types/wrappers.pb.go create mode 100644 vendor/github.com/gogo/protobuf/types/wrappers_gogo.go create mode 100644 vendor/github.com/grafana/regexp/.gitignore create mode 100644 vendor/github.com/grafana/regexp/LICENSE create mode 100644 vendor/github.com/grafana/regexp/README.md create mode 100644 vendor/github.com/grafana/regexp/backtrack.go create mode 100644 vendor/github.com/grafana/regexp/exec.go create mode 100644 vendor/github.com/grafana/regexp/onepass.go create mode 100644 vendor/github.com/grafana/regexp/regexp.go create mode 100644 vendor/github.com/grafana/regexp/syntax/compile.go create mode 100644 vendor/github.com/grafana/regexp/syntax/doc.go create mode 100644 vendor/github.com/grafana/regexp/syntax/make_perl_groups.pl create mode 100644 vendor/github.com/grafana/regexp/syntax/op_string.go create mode 100644 vendor/github.com/grafana/regexp/syntax/parse.go create mode 100644 vendor/github.com/grafana/regexp/syntax/perl_groups.go create mode 100644 vendor/github.com/grafana/regexp/syntax/prog.go create mode 100644 vendor/github.com/grafana/regexp/syntax/regexp.go create mode 100644 vendor/github.com/grafana/regexp/syntax/simplify.go create mode 100644 vendor/github.com/prometheus/prometheus/LICENSE create mode 100644 vendor/github.com/prometheus/prometheus/NOTICE create mode 100644 vendor/github.com/prometheus/prometheus/model/exemplar/exemplar.go create mode 100644 vendor/github.com/prometheus/prometheus/model/histogram/float_histogram.go create mode 100644 vendor/github.com/prometheus/prometheus/model/histogram/generic.go create mode 100644 vendor/github.com/prometheus/prometheus/model/histogram/histogram.go create mode 100644 vendor/github.com/prometheus/prometheus/model/histogram/test_utils.go create mode 100644 vendor/github.com/prometheus/prometheus/model/labels/labels.go create mode 100644 vendor/github.com/prometheus/prometheus/model/labels/labels_common.go create mode 100644 vendor/github.com/prometheus/prometheus/model/labels/labels_stringlabels.go create mode 100644 vendor/github.com/prometheus/prometheus/model/labels/matcher.go create mode 100644 vendor/github.com/prometheus/prometheus/model/labels/regexp.go create mode 100644 vendor/github.com/prometheus/prometheus/model/labels/test_utils.go create mode 100644 vendor/github.com/prometheus/prometheus/model/metadata/metadata.go create mode 100644 vendor/github.com/prometheus/prometheus/model/textparse/README.md create mode 100644 vendor/github.com/prometheus/prometheus/model/textparse/interface.go create mode 100644 vendor/github.com/prometheus/prometheus/model/textparse/openmetricslex.l create mode 100644 vendor/github.com/prometheus/prometheus/model/textparse/openmetricslex.l.go create mode 100644 vendor/github.com/prometheus/prometheus/model/textparse/openmetricsparse.go create mode 100644 vendor/github.com/prometheus/prometheus/model/textparse/promlex.l create mode 100644 vendor/github.com/prometheus/prometheus/model/textparse/promlex.l.go create mode 100644 vendor/github.com/prometheus/prometheus/model/textparse/promparse.go create mode 100644 vendor/github.com/prometheus/prometheus/model/textparse/promtestdata.nometa.txt create mode 100644 vendor/github.com/prometheus/prometheus/model/textparse/promtestdata.txt create mode 100644 vendor/github.com/prometheus/prometheus/model/textparse/protobufparse.go create mode 100644 vendor/github.com/prometheus/prometheus/model/timestamp/timestamp.go create mode 100644 vendor/github.com/prometheus/prometheus/model/value/value.go create mode 100644 vendor/github.com/prometheus/prometheus/prompb/io/prometheus/client/metrics.pb.go create mode 100644 vendor/github.com/prometheus/prometheus/prompb/io/prometheus/client/metrics.proto create mode 100644 vendor/github.com/prometheus/prometheus/promql/parser/ast.go create mode 100644 vendor/github.com/prometheus/prometheus/promql/parser/functions.go create mode 100644 vendor/github.com/prometheus/prometheus/promql/parser/generated_parser.y create mode 100644 vendor/github.com/prometheus/prometheus/promql/parser/generated_parser.y.go create mode 100644 vendor/github.com/prometheus/prometheus/promql/parser/lex.go create mode 100644 vendor/github.com/prometheus/prometheus/promql/parser/parse.go create mode 100644 vendor/github.com/prometheus/prometheus/promql/parser/posrange/posrange.go create mode 100644 vendor/github.com/prometheus/prometheus/promql/parser/prettier.go create mode 100644 vendor/github.com/prometheus/prometheus/promql/parser/prettier_rules.md create mode 100644 vendor/github.com/prometheus/prometheus/promql/parser/printer.go create mode 100644 vendor/github.com/prometheus/prometheus/promql/parser/value.go create mode 100644 vendor/github.com/prometheus/prometheus/storage/buffer.go create mode 100644 vendor/github.com/prometheus/prometheus/storage/fanout.go create mode 100644 vendor/github.com/prometheus/prometheus/storage/generic.go create mode 100644 vendor/github.com/prometheus/prometheus/storage/interface.go create mode 100644 vendor/github.com/prometheus/prometheus/storage/lazy.go create mode 100644 vendor/github.com/prometheus/prometheus/storage/memoized_iterator.go create mode 100644 vendor/github.com/prometheus/prometheus/storage/merge.go create mode 100644 vendor/github.com/prometheus/prometheus/storage/noop.go create mode 100644 vendor/github.com/prometheus/prometheus/storage/secondary.go create mode 100644 vendor/github.com/prometheus/prometheus/storage/series.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/chunkenc/bstream.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/chunkenc/chunk.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/chunkenc/float_histogram.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/chunkenc/histogram.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/chunkenc/histogram_meta.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/chunkenc/varbit.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/chunkenc/xor.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/chunks/chunk_write_queue.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/chunks/chunks.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/chunks/head_chunks.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/chunks/head_chunks_other.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/chunks/head_chunks_windows.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/chunks/queue.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/chunks/samples.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/errors/errors.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/dir.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/dir_unix.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/dir_windows.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/fileutil.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/flock.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/flock_js.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/flock_plan9.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/flock_solaris.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/flock_unix.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/flock_windows.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/mmap.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/mmap_386.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/mmap_amd64.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/mmap_arm64.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/mmap_js.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/mmap_unix.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/mmap_windows.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/preallocate.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/preallocate_darwin.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/preallocate_linux.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/preallocate_other.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/sync.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/sync_darwin.go create mode 100644 vendor/github.com/prometheus/prometheus/tsdb/fileutil/sync_linux.go create mode 100644 vendor/github.com/prometheus/prometheus/util/annotations/annotations.go create mode 100644 vendor/github.com/prometheus/prometheus/util/strutil/quote.go create mode 100644 vendor/github.com/prometheus/prometheus/util/strutil/strconv.go diff --git a/go.mod b/go.mod index b6eff2b66cc..4322a6cafee 100644 --- a/go.mod +++ b/go.mod @@ -210,6 +210,7 @@ require ( github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/dennwc/varint v1.0.0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dimchansky/utfbom v1.1.1 // indirect github.com/eapache/go-resiliency v1.6.0 // indirect @@ -224,6 +225,8 @@ require ( github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/go-errors/errors v1.5.1 // indirect github.com/go-jose/go-jose/v4 v4.0.1 // indirect + github.com/go-kit/log v0.2.1 // indirect + github.com/go-logfmt/logfmt v0.6.0 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-logr/zapr v1.3.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect @@ -254,6 +257,7 @@ require ( github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/googleapis/gax-go/v2 v2.13.0 // indirect github.com/gorilla/websocket v1.5.1 // indirect + github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect @@ -308,6 +312,7 @@ require ( github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/procfs v0.15.1 // indirect + github.com/prometheus/prometheus v0.49.0 github.com/rivo/uniseg v0.4.4 // indirect github.com/ryanuber/go-glob v1.0.0 // indirect github.com/samber/lo v1.39.0 // indirect diff --git a/go.sum b/go.sum index 8ecdbc874fd..e7cefad3489 100644 --- a/go.sum +++ b/go.sum @@ -866,6 +866,8 @@ github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3 github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b/go.mod h1:1KcenG0jGWcpt8ov532z81sp/kMMUG485J2InIOyADM= github.com/alecthomas/kingpin/v2 v2.3.2/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE= github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE= +github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 h1:ez/4by2iGztzR4L0zgAOR8lTQK9VlyBVVd7G4omaOQs= +github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE= github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= @@ -891,6 +893,8 @@ github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3d github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= github.com/aws/aws-msk-iam-sasl-signer-go v1.0.0 h1:UyjtGmO0Uwl/K+zpzPwLoXzMhcN9xmnR2nrqJoBrg3c= github.com/aws/aws-msk-iam-sasl-signer-go v1.0.0/go.mod h1:TJAXuFs2HcMib3sN5L0gUC+Q01Qvy3DemvA55WuC+iA= +github.com/aws/aws-sdk-go v1.48.14 h1:nVLrp+F84SG+xGiFMfe1TE6ZV6smF+42tuuNgYGV30s= +github.com/aws/aws-sdk-go v1.48.14/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= github.com/aws/aws-sdk-go-v2 v1.16.12/go.mod h1:C+Ym0ag2LIghJbXhfXZ0YEEp49rBWowxKzJLUoob0ts= github.com/aws/aws-sdk-go-v2 v1.30.3 h1:jUeBtG0Ih+ZIFH0F4UkmL9w3cSpaMv9tYYDbzILP8dY= github.com/aws/aws-sdk-go-v2 v1.30.3/go.mod h1:nIQjQVp5sfpQcTc9mPSr1B0PaWK5ByX9MOoDadSN4lc= @@ -1015,6 +1019,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1 github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denisenkom/go-mssqldb v0.12.3 h1:pBSGx9Tq67pBOTLmxNuirNTeB8Vjmf886Kx+8Y+8shw= github.com/denisenkom/go-mssqldb v0.12.3/go.mod h1:k0mtMFOnU+AihqFxPMiF05rtiDrorD1Vrm1KEz5hxDo= +github.com/dennwc/varint v1.0.0 h1:kGNFFSSw8ToIy3obO/kKr8U9GZYUAxQEVuix4zfDWzE= +github.com/dennwc/varint v1.0.0/go.mod h1:hnItb35rvZvJrbTALZtY/iQfDs48JKRG1RPpgziApxA= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/dimchansky/utfbom v1.1.1 h1:vV6w1AhK4VMnhBno/TPVCoK9U/LP0PkLCS9tbxHdi/U= @@ -1088,6 +1094,7 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2 github.com/go-jose/go-jose/v4 v4.0.1 h1:QVEPDE3OluqXBQZDcnNvQrInro2h0e4eqNbnZSWqS6U= github.com/go-jose/go-jose/v4 v4.0.1/go.mod h1:WVf9LFMHh/QVrmqrOfqun0C45tMe3RoiKJMPvgWwLfY= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= +github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU= github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0= github.com/go-kivik/couchdb/v3 v3.4.1 h1:TlGYEFOmG5a0pN6MpDkIDdd+sn75+w5aSDTcEou02kk= github.com/go-kivik/couchdb/v3 v3.4.1/go.mod h1:scodbTTSS6vOAacJXaCx6XZ57qw8YH1JOvhMwvP0vuw= @@ -1099,6 +1106,8 @@ github.com/go-latex/latex v0.0.0-20210118124228-b3d85cf34e07/go.mod h1:CO1AlKB2C github.com/go-latex/latex v0.0.0-20210823091927-c0d11ff05a81/go.mod h1:SX0U8uGpxhq9o2S/CELCSUxEWWAuoCUcVCQWv7G2OCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= +github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= +github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= @@ -1309,6 +1318,8 @@ github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/z github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= +github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd h1:PpuIBO5P3e9hpqBD0O/HjhShYuM6XE0i/lbE6J94kww= +github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd/go.mod h1:M5qHK+eWfAv8VR/265dIuEpL3fNfeC21tXXp9itM24A= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8= @@ -1539,6 +1550,8 @@ github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro= github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg= +github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4= +github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -1590,10 +1603,14 @@ github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cY github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM= github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY= +github.com/prometheus/common/sigv4 v0.1.0 h1:qoVebwtwwEhS85Czm2dSROY5fTo2PAPEVdDeppTwGX4= +github.com/prometheus/common/sigv4 v0.1.0/go.mod h1:2Jkxxk9yYvCkE5G1sQT7GuEXm57JrvHu9k5YwTjsNtI= github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY= github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= +github.com/prometheus/prometheus v0.49.0 h1:i0CEhreJo3ZcZNeK7ulISinCac0MgL0krVOGgNmfFRY= +github.com/prometheus/prometheus v0.49.0/go.mod h1:aDogiyqmv3aBIWDb5z5Sdcxuuf2BOfiJwOIm9JGpMnI= github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw= github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM= diff --git a/pkg/scalers/metrics_api_scaler.go b/pkg/scalers/metrics_api_scaler.go index b3a678b3cc5..e98867fd869 100644 --- a/pkg/scalers/metrics_api_scaler.go +++ b/pkg/scalers/metrics_api_scaler.go @@ -1,8 +1,6 @@ package scalers import ( - "bufio" - "bytes" "context" "encoding/xml" "errors" @@ -14,6 +12,8 @@ import ( "strings" "github.com/go-logr/logr" + "github.com/prometheus/common/expfmt" + "github.com/prometheus/prometheus/promql/parser" "github.com/tidwall/gjson" "gopkg.in/yaml.v3" v2 "k8s.io/api/autoscaling/v2" @@ -264,25 +264,70 @@ func GetValueFromResponse(body []byte, valueLocation string, format APIFormat) ( // getValueFromPrometheusResponse uses provided valueLocation to access the numeric value in provided body func getValueFromPrometheusResponse(body []byte, valueLocation string) (float64, error) { - scanner := bufio.NewScanner(bytes.NewReader(body)) - for scanner.Scan() { - line := scanner.Text() - fields := strings.Fields(line) - if len(fields) == 0 || strings.HasPrefix(fields[0], "#") { - continue - } - if len(fields) == 2 && strings.HasPrefix(fields[0], valueLocation) { - value, err := strconv.ParseFloat(fields[1], 64) - if err != nil { - return 0, err - } - return value, nil + familiesParser := expfmt.TextParser{} + metricss, _ := parser.ParseMetric(string(body)) + + matchers, err := parser.ParseMetricSelector(valueLocation) + if err != nil { + return 0, err + } + metricName := "" + for _, v := range matchers { + if v.Name == "__name__" { + metricName = v.Value } } - - if err := scanner.Err(); err != nil { + _ = metricss + // Ensure EOL + reader := strings.NewReader(strings.ReplaceAll(string(body), "\r\n", "\n")) + families, err := familiesParser.TextToMetricFamilies(reader) + if err != nil { return 0, err } + family, ok := families[metricName] + if !ok { + return 0, fmt.Errorf("metric %s not found", metricName) + } + + metrics := family.GetMetric() + for _, metric := range metrics { + labels := metric.GetLabel() + match := true + for _, matcher := range matchers { + matcherFound := false + if matcher == nil { + continue + } + // The name has been already validated, + // so we can skip it and check the other metrics + if matcher.Name == "__name__" { + continue + } + for _, label := range labels { + if *label.Name == matcher.Name && + *label.Value == matcher.Value { + matcherFound = true + } + } + if !matcherFound { + match = false + } + } + if match { + untyped := metric.GetUntyped() + if untyped != nil && untyped.Value != nil { + return *untyped.Value, nil + } + counter := metric.GetCounter() + if counter != nil && counter.Value != nil { + return *counter.Value, nil + } + gauge := metric.GetGauge() + if gauge != nil && gauge.Value != nil { + return *gauge.Value, nil + } + } + } return 0, fmt.Errorf("value %s not found", valueLocation) } diff --git a/pkg/scalers/metrics_api_scaler_test.go b/pkg/scalers/metrics_api_scaler_test.go index cef44a7bfef..a580d8e1af1 100644 --- a/pkg/scalers/metrics_api_scaler_test.go +++ b/pkg/scalers/metrics_api_scaler_test.go @@ -125,6 +125,16 @@ func TestMetricsAPIGetMetricSpecForScaling(t *testing.T) { func TestGetValueFromResponse(t *testing.T) { inputJSON := []byte(`{"components":[{"id": "82328e93e", "tasks": 32, "str": "64", "k":"1k","wrong":"NaN"}],"count":2.43}`) inputYAML := []byte(`{components: [{id: 82328e93e, tasks: 32, str: '64', k: 1k, wrong: NaN}], count: 2.43}`) + inputPrometheus := []byte(`# HELP backend_queue_size Total number of items + # TYPE backend_queue_size counter + backend_queue_size{queueName="zero"} 0 + backend_queue_size{queueName="one"} 1 + backend_queue_size{queueName="two", instance="random"} 2 + backend_queue_size{queueName="two", instance="zero"} 20 + # HELP random_metric Random metric generate to include noise + # TYPE random_metric counter + random_metric 10 + `) testCases := []struct { name string @@ -143,6 +153,12 @@ func TestGetValueFromResponse(t *testing.T) { {name: "string", input: inputYAML, key: "components.0.str", format: YAMLFormat, expectVal: 64}, {name: "{}.[].{}", input: inputYAML, key: "components.0.tasks", format: YAMLFormat, expectVal: 32}, {name: "invalid data", input: inputYAML, key: "components.0.wrong", format: YAMLFormat, expectErr: true}, + + {name: "no labels", input: inputPrometheus, key: "random_metric", format: PrometheusFormat, expectVal: 10}, + {name: "one label", input: inputPrometheus, key: "backend_queue_size{queueName=\"one\"}", format: PrometheusFormat, expectVal: 1}, + {name: "multiple labels not queried", input: inputPrometheus, key: "backend_queue_size{queueName=\"two\"}", format: PrometheusFormat, expectVal: 2}, + {name: "multiple labels queried", input: inputPrometheus, key: "backend_queue_size{queueName=\"two\", instance=\"zero\"}", format: PrometheusFormat, expectVal: 20}, + {name: "invalid data", input: inputPrometheus, key: "backend_queue_size{invalid=test}", format: PrometheusFormat, expectErr: true}, } for _, tc := range testCases { diff --git a/vendor/github.com/dennwc/varint/.gitignore b/vendor/github.com/dennwc/varint/.gitignore new file mode 100644 index 00000000000..9385b6db18b --- /dev/null +++ b/vendor/github.com/dennwc/varint/.gitignore @@ -0,0 +1,2 @@ +*.o +*.txt \ No newline at end of file diff --git a/vendor/github.com/dennwc/varint/.travis.yml b/vendor/github.com/dennwc/varint/.travis.yml new file mode 100644 index 00000000000..b3da258f520 --- /dev/null +++ b/vendor/github.com/dennwc/varint/.travis.yml @@ -0,0 +1,7 @@ +language: go + +go: + - 1.12.x + +env: + - GO111MODULE=on \ No newline at end of file diff --git a/vendor/github.com/dennwc/varint/LICENSE b/vendor/github.com/dennwc/varint/LICENSE new file mode 100644 index 00000000000..8b3f68715c1 --- /dev/null +++ b/vendor/github.com/dennwc/varint/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Denys Smirnov + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/dennwc/varint/README.md b/vendor/github.com/dennwc/varint/README.md new file mode 100644 index 00000000000..fe15b3b5003 --- /dev/null +++ b/vendor/github.com/dennwc/varint/README.md @@ -0,0 +1,47 @@ +# varint + +This package provides an optimized implementation of protobuf's varint encoding/decoding. +It has no dependencies. + +Benchmarks comparing to a `binary.Uvarint`: + +``` +benchmark old ns/op new ns/op delta +BenchmarkUvarint/1-8 4.13 2.85 -30.99% +BenchmarkUvarint/1_large-8 4.01 2.28 -43.14% +BenchmarkUvarint/2-8 6.23 2.87 -53.93% +BenchmarkUvarint/2_large-8 5.60 2.86 -48.93% +BenchmarkUvarint/3-8 6.55 3.44 -47.48% +BenchmarkUvarint/3_large-8 6.54 2.86 -56.27% +BenchmarkUvarint/4-8 7.30 3.71 -49.18% +BenchmarkUvarint/4_large-8 7.46 3.10 -58.45% +BenchmarkUvarint/5-8 8.31 4.12 -50.42% +BenchmarkUvarint/5_large-8 8.56 3.48 -59.35% +BenchmarkUvarint/6-8 9.42 4.66 -50.53% +BenchmarkUvarint/6_large-8 9.91 4.07 -58.93% +BenchmarkUvarint/7-8 10.6 5.28 -50.19% +BenchmarkUvarint/7_large-8 11.0 4.70 -57.27% +BenchmarkUvarint/8-8 11.7 6.02 -48.55% +BenchmarkUvarint/8_large-8 12.1 5.19 -57.11% +BenchmarkUvarint/9-8 12.9 6.83 -47.05% +BenchmarkUvarint/9_large-8 13.1 5.71 -56.41% +``` + +It also provides additional functionality like `UvarintSize` (similar to `sov*` in `gogo/protobuf`): + +``` +benchmark old ns/op new ns/op delta +BenchmarkUvarintSize/1-8 1.71 0.43 -74.85% +BenchmarkUvarintSize/2-8 2.56 0.57 -77.73% +BenchmarkUvarintSize/3-8 3.22 0.72 -77.64% +BenchmarkUvarintSize/4-8 3.74 0.72 -80.75% +BenchmarkUvarintSize/5-8 4.29 0.57 -86.71% +BenchmarkUvarintSize/6-8 4.85 0.58 -88.04% +BenchmarkUvarintSize/7-8 5.43 0.71 -86.92% +BenchmarkUvarintSize/8-8 6.01 0.86 -85.69% +BenchmarkUvarintSize/9-8 6.64 1.00 -84.94% +``` + +# License + +MIT \ No newline at end of file diff --git a/vendor/github.com/dennwc/varint/proto.go b/vendor/github.com/dennwc/varint/proto.go new file mode 100644 index 00000000000..e3b458547f9 --- /dev/null +++ b/vendor/github.com/dennwc/varint/proto.go @@ -0,0 +1,244 @@ +package varint + +// ProtoTag decodes a protobuf's field number and wire type pair +// from buf and returns that value and the number of bytes read (> 0). +// If an error occurred, n = 0 is returned. +func ProtoTag(buf []byte) (num int, typ byte, n int) { + // Same unrolled implementation as in Uvarint. + // + // But this time we can check if the wire type and field num + // are valid when reading the first byte. + // + // Also, the swifts are now different, because first 3 bits + // are for the wire type. + // + // The implementation will stop at 9 bytes, returning an error. + sz := len(buf) + if sz == 0 { + return 0, 0, 0 + } + const ( + bit = 1 << 7 + mask = bit - 1 + step = 7 + + // protobuf + typBits = 3 + typMask = 1<<3 - 1 + ) + if sz >= 9 { // no bound checks + // i == 0 + b := buf[0] + if b == 0 { + return 0, 0, 0 + } + typ = b & typMask + if typ > 5 { + return 0, 0, 0 + } + if b < bit { + num = int(b >> typBits) + if num == 0 { + return 0, 0, 0 + } + n = 1 + return + } + num = int((b & mask) >> typBits) + var s uint = step - typBits + + // i == 1 + b = buf[1] + if b < bit { + num |= int(b) << s + n = 2 + return + } + num |= int(b&mask) << s + s += step + + // i == 2 + b = buf[2] + if b < bit { + num |= int(b) << s + n = 3 + return + } + num |= int(b&mask) << s + s += step + + // i == 3 + b = buf[3] + if b < bit { + num |= int(b) << s + n = 4 + return + } + num |= int(b&mask) << s + s += step + + // i == 4 + b = buf[4] + if b < bit { + num |= int(b) << s + n = 5 + return + } + num |= int(b&mask) << s + s += step + + // i == 5 + b = buf[5] + if b < bit { + num |= int(b) << s + n = 6 + return + } + num |= int(b&mask) << s + s += step + + // i == 6 + b = buf[6] + if b < bit { + num |= int(b) << s + n = 7 + return + } + num |= int(b&mask) << s + s += step + + // i == 7 + b = buf[7] + if b < bit { + num |= int(b) << s + n = 8 + return + } + num |= int(b&mask) << s + s += step + + // i == 8 + b = buf[8] + if b < bit { + num |= int(b) << s + n = 9 + return + } + return 0, 0, 0 // too much + } + + // i == 0 + b := buf[0] + if b == 0 { + return 0, 0, 0 + } + typ = b & typMask + if typ > 5 { + return 0, 0, 0 + } + if b < bit { + num = int(b >> typBits) + if num == 0 { + return 0, 0, 0 + } + n = 1 + return + } else if sz == 1 { + return 0, 0, 0 + } + num = int((b & mask) >> typBits) + var s uint = step - typBits + + // i == 1 + b = buf[1] + if b < bit { + num |= int(b) << s + n = 2 + return + } else if sz == 2 { + return 0, 0, 0 + } + num |= int(b&mask) << s + s += step + + // i == 2 + b = buf[2] + if b < bit { + num |= int(b) << s + n = 3 + return + } else if sz == 3 { + return 0, 0, 0 + } + num |= int(b&mask) << s + s += step + + // i == 3 + b = buf[3] + if b < bit { + num |= int(b) << s + n = 4 + return + } else if sz == 4 { + return 0, 0, 0 + } + num |= int(b&mask) << s + s += step + + // i == 4 + b = buf[4] + if b < bit { + num |= int(b) << s + n = 5 + return + } else if sz == 5 { + return 0, 0, 0 + } + num |= int(b&mask) << s + s += step + + // i == 5 + b = buf[5] + if b < bit { + num |= int(b) << s + n = 6 + return + } else if sz == 6 { + return 0, 0, 0 + } + num |= int(b&mask) << s + s += step + + // i == 6 + b = buf[6] + if b < bit { + num |= int(b) << s + n = 7 + return + } else if sz == 7 { + return 0, 0, 0 + } + num |= int(b&mask) << s + s += step + + // i == 7 + b = buf[7] + if b < bit { + num |= int(b) << s + n = 8 + return + } else if sz == 8 { + return 0, 0, 0 + } + num |= int(b&mask) << s + s += step + + // i == 8 + b = buf[8] + if b < bit { + num |= int(b) << s + n = 9 + return + } + return 0, 0, 0 // too much +} diff --git a/vendor/github.com/dennwc/varint/varint.go b/vendor/github.com/dennwc/varint/varint.go new file mode 100644 index 00000000000..83278c2d7d2 --- /dev/null +++ b/vendor/github.com/dennwc/varint/varint.go @@ -0,0 +1,270 @@ +package varint + +const maxUint64 = uint64(1<<64 - 1) + +// MaxLenN is the maximum length of a varint-encoded N-bit integer. +const ( + MaxLen8 = 2 + MaxLen16 = 3 + MaxLen32 = 5 + MaxLen64 = 10 +) + +// MaxValN is the maximum varint-encoded integer that fits in N bytes. +const ( + MaxVal9 = maxUint64 >> (1 + iota*7) + MaxVal8 + MaxVal7 + MaxVal6 + MaxVal5 + MaxVal4 + MaxVal3 + MaxVal2 + MaxVal1 +) + +// UvarintSize returns the number of bytes necessary to encode a given uint. +func UvarintSize(x uint64) int { + if x <= MaxVal4 { + if x <= MaxVal1 { + return 1 + } else if x <= MaxVal2 { + return 2 + } else if x <= MaxVal3 { + return 3 + } + return 4 + } + if x <= MaxVal5 { + return 5 + } else if x <= MaxVal6 { + return 6 + } else if x <= MaxVal7 { + return 7 + } else if x <= MaxVal8 { + return 8 + } else if x <= MaxVal9 { + return 9 + } + return 10 +} + +// Uvarint decodes a uint64 from buf and returns that value and the +// number of bytes read (> 0). If an error occurred, the value is 0 +// and the number of bytes n is <= 0 meaning: +// +// n == 0: buf too small +// n < 0: value larger than 64 bits (overflow) +// and -n is the number of bytes read +// +func Uvarint(buf []byte) (uint64, int) { + // Fully unrolled implementation of binary.Uvarint. + // + // It will also eliminate bound checks for buffers larger than 9 bytes. + sz := len(buf) + if sz == 0 { + return 0, 0 + } + const ( + step = 7 + bit = 1 << 7 + mask = bit - 1 + ) + if sz >= 10 { // no bound checks + // i == 0 + b := buf[0] + if b < bit { + return uint64(b), 1 + } + x := uint64(b & mask) + var s uint = step + + // i == 1 + b = buf[1] + if b < bit { + return x | uint64(b)< 1 { + return 0, -10 // overflow + } + return x | uint64(b)< 1 { + return 0, -10 // overflow + } + return x | uint64(b)< 100 { +// level.Error(logger).Log("value", value) +// } +// +// NewFilter allows precise control over what happens when a log event is +// emitted without a level key, or if a squelched level is used. Check the +// Option functions for details. +package level diff --git a/vendor/github.com/go-kit/log/level/level.go b/vendor/github.com/go-kit/log/level/level.go new file mode 100644 index 00000000000..c641d985524 --- /dev/null +++ b/vendor/github.com/go-kit/log/level/level.go @@ -0,0 +1,256 @@ +package level + +import ( + "errors" + "strings" + + "github.com/go-kit/log" +) + +// ErrInvalidLevelString is returned whenever an invalid string is passed to Parse. +var ErrInvalidLevelString = errors.New("invalid level string") + +// Error returns a logger that includes a Key/ErrorValue pair. +func Error(logger log.Logger) log.Logger { + return log.WithPrefix(logger, Key(), ErrorValue()) +} + +// Warn returns a logger that includes a Key/WarnValue pair. +func Warn(logger log.Logger) log.Logger { + return log.WithPrefix(logger, Key(), WarnValue()) +} + +// Info returns a logger that includes a Key/InfoValue pair. +func Info(logger log.Logger) log.Logger { + return log.WithPrefix(logger, Key(), InfoValue()) +} + +// Debug returns a logger that includes a Key/DebugValue pair. +func Debug(logger log.Logger) log.Logger { + return log.WithPrefix(logger, Key(), DebugValue()) +} + +// NewFilter wraps next and implements level filtering. See the commentary on +// the Option functions for a detailed description of how to configure levels. +// If no options are provided, all leveled log events created with Debug, +// Info, Warn or Error helper methods are squelched and non-leveled log +// events are passed to next unmodified. +func NewFilter(next log.Logger, options ...Option) log.Logger { + l := &logger{ + next: next, + } + for _, option := range options { + option(l) + } + return l +} + +type logger struct { + next log.Logger + allowed level + squelchNoLevel bool + errNotAllowed error + errNoLevel error +} + +func (l *logger) Log(keyvals ...interface{}) error { + var hasLevel, levelAllowed bool + for i := 1; i < len(keyvals); i += 2 { + if v, ok := keyvals[i].(*levelValue); ok { + hasLevel = true + levelAllowed = l.allowed&v.level != 0 + break + } + } + if !hasLevel && l.squelchNoLevel { + return l.errNoLevel + } + if hasLevel && !levelAllowed { + return l.errNotAllowed + } + return l.next.Log(keyvals...) +} + +// Option sets a parameter for the leveled logger. +type Option func(*logger) + +// Allow the provided log level to pass. +func Allow(v Value) Option { + switch v { + case debugValue: + return AllowDebug() + case infoValue: + return AllowInfo() + case warnValue: + return AllowWarn() + case errorValue: + return AllowError() + default: + return AllowNone() + } +} + +// AllowAll is an alias for AllowDebug. +func AllowAll() Option { + return AllowDebug() +} + +// AllowDebug allows error, warn, info and debug level log events to pass. +func AllowDebug() Option { + return allowed(levelError | levelWarn | levelInfo | levelDebug) +} + +// AllowInfo allows error, warn and info level log events to pass. +func AllowInfo() Option { + return allowed(levelError | levelWarn | levelInfo) +} + +// AllowWarn allows error and warn level log events to pass. +func AllowWarn() Option { + return allowed(levelError | levelWarn) +} + +// AllowError allows only error level log events to pass. +func AllowError() Option { + return allowed(levelError) +} + +// AllowNone allows no leveled log events to pass. +func AllowNone() Option { + return allowed(0) +} + +func allowed(allowed level) Option { + return func(l *logger) { l.allowed = allowed } +} + +// Parse a string to its corresponding level value. Valid strings are "debug", +// "info", "warn", and "error". Strings are normalized via strings.TrimSpace and +// strings.ToLower. +func Parse(level string) (Value, error) { + switch strings.TrimSpace(strings.ToLower(level)) { + case debugValue.name: + return debugValue, nil + case infoValue.name: + return infoValue, nil + case warnValue.name: + return warnValue, nil + case errorValue.name: + return errorValue, nil + default: + return nil, ErrInvalidLevelString + } +} + +// ParseDefault calls Parse and returns the default Value on error. +func ParseDefault(level string, def Value) Value { + v, err := Parse(level) + if err != nil { + return def + } + return v +} + +// ErrNotAllowed sets the error to return from Log when it squelches a log +// event disallowed by the configured Allow[Level] option. By default, +// ErrNotAllowed is nil; in this case the log event is squelched with no +// error. +func ErrNotAllowed(err error) Option { + return func(l *logger) { l.errNotAllowed = err } +} + +// SquelchNoLevel instructs Log to squelch log events with no level, so that +// they don't proceed through to the wrapped logger. If SquelchNoLevel is set +// to true and a log event is squelched in this way, the error value +// configured with ErrNoLevel is returned to the caller. +func SquelchNoLevel(squelch bool) Option { + return func(l *logger) { l.squelchNoLevel = squelch } +} + +// ErrNoLevel sets the error to return from Log when it squelches a log event +// with no level. By default, ErrNoLevel is nil; in this case the log event is +// squelched with no error. +func ErrNoLevel(err error) Option { + return func(l *logger) { l.errNoLevel = err } +} + +// NewInjector wraps next and returns a logger that adds a Key/level pair to +// the beginning of log events that don't already contain a level. In effect, +// this gives a default level to logs without a level. +func NewInjector(next log.Logger, level Value) log.Logger { + return &injector{ + next: next, + level: level, + } +} + +type injector struct { + next log.Logger + level interface{} +} + +func (l *injector) Log(keyvals ...interface{}) error { + for i := 1; i < len(keyvals); i += 2 { + if _, ok := keyvals[i].(*levelValue); ok { + return l.next.Log(keyvals...) + } + } + kvs := make([]interface{}, len(keyvals)+2) + kvs[0], kvs[1] = key, l.level + copy(kvs[2:], keyvals) + return l.next.Log(kvs...) +} + +// Value is the interface that each of the canonical level values implement. +// It contains unexported methods that prevent types from other packages from +// implementing it and guaranteeing that NewFilter can distinguish the levels +// defined in this package from all other values. +type Value interface { + String() string + levelVal() +} + +// Key returns the unique key added to log events by the loggers in this +// package. +func Key() interface{} { return key } + +// ErrorValue returns the unique value added to log events by Error. +func ErrorValue() Value { return errorValue } + +// WarnValue returns the unique value added to log events by Warn. +func WarnValue() Value { return warnValue } + +// InfoValue returns the unique value added to log events by Info. +func InfoValue() Value { return infoValue } + +// DebugValue returns the unique value added to log events by Debug. +func DebugValue() Value { return debugValue } + +var ( + // key is of type interface{} so that it allocates once during package + // initialization and avoids allocating every time the value is added to a + // []interface{} later. + key interface{} = "level" + + errorValue = &levelValue{level: levelError, name: "error"} + warnValue = &levelValue{level: levelWarn, name: "warn"} + infoValue = &levelValue{level: levelInfo, name: "info"} + debugValue = &levelValue{level: levelDebug, name: "debug"} +) + +type level byte + +const ( + levelDebug level = 1 << iota + levelInfo + levelWarn + levelError +) + +type levelValue struct { + name string + level +} + +func (v *levelValue) String() string { return v.name } +func (v *levelValue) levelVal() {} diff --git a/vendor/github.com/go-kit/log/log.go b/vendor/github.com/go-kit/log/log.go new file mode 100644 index 00000000000..62e11adace5 --- /dev/null +++ b/vendor/github.com/go-kit/log/log.go @@ -0,0 +1,179 @@ +package log + +import "errors" + +// Logger is the fundamental interface for all log operations. Log creates a +// log event from keyvals, a variadic sequence of alternating keys and values. +// Implementations must be safe for concurrent use by multiple goroutines. In +// particular, any implementation of Logger that appends to keyvals or +// modifies or retains any of its elements must make a copy first. +type Logger interface { + Log(keyvals ...interface{}) error +} + +// ErrMissingValue is appended to keyvals slices with odd length to substitute +// the missing value. +var ErrMissingValue = errors.New("(MISSING)") + +// With returns a new contextual logger with keyvals prepended to those passed +// to calls to Log. If logger is also a contextual logger created by With, +// WithPrefix, or WithSuffix, keyvals is appended to the existing context. +// +// The returned Logger replaces all value elements (odd indexes) containing a +// Valuer with their generated value for each call to its Log method. +func With(logger Logger, keyvals ...interface{}) Logger { + if len(keyvals) == 0 { + return logger + } + l := newContext(logger) + kvs := append(l.keyvals, keyvals...) + if len(kvs)%2 != 0 { + kvs = append(kvs, ErrMissingValue) + } + return &context{ + logger: l.logger, + // Limiting the capacity of the stored keyvals ensures that a new + // backing array is created if the slice must grow in Log or With. + // Using the extra capacity without copying risks a data race that + // would violate the Logger interface contract. + keyvals: kvs[:len(kvs):len(kvs)], + hasValuer: l.hasValuer || containsValuer(keyvals), + sKeyvals: l.sKeyvals, + sHasValuer: l.sHasValuer, + } +} + +// WithPrefix returns a new contextual logger with keyvals prepended to those +// passed to calls to Log. If logger is also a contextual logger created by +// With, WithPrefix, or WithSuffix, keyvals is prepended to the existing context. +// +// The returned Logger replaces all value elements (odd indexes) containing a +// Valuer with their generated value for each call to its Log method. +func WithPrefix(logger Logger, keyvals ...interface{}) Logger { + if len(keyvals) == 0 { + return logger + } + l := newContext(logger) + // Limiting the capacity of the stored keyvals ensures that a new + // backing array is created if the slice must grow in Log or With. + // Using the extra capacity without copying risks a data race that + // would violate the Logger interface contract. + n := len(l.keyvals) + len(keyvals) + if len(keyvals)%2 != 0 { + n++ + } + kvs := make([]interface{}, 0, n) + kvs = append(kvs, keyvals...) + if len(kvs)%2 != 0 { + kvs = append(kvs, ErrMissingValue) + } + kvs = append(kvs, l.keyvals...) + return &context{ + logger: l.logger, + keyvals: kvs, + hasValuer: l.hasValuer || containsValuer(keyvals), + sKeyvals: l.sKeyvals, + sHasValuer: l.sHasValuer, + } +} + +// WithSuffix returns a new contextual logger with keyvals appended to those +// passed to calls to Log. If logger is also a contextual logger created by +// With, WithPrefix, or WithSuffix, keyvals is appended to the existing context. +// +// The returned Logger replaces all value elements (odd indexes) containing a +// Valuer with their generated value for each call to its Log method. +func WithSuffix(logger Logger, keyvals ...interface{}) Logger { + if len(keyvals) == 0 { + return logger + } + l := newContext(logger) + // Limiting the capacity of the stored keyvals ensures that a new + // backing array is created if the slice must grow in Log or With. + // Using the extra capacity without copying risks a data race that + // would violate the Logger interface contract. + n := len(l.sKeyvals) + len(keyvals) + if len(keyvals)%2 != 0 { + n++ + } + kvs := make([]interface{}, 0, n) + kvs = append(kvs, keyvals...) + if len(kvs)%2 != 0 { + kvs = append(kvs, ErrMissingValue) + } + kvs = append(l.sKeyvals, kvs...) + return &context{ + logger: l.logger, + keyvals: l.keyvals, + hasValuer: l.hasValuer, + sKeyvals: kvs, + sHasValuer: l.sHasValuer || containsValuer(keyvals), + } +} + +// context is the Logger implementation returned by With, WithPrefix, and +// WithSuffix. It wraps a Logger and holds keyvals that it includes in all +// log events. Its Log method calls bindValues to generate values for each +// Valuer in the context keyvals. +// +// A context must always have the same number of stack frames between calls to +// its Log method and the eventual binding of Valuers to their value. This +// requirement comes from the functional requirement to allow a context to +// resolve application call site information for a Caller stored in the +// context. To do this we must be able to predict the number of logging +// functions on the stack when bindValues is called. +// +// Two implementation details provide the needed stack depth consistency. +// +// 1. newContext avoids introducing an additional layer when asked to +// wrap another context. +// 2. With, WithPrefix, and WithSuffix avoid introducing an additional +// layer by returning a newly constructed context with a merged keyvals +// rather than simply wrapping the existing context. +type context struct { + logger Logger + keyvals []interface{} + sKeyvals []interface{} // suffixes + hasValuer bool + sHasValuer bool +} + +func newContext(logger Logger) *context { + if c, ok := logger.(*context); ok { + return c + } + return &context{logger: logger} +} + +// Log replaces all value elements (odd indexes) containing a Valuer in the +// stored context with their generated value, appends keyvals, and passes the +// result to the wrapped Logger. +func (l *context) Log(keyvals ...interface{}) error { + kvs := append(l.keyvals, keyvals...) + if len(kvs)%2 != 0 { + kvs = append(kvs, ErrMissingValue) + } + if l.hasValuer { + // If no keyvals were appended above then we must copy l.keyvals so + // that future log events will reevaluate the stored Valuers. + if len(keyvals) == 0 { + kvs = append([]interface{}{}, l.keyvals...) + } + bindValues(kvs[:(len(l.keyvals))]) + } + kvs = append(kvs, l.sKeyvals...) + if l.sHasValuer { + bindValues(kvs[len(kvs)-len(l.sKeyvals):]) + } + return l.logger.Log(kvs...) +} + +// LoggerFunc is an adapter to allow use of ordinary functions as Loggers. If +// f is a function with the appropriate signature, LoggerFunc(f) is a Logger +// object that calls f. +type LoggerFunc func(...interface{}) error + +// Log implements Logger by calling f(keyvals...). +func (f LoggerFunc) Log(keyvals ...interface{}) error { + return f(keyvals...) +} diff --git a/vendor/github.com/go-kit/log/logfmt_logger.go b/vendor/github.com/go-kit/log/logfmt_logger.go new file mode 100644 index 00000000000..a00305298b8 --- /dev/null +++ b/vendor/github.com/go-kit/log/logfmt_logger.go @@ -0,0 +1,62 @@ +package log + +import ( + "bytes" + "io" + "sync" + + "github.com/go-logfmt/logfmt" +) + +type logfmtEncoder struct { + *logfmt.Encoder + buf bytes.Buffer +} + +func (l *logfmtEncoder) Reset() { + l.Encoder.Reset() + l.buf.Reset() +} + +var logfmtEncoderPool = sync.Pool{ + New: func() interface{} { + var enc logfmtEncoder + enc.Encoder = logfmt.NewEncoder(&enc.buf) + return &enc + }, +} + +type logfmtLogger struct { + w io.Writer +} + +// NewLogfmtLogger returns a logger that encodes keyvals to the Writer in +// logfmt format. Each log event produces no more than one call to w.Write. +// The passed Writer must be safe for concurrent use by multiple goroutines if +// the returned Logger will be used concurrently. +func NewLogfmtLogger(w io.Writer) Logger { + return &logfmtLogger{w} +} + +func (l logfmtLogger) Log(keyvals ...interface{}) error { + enc := logfmtEncoderPool.Get().(*logfmtEncoder) + enc.Reset() + defer logfmtEncoderPool.Put(enc) + + if err := enc.EncodeKeyvals(keyvals...); err != nil { + return err + } + + // Add newline to the end of the buffer + if err := enc.EndRecord(); err != nil { + return err + } + + // The Logger interface requires implementations to be safe for concurrent + // use by multiple goroutines. For this implementation that means making + // only one call to l.w.Write() for each call to Log. + if _, err := l.w.Write(enc.buf.Bytes()); err != nil { + return err + } + return nil +} diff --git a/vendor/github.com/go-kit/log/nop_logger.go b/vendor/github.com/go-kit/log/nop_logger.go new file mode 100644 index 00000000000..1047d626c43 --- /dev/null +++ b/vendor/github.com/go-kit/log/nop_logger.go @@ -0,0 +1,8 @@ +package log + +type nopLogger struct{} + +// NewNopLogger returns a logger that doesn't do anything. +func NewNopLogger() Logger { return nopLogger{} } + +func (nopLogger) Log(...interface{}) error { return nil } diff --git a/vendor/github.com/go-kit/log/staticcheck.conf b/vendor/github.com/go-kit/log/staticcheck.conf new file mode 100644 index 00000000000..528438b97d2 --- /dev/null +++ b/vendor/github.com/go-kit/log/staticcheck.conf @@ -0,0 +1 @@ +checks = ["all"] diff --git a/vendor/github.com/go-kit/log/stdlib.go b/vendor/github.com/go-kit/log/stdlib.go new file mode 100644 index 00000000000..0338edbe2ba --- /dev/null +++ b/vendor/github.com/go-kit/log/stdlib.go @@ -0,0 +1,151 @@ +package log + +import ( + "bytes" + "io" + "log" + "regexp" + "strings" +) + +// StdlibWriter implements io.Writer by invoking the stdlib log.Print. It's +// designed to be passed to a Go kit logger as the writer, for cases where +// it's necessary to redirect all Go kit log output to the stdlib logger. +// +// If you have any choice in the matter, you shouldn't use this. Prefer to +// redirect the stdlib log to the Go kit logger via NewStdlibAdapter. +type StdlibWriter struct{} + +// Write implements io.Writer. +func (w StdlibWriter) Write(p []byte) (int, error) { + log.Print(strings.TrimSpace(string(p))) + return len(p), nil +} + +// StdlibAdapter wraps a Logger and allows it to be passed to the stdlib +// logger's SetOutput. It will extract date/timestamps, filenames, and +// messages, and place them under relevant keys. +type StdlibAdapter struct { + Logger + timestampKey string + fileKey string + messageKey string + prefix string + joinPrefixToMsg bool +} + +// StdlibAdapterOption sets a parameter for the StdlibAdapter. +type StdlibAdapterOption func(*StdlibAdapter) + +// TimestampKey sets the key for the timestamp field. By default, it's "ts". +func TimestampKey(key string) StdlibAdapterOption { + return func(a *StdlibAdapter) { a.timestampKey = key } +} + +// FileKey sets the key for the file and line field. By default, it's "caller". +func FileKey(key string) StdlibAdapterOption { + return func(a *StdlibAdapter) { a.fileKey = key } +} + +// MessageKey sets the key for the actual log message. By default, it's "msg". +func MessageKey(key string) StdlibAdapterOption { + return func(a *StdlibAdapter) { a.messageKey = key } +} + +// Prefix configures the adapter to parse a prefix from stdlib log events. If +// you provide a non-empty prefix to the stdlib logger, then your should provide +// that same prefix to the adapter via this option. +// +// By default, the prefix isn't included in the msg key. Set joinPrefixToMsg to +// true if you want to include the parsed prefix in the msg. +func Prefix(prefix string, joinPrefixToMsg bool) StdlibAdapterOption { + return func(a *StdlibAdapter) { a.prefix = prefix; a.joinPrefixToMsg = joinPrefixToMsg } +} + +// NewStdlibAdapter returns a new StdlibAdapter wrapper around the passed +// logger. It's designed to be passed to log.SetOutput. +func NewStdlibAdapter(logger Logger, options ...StdlibAdapterOption) io.Writer { + a := StdlibAdapter{ + Logger: logger, + timestampKey: "ts", + fileKey: "caller", + messageKey: "msg", + } + for _, option := range options { + option(&a) + } + return a +} + +func (a StdlibAdapter) Write(p []byte) (int, error) { + p = a.handlePrefix(p) + + result := subexps(p) + keyvals := []interface{}{} + var timestamp string + if date, ok := result["date"]; ok && date != "" { + timestamp = date + } + if time, ok := result["time"]; ok && time != "" { + if timestamp != "" { + timestamp += " " + } + timestamp += time + } + if timestamp != "" { + keyvals = append(keyvals, a.timestampKey, timestamp) + } + if file, ok := result["file"]; ok && file != "" { + keyvals = append(keyvals, a.fileKey, file) + } + if msg, ok := result["msg"]; ok { + msg = a.handleMessagePrefix(msg) + keyvals = append(keyvals, a.messageKey, msg) + } + if err := a.Logger.Log(keyvals...); err != nil { + return 0, err + } + return len(p), nil +} + +func (a StdlibAdapter) handlePrefix(p []byte) []byte { + if a.prefix != "" { + p = bytes.TrimPrefix(p, []byte(a.prefix)) + } + return p +} + +func (a StdlibAdapter) handleMessagePrefix(msg string) string { + if a.prefix == "" { + return msg + } + + msg = strings.TrimPrefix(msg, a.prefix) + if a.joinPrefixToMsg { + msg = a.prefix + msg + } + return msg +} + +const ( + logRegexpDate = `(?P[0-9]{4}/[0-9]{2}/[0-9]{2})?[ ]?` + logRegexpTime = `(?P