Skip to content
This repository has been archived by the owner on Jun 19, 2022. It is now read-only.

Commit

Permalink
update prior to release 0.19 branch (#1903)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Lopez authored Nov 10, 2020
1 parent 80904b0 commit 58158f3
Show file tree
Hide file tree
Showing 21 changed files with 1,051 additions and 63 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ require (
k8s.io/api v0.18.8
k8s.io/apimachinery v0.18.8
k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible
knative.dev/eventing v0.18.1-0.20201109063108-dccb17e10b6b
knative.dev/hack v0.0.0-20201106190108-88f9ae4e6734
knative.dev/pkg v0.0.0-20201106192907-f98befda69eb
knative.dev/serving v0.18.1-0.20201109120108-68c2e63e32ee
knative.dev/eventing v0.18.1-0.20201110185558-0f9a8c55c7b6
knative.dev/hack v0.0.0-20201103151104-3d5abc3a0075
knative.dev/pkg v0.0.0-20201103163404-5514ab0c1fdf
knative.dev/serving v0.18.1-0.20201110011758-db4879e0f615
sigs.k8s.io/yaml v1.2.0
)

Expand Down
15 changes: 7 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
Expand Down Expand Up @@ -726,6 +727,7 @@ github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdh
github.com/sclevine/spec v1.2.0/go.mod h1:W4J29eT/Kzv7/b9IWLB055Z+qvVC9vt0Arko24q7p+U=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand All @@ -744,6 +746,7 @@ github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkU
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
Expand Down Expand Up @@ -1316,22 +1319,18 @@ k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89/go.mod h1:sZAwmy6armz5eXlNoLmJcl
k8s.io/utils v0.0.0-20200603063816-c1c6865ac451 h1:v8ud2Up6QK1lNOKFgiIVrZdMg7MpmSnvtrOieolJKoE=
k8s.io/utils v0.0.0-20200603063816-c1c6865ac451/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
knative.dev/caching v0.0.0-20201104174804-1a305923cfbe/go.mod h1:twy5Yz5LtFbmihtbBG0CcgwJzV5d8RUBoGkZFs7uRQs=
knative.dev/eventing v0.18.1-0.20201109063108-dccb17e10b6b h1:X0cLLfDZeDPLChjG2YWzWWX9YlyK44UQS96cPvvk5js=
knative.dev/eventing v0.18.1-0.20201109063108-dccb17e10b6b/go.mod h1:jwhDgDvoscWE4jWF8cXh7yHfxJcK0mTawVKVfrSjnvg=
knative.dev/eventing v0.18.1-0.20201110185558-0f9a8c55c7b6 h1:OyHSzOB5vOFRiFhL0SJmy7EtJ/LHLTlqC+ctWJvqu84=
knative.dev/eventing v0.18.1-0.20201110185558-0f9a8c55c7b6/go.mod h1:jwhDgDvoscWE4jWF8cXh7yHfxJcK0mTawVKVfrSjnvg=
knative.dev/hack v0.0.0-20201102193445-9349aeeb6701/go.mod h1:PHt8x8yX5Z9pPquBEfIj0X66f8iWkWfR0S/sarACJrI=
knative.dev/hack v0.0.0-20201103151104-3d5abc3a0075 h1:YAgWplKIy4O5e3F5vUUECmXAAyZ0M5ymo6fCt1jeZhs=
knative.dev/hack v0.0.0-20201103151104-3d5abc3a0075/go.mod h1:PHt8x8yX5Z9pPquBEfIj0X66f8iWkWfR0S/sarACJrI=
knative.dev/hack v0.0.0-20201106190108-88f9ae4e6734 h1:AF3ZnSNQrSF6NNjJxD1EvBCr/0/WnSJt6HjCstextBU=
knative.dev/hack v0.0.0-20201106190108-88f9ae4e6734/go.mod h1:PHt8x8yX5Z9pPquBEfIj0X66f8iWkWfR0S/sarACJrI=
knative.dev/networking v0.0.0-20201103163404-b9f80f4537af h1:xPS3bRfp30XHb69B0F9Pgv4Nc2eTfJHWR0TzVRHf1hU=
knative.dev/networking v0.0.0-20201103163404-b9f80f4537af/go.mod h1:0OaR4FLbs3Xn6UjeX1zYql9RWO8sLTO/ZMJS4jDbfFE=
knative.dev/pkg v0.0.0-20201103150904-7f1970af5b6f/go.mod h1:2hMxAUviPH2nl3BuODx5L6shYCdzA+7SPzQpdkcg9wc=
knative.dev/pkg v0.0.0-20201103163404-5514ab0c1fdf h1:QwULgRwcv6R3Ya1GZlf/E1atcaGUNw4DKjxSQUfcR6U=
knative.dev/pkg v0.0.0-20201103163404-5514ab0c1fdf/go.mod h1:cuKOgUvJvnWHIps/apCXX8wZuMlT0dyMZLqRQfsENbQ=
knative.dev/pkg v0.0.0-20201106192907-f98befda69eb h1:wdathwJYpgxe5E35+2245E7oFDfeypAsmJsIzWBPhE0=
knative.dev/pkg v0.0.0-20201106192907-f98befda69eb/go.mod h1:5vNHKNtZtzlNeNrcDoUtZIn+dma/8DjmpjfdHiwoQyM=
knative.dev/serving v0.18.1-0.20201109120108-68c2e63e32ee h1:LKuaz3yq666KfuuoQBMcFGrKoNyZojcmjRKMkfm2y/w=
knative.dev/serving v0.18.1-0.20201109120108-68c2e63e32ee/go.mod h1:G5FVEbwcHKLvf8XTr6XEVqLd3tn31JX8vGFEI8kdYlk=
knative.dev/serving v0.18.1-0.20201110011758-db4879e0f615 h1:HSJGCLfPOgXLa6gsmmDSqWY6/lg9rYaTkyc+IsJVsPQ=
knative.dev/serving v0.18.1-0.20201110011758-db4879e0f615/go.mod h1:G5FVEbwcHKLvf8XTr6XEVqLd3tn31JX8vGFEI8kdYlk=
pgregory.net/rapid v0.3.3/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
Expand Down
4 changes: 2 additions & 2 deletions vendor/knative.dev/eventing/test/e2e-conformance-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ source "$(dirname "$0")/e2e-common.sh"

initialize $@ --skip-istio-addon

echo "Running tests with Multi Tenant Channel Based Broker"
go_test_e2e -timeout=30m -parallel=12 ./test/conformance -brokers=eventing.knative.dev/v1beta1:MTChannelBasedBroker -channels=messaging.knative.dev/v1beta1:Channel,messaging.knative.dev/v1beta1:InMemoryChannel,messaging.knative.dev/v1:Channel,messaging.knative.dev/v1:InMemoryChannel -sources=sources.knative.dev/v1beta1:ApiServerSource,sources.knative.dev/v1alpha2:ContainerSource,sources.knative.dev/v1beta1:PingSource || fail_test
echo "Running Conformance tests for: Multi Tenant Channel Based Broker (v1beta1), Channel (v1beta1, v1), InMemoryChannel (v1beta1, v1) , ApiServerSource (v1beta1, v1), ContainerSource (v1alpha2, v1) and PingSource (v1beta1)"
go_test_e2e -timeout=30m -parallel=12 ./test/conformance -brokers=eventing.knative.dev/v1beta1:MTChannelBasedBroker -channels=messaging.knative.dev/v1beta1:Channel,messaging.knative.dev/v1beta1:InMemoryChannel,messaging.knative.dev/v1:Channel,messaging.knative.dev/v1:InMemoryChannel -sources=sources.knative.dev/v1beta1:ApiServerSource,sources.knative.dev/v1alpha2:ContainerSource,sources.knative.dev/v1beta1:PingSource,sources.knative.dev/v1:ApiServerSource,sources.knative.dev/v1:ContainerSource || fail_test

success
4 changes: 2 additions & 2 deletions vendor/knative.dev/eventing/test/e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ source "$(dirname "$0")/e2e-common.sh"

initialize $@ --skip-istio-addon

echo "Running tests with Multi Tenant Channel Based Broker"
go_test_e2e -timeout=30m -parallel=12 ./test/e2e -brokerclass=MTChannelBasedBroker -channels=messaging.knative.dev/v1beta1:Channel,messaging.knative.dev/v1beta1:InMemoryChannel,messaging.knative.dev/v1:Channel,messaging.knative.dev/v1:InMemoryChannel -sources=sources.knative.dev/v1alpha2:ApiServerSource,sources.knative.dev/v1alpha2:ContainerSource,sources.knative.dev/v1alpha2:PingSource || fail_test
echo "Running E2E tests for: Multi Tenant Channel Based Broker, Channel (v1beta1, v1), InMemoryChannel (v1beta1, v1) , ApiServerSource (v1beta1, v1), ContainerSource (v1alpha2, v1) and PingSource (v1beta1)"
go_test_e2e -timeout=30m -parallel=12 ./test/e2e -brokerclass=MTChannelBasedBroker -channels=messaging.knative.dev/v1beta1:Channel,messaging.knative.dev/v1beta1:InMemoryChannel,messaging.knative.dev/v1:Channel,messaging.knative.dev/v1:InMemoryChannel -sources=sources.knative.dev/v1beta1:ApiServerSource,sources.knative.dev/v1alpha2:ContainerSource,sources.knative.dev/v1beta1:PingSource,sources.knative.dev/v1:ApiServerSource,sources.knative.dev/v1:ContainerSource || fail_test

success
2 changes: 1 addition & 1 deletion vendor/knative.dev/hack/library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ function go_update_deps() {
echo "=== Update Deps for Golang"

local UPGRADE=0
local VERSION="v9000.1" # release v9000 is so far in the future, it will always pick the default branch.
local VERSION="master"
local DOMAIN="knative.dev"
while [[ $# -ne 0 ]]; do
parameter=$1
Expand Down
5 changes: 1 addition & 4 deletions vendor/knative.dev/pkg/logging/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ func NewLogger(configJSON string, levelOverride string, opts ...zap.Option) (*za
if err2 != nil {
panic(err2)
}

slogger := enrichLoggerWithCommitID(logger.Named(fallbackLoggerName))
slogger.Warnw("Failed to parse logging config - using default zap production config", zap.Error(err))
return slogger, loggingCfg.Level
return enrichLoggerWithCommitID(logger.Named(fallbackLoggerName)), loggingCfg.Level
}

func enrichLoggerWithCommitID(logger *zap.Logger) *zap.SugaredLogger {
Expand Down
18 changes: 0 additions & 18 deletions vendor/knative.dev/pkg/metrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ const (
defaultPrometheusPort = 9090
maxPrometheusPort = 65535
minPrometheusPort = 1024
defaultPrometheusHost = "0.0.0.0"
prometheusPortEnvName = "METRICS_PROMETHEUS_PORT"
prometheusHostEnvName = "METRICS_PROMETHEUS_HOST"
)

// Metrics backend "enum".
Expand Down Expand Up @@ -107,10 +105,6 @@ type metricsConfig struct {
// format. It defaults to 9090.
prometheusPort int

// prometheusHost is the host where the metrics are exposed in Prometheus
// format. It defaults to "0.0.0.0"
prometheusHost string

// ---- Stackdriver specific below ----
// True if backendDestination equals to "stackdriver". Store this in a variable
// to reduce string comparison operations.
Expand Down Expand Up @@ -246,7 +240,6 @@ func createMetricsConfig(ctx context.Context, ops ExporterOptions) (*metricsConf
}

mc.prometheusPort = pp
mc.prometheusHost = prometheusHost()
case stackdriver:
// If stackdriverClientConfig is not provided for stackdriver backend destination, OpenCensus will try to
// use the application default credentials. If that is not available, Opencensus would fail to create the
Expand Down Expand Up @@ -348,17 +341,6 @@ func prometheusPort() (int, error) {
return int(pp), nil
}

// prometheusHost returns the host configured via the environment
// for the Prometheus metrics exporter if it's set, a default value otherwise.
// No validation is done here.
func prometheusHost() string {
phStr := os.Getenv(prometheusHostEnvName)
if phStr == "" {
return defaultPrometheusHost
}
return phStr
}

// JSONToOptions converts a json string to ExporterOptions.
func JSONToOptions(jsonOpts string) (*ExporterOptions, error) {
var opts ExporterOptions
Expand Down
7 changes: 0 additions & 7 deletions vendor/knative.dev/pkg/metrics/config_observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ type ObservabilityConfig struct {
// the pods via an HTTP server in the format expected by the pprof visualization tool.
EnableProfiling bool

// DisableQPProfiling indicates whether EnableProfiling flag applies to the QueueProxy processes.
// If false (default) — enabling or disabling profiling causes a global rolling restart of the
// Knative service pods, which might not be desirable (especially if there is no need to profile
// QP processes).
DisableQPProfiling bool

// EnableRequestLog enables activator/queue-proxy to write request logs.
EnableRequestLog bool

Expand Down Expand Up @@ -107,7 +101,6 @@ func NewObservabilityConfigFromConfigMap(configMap *corev1.ConfigMap) (*Observab
cm.AsBool(EnableProbeReqLogKey, &oc.EnableProbeRequestLog),
cm.AsString("metrics.request-metrics-backend-destination", &oc.RequestMetricsBackend),
cm.AsBool("profiling.enable", &oc.EnableProfiling),
cm.AsBool("profiling.disable-qp", &oc.DisableQPProfiling),
cm.AsString("metrics.opencensus-address", &oc.MetricsCollectorAddress),
); err != nil {
return nil, err
Expand Down
7 changes: 1 addition & 6 deletions vendor/knative.dev/pkg/metrics/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,10 @@ type ExporterOptions struct {

// PrometheusPort is the port to expose metrics if metrics backend is Prometheus.
// It should be between maxPrometheusPort and maxPrometheusPort. 0 value means
// using the default 9090 value. It is ignored if metrics backend is not
// using the default 9090 value. If is ignored if metrics backend is not
// Prometheus.
PrometheusPort int

// PrometheusHost is the host to expose metrics on if metrics backend is Prometheus.
// The default value is "0.0.0.0". It is ignored if metrics backend is not
// Prometheus.
PrometheusHost string

// ConfigMap is the data from config map config-observability. Must be present.
// See https://github.com/knative/serving/blob/master/config/config-observability.yaml
// for details.
Expand Down
9 changes: 4 additions & 5 deletions vendor/knative.dev/pkg/metrics/prometheus_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
package metrics

import (
"fmt"
"net/http"
"strconv"
"sync"

prom "contrib.go.opencensus.io/exporter/prometheus"
Expand All @@ -41,7 +41,6 @@ func (emptyPromExporter) ExportView(viewData *view.Data) {
// a signal to enrich the internal Meters with Resource information.
}

//nolint: unparam // False positive of flagging the second result of this function unused.
func newPrometheusExporter(config *metricsConfig, logger *zap.SugaredLogger) (view.Exporter, ResourceExporterFactory, error) {
e, err := prom.NewExporter(prom.Options{Namespace: config.component})
if err != nil {
Expand All @@ -51,7 +50,7 @@ func newPrometheusExporter(config *metricsConfig, logger *zap.SugaredLogger) (vi
logger.Infof("Created Opencensus Prometheus exporter with config: %v. Start the server for Prometheus exporter.", config)
// Start the server for Prometheus scraping
go func() {
srv := startNewPromSrv(e, config.prometheusHost, config.prometheusPort)
srv := startNewPromSrv(e, config.prometheusPort)
srv.ListenAndServe()
}()
return e,
Expand All @@ -74,7 +73,7 @@ func resetCurPromSrv() {
}
}

func startNewPromSrv(e *prom.Exporter, host string, port int) *http.Server {
func startNewPromSrv(e *prom.Exporter, port int) *http.Server {
sm := http.NewServeMux()
sm.Handle("/metrics", e)
curPromSrvMux.Lock()
Expand All @@ -83,7 +82,7 @@ func startNewPromSrv(e *prom.Exporter, host string, port int) *http.Server {
curPromSrv.Close()
}
curPromSrv = &http.Server{
Addr: host + ":" + strconv.Itoa(port),
Addr: fmt.Sprint(":", port),
Handler: sm,
}
return curPromSrv
Expand Down
1 change: 1 addition & 0 deletions vendor/knative.dev/pkg/test/logging/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
memory_encoder.go coverage-excluded=true
49 changes: 49 additions & 0 deletions vendor/knative.dev/pkg/test/logging/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/*
Package logging assists setting up test logging and using leveled logging in tests.
The TLogger is designed to assist the test writer in creating more useful tests and
collecting log data in multiple streams, optimizing for human readability in one and
machine readability in another. It's designed to mimic the testing.T object rather closely and
use Zap logging semantics, both things already in use in Knative, to minimize the time developers
need to spend learning the tool.
Inspired by and uses go-logr.
Advantages
The TLogger enhances test design through subtle nudges and affordances:
* It encourages only logging with .V(), giving the writer a nudge to think about how important it is,
but without requiring them to fit it in a narrowly-defined category.
* Reduces boilerplate of carrying around context for errors in several different variables,
using .WithValues(), which results in more consistent and reusable code across the tests.
Porting
To port code from using testing.T to logging.TLogger, the interfaces knative.dev/pkg/test.T and
knative.dev/pkg/test.TLegacy have been created. All library functions should be refactored to use
one interface and all .Log() calls rewritten to use structured format, which works with testing and
TLogger. If a library function needs test functions not available even in test.TLegacy,
it's probably badly written.
Then any test can be incrementally rewritten to use TLogger, as it coexists with testing.T without issue.
*/
package logging
90 changes: 90 additions & 0 deletions vendor/knative.dev/pkg/test/logging/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package logging

import (
"fmt"

"github.com/davecgh/go-spew/spew"
)

// StructuredError is an error which can hold arbitrary key-value arguments.
//
// TODO(coryrc): The Structured Error is experimental and likely to be removed, but is currently in use in a refactored test.
type StructuredError interface {
error
GetValues() []interface{}
WithValues(...interface{}) StructuredError
DisableValuePrinting()
EnableValuePrinting()
}

type structuredError struct {
msg string
keysAndValues []interface{}
print bool
}

func keysAndValuesToSpewedMap(args ...interface{}) map[string]string {
m := make(map[string]string, len(args)/2)
for i := 0; i < len(args); i += 2 {
key, val := args[i], args[i+1]
if keyStr, ok := key.(string); ok {
m[keyStr] = spew.Sdump(val)
}
}
return m
}

// Error implements `error` interface
func (e structuredError) Error() string {
// TODO(coryrc): accept zap.Field entries?
if e.print {
// %v for fmt.Sprintf does print keys sorted
return fmt.Sprintf("Error: %s\nContext:\n%v", e.msg, keysAndValuesToSpewedMap(e.keysAndValues...))
}
return e.msg
}

// GetValues gives you the structured key values in a plist
func (e structuredError) GetValues() []interface{} {
return e.keysAndValues
}

// DisableValuePrinting disables printing out the keys and values from the Error() method
func (e *structuredError) DisableValuePrinting() {
e.print = false
}

// EnableValuePrinting enables printing out the keys and values from the Error() method
func (e *structuredError) EnableValuePrinting() {
e.print = true
}

// Create a StructuredError. Gives a little better logging when given to a TLogger.
// This may prove to not be useful if users use the logger's WithValues() better.
func Error(msg string, keysAndValues ...interface{}) error {
return &structuredError{msg, keysAndValues, true}
}

// WithValues operates just like TLogger's WithValues but stores them in the error object.
func (e *structuredError) WithValues(keysAndValues ...interface{}) StructuredError {
newKAV := make([]interface{}, 0, len(keysAndValues)+len(e.keysAndValues))
newKAV = append(newKAV, e.keysAndValues...)
newKAV = append(newKAV, keysAndValues...)
return &structuredError{e.msg, newKAV, e.print}
}
Loading

0 comments on commit 58158f3

Please sign in to comment.