Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simple span error log and gofumpt #214

Merged
merged 3 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion c.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func New(opts ...CoreOption) *C {
diContainer := values.diProvider(conf)
dispatcher := values.eventDispatcherProvider(conf)

var c = C{
c := C{
AppName: appName,
Env: env,
ConfigAccessor: config.WithAccessor(conf),
Expand Down
6 changes: 4 additions & 2 deletions c_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ func TestC_Provide(t *testing.T) {
assert.Len(t, c.Modules(), 2)
}

type a struct{}
type b struct{}
type (
a struct{}
b struct{}
)

func mockConstructor(b b) (a, func(), error) {
return a{}, func() {}, nil
Expand Down
14 changes: 4 additions & 10 deletions clihttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -88,8 +87,7 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
c.tracer.Inject(clientSpan.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
response, err := c.underlying.Do(req)
if err != nil {
ext.Error.Set(clientSpan, true)
clientSpan.LogFields(log.String("event", "error"), log.String("message", err.Error()))
ext.LogError(clientSpan, err)
return response, err
}

Expand All @@ -104,22 +102,19 @@ func (c *Client) logRequest(req *http.Request, span opentracing.Span) {
}
body, err := req.GetBody()
if err != nil {
ext.Error.Set(span, true)
span.LogKV("error", errors.Wrap(err, "cannot get request body"))
ext.LogError(span, errors.Wrap(err, "cannot get request body"))
return
}
defer body.Close()

byt, err := ioutil.ReadAll(io.LimitReader(body, int64(c.responseLogThreshold)))
if err != nil {
ext.Error.Set(span, true)
span.LogKV("error", errors.Wrap(err, "cannot read request body"))
ext.LogError(span, errors.Wrap(err, "cannot read request body"))
return
}
if span != nil {
span.LogKV("request", string(byt))
}

}

func (c *Client) logResponse(response *http.Response, span opentracing.Span) {
Expand All @@ -128,8 +123,7 @@ func (c *Client) logResponse(response *http.Response, span opentracing.Span) {
}
byt, err := ioutil.ReadAll(io.LimitReader(response.Body, int64(c.responseLogThreshold)))
if err != nil {
ext.Error.Set(span, true)
span.LogFields(log.Error(err))
ext.LogError(span, err)
return
}
if span != nil {
Expand Down
2 changes: 1 addition & 1 deletion clihttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestClient_Option(t *testing.T) {

func TestClient_race(t *testing.T) {
// the mock tracer is not concurrent safe.
//tracer := opentracing.GlobalTracer()
// tracer := opentracing.GlobalTracer()
tracer := opentracing.NoopTracer{}
client := NewClient(tracer)
for i := 0; i < 100; i++ {
Expand Down
44 changes: 28 additions & 16 deletions codec/yaml/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func TestCodec_Unmarshal(t *testing.T) {

tests := []struct {
data string
value interface{}
Expand All @@ -18,45 +17,59 @@ func TestCodec_Unmarshal(t *testing.T) {
},
{
"{}", &struct{}{},
}, {
},
{
"v: hi",
map[string]string{"v": "hi"},
}, {
},
{
"v: hi", map[string]interface{}{"v": "hi"},
}, {
},
{
"v: true",
map[string]string{"v": "true"},
}, {
},
{
"v: true",
map[string]interface{}{"v": true},
}, {
},
{
"v: 10",
map[string]interface{}{"v": 10},
}, {
},
{
"v: 0b10",
map[string]interface{}{"v": 2},
}, {
},
{
"v: 0xA",
map[string]interface{}{"v": 10},
}, {
},
{
"v: 4294967296",
map[string]int64{"v": 4294967296},
}, {
},
{
"v: 0.1",
map[string]interface{}{"v": 0.1},
}, {
},
{
"v: .1",
map[string]interface{}{"v": 0.1},
}, {
},
{
"v: .Inf",
map[string]interface{}{"v": math.Inf(+1)},
}, {
},
{
"v: -.Inf",
map[string]interface{}{"v": math.Inf(-1)},
}, {
},
{
"v: -10",
map[string]interface{}{"v": -10},
}, {
},
{
"v: -.1",
map[string]interface{}{"v": -0.1},
},
Expand All @@ -77,7 +90,6 @@ func TestCodec_Unmarshal(t *testing.T) {
if err != nil {
t.Fatalf("(codec{}).Unmarshal should not return err")
}

}

func TestCodec_Marshal(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion config/codec_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type CodecParser struct {

// Unmarshal converts the bytes to map
func (c CodecParser) Unmarshal(bytes []byte) (map[string]interface{}, error) {
var m = make(map[string]interface{})
m := make(map[string]interface{})
if err := c.Codec.Unmarshal(bytes, &m); err != nil {
return m, err
}
Expand Down
5 changes: 3 additions & 2 deletions config/codec_parser_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package config

import (
"io/ioutil"
"testing"

"github.com/DoNewsCode/core/codec/yaml"
yaml2 "github.com/knadh/koanf/parsers/yaml"
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"
)

func TestCodecParser_Marshal(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func NewConfig(options ...Option) (*KoanfAdapter, error) {
// an error occurred, Reload will return early and abort the rest of the
// reloading.
func (k *KoanfAdapter) Reload() error {
var tmp = koanf.New(".")
tmp := koanf.New(".")

for i := len(k.layers) - 1; i >= 0; i-- {
err := tmp.Load(k.layers[i].Provider, k.layers[i].Parser)
Expand Down
9 changes: 4 additions & 5 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ func TestKoanfAdapter_race(t *gotesting.T) {
go ka.Reload()
ka.String("string")
}

}

func TestKoanfAdapter_Watch(t *gotesting.T) {
f, _ := ioutil.TempFile(os.TempDir(), "*")
defer os.Remove(f.Name())

ioutil.WriteFile(f.Name(), []byte("foo: baz"), 0644)
ioutil.WriteFile(f.Name(), []byte("foo: baz"), 0o644)

ka, err := NewConfig(
WithProviderLayer(file.Provider(f.Name()), yaml.Parser()),
Expand All @@ -54,7 +53,7 @@ func TestKoanfAdapter_Watch(t *gotesting.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

var ch = make(chan struct{})
ch := make(chan struct{})
go func() {
ka.watcher.Watch(ctx, func() error {
assert.NoError(t, ka.Reload(), "reload should be successful")
Expand All @@ -65,8 +64,8 @@ func TestKoanfAdapter_Watch(t *gotesting.T) {
})
}()
time.Sleep(time.Second)
ioutil.WriteFile(f.Name(), []byte("foo: bar"), 0644)
ioutil.WriteFile(f.Name(), []byte("foo: bar"), 0644)
ioutil.WriteFile(f.Name(), []byte("foo: bar"), 0o644)
ioutil.WriteFile(f.Name(), []byte("foo: bar"), 0o644)
<-ch

// The following test is flaky on CI. Unable to reproduce locally.
Expand Down
2 changes: 1 addition & 1 deletion config/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Example_minimum() {
}

func Example_configurationStack() {
var fs = flag.NewFlagSet("config", flag.ContinueOnError)
fs := flag.NewFlagSet("config", flag.ContinueOnError)
fs.String("foo", "", "foo value")
fs.Parse([]string{"-foo", "bar"})
conf, _ := config.NewConfig(
Expand Down
4 changes: 2 additions & 2 deletions config/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (m Module) ProvideCommand(command *cobra.Command) {
exportedConfigs = m.exportedConfigs
}
if len(args) >= 1 {
var copy = make([]ExportedConfig, 0)
copy := make([]ExportedConfig, 0)
for i := range m.exportedConfigs {
for j := 0; j < len(args); j++ {
if args[j] == m.exportedConfigs[i].Owner {
Expand Down Expand Up @@ -145,7 +145,7 @@ func (m Module) ProvideCommand(command *cobra.Command) {
exportedConfigs = m.exportedConfigs
}
if len(args) >= 1 {
var copy = make([]ExportedConfig, 0)
copy := make([]ExportedConfig, 0)
for i := range m.exportedConfigs {
for j := 0; j < len(args); j++ {
if args[j] == m.exportedConfigs[i].Owner {
Expand Down
6 changes: 2 additions & 4 deletions config/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import (
)

func setup() *cobra.Command {

var config, _ = NewConfig()
var mod = Module{
config, _ := NewConfig()
mod := Module{
conf: config,
exportedConfigs: []ExportedConfig{
{
Expand Down Expand Up @@ -61,7 +60,6 @@ func tearDown() {
}

func TestModule_ProvideCommand_initCmd(t *testing.T) {

cases := []struct {
name string
output string
Expand Down
4 changes: 2 additions & 2 deletions config/remote/etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestRemote(t *testing.T) {

r := Provider(cfg, "config.yaml")

var testVal = "name: app"
testVal := "name: app"
// PREPARE TEST DATA
if err := put(r, testVal); err != nil {
t.Fatal(err)
Expand All @@ -42,7 +42,7 @@ func TestRemote(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, testVal, string(bytes))

var ch = make(chan string)
ch := make(chan string)
go r.Watch(ctx, func() error {
bytes, err := r.ReadBytes()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions config/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestDuration_UnmarshalJSON(t *testing.T) {
var cases = []struct {
cases := []struct {
name string
value string
expected Duration
Expand Down Expand Up @@ -40,7 +40,7 @@ func TestDuration_UnmarshalJSON(t *testing.T) {
}

func TestDuration_UnmarshalYaml(t *testing.T) {
var cases = []struct {
cases := []struct {
name string
value string
expected Duration
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestDuration_UnmarshalYaml(t *testing.T) {
}

func TestDuration_MarshalJSON(t *testing.T) {
var cases = []struct {
cases := []struct {
name string
value interface{}
expectedJSON string
Expand Down
3 changes: 2 additions & 1 deletion config/watcher/signal.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build !windows
//go:build !windows
// +build !windows

package watcher

Expand Down
1 change: 1 addition & 0 deletions config/watcher/singal_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package watcher
Expand Down
2 changes: 1 addition & 1 deletion container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (f closer) ProvideCloser() {
}

func TestContainer_Shutdown(t *testing.T) {
var seq = 0
seq := 0
container := Container{}
container.AddModule(closer(func() { assert.Equal(t, 2, seq); seq = 1 }))
container.AddModule(closer(func() { assert.Equal(t, 3, seq); seq = 2 }))
Expand Down
1 change: 0 additions & 1 deletion contract/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type ConfigUnmarshaler interface {
// ConfigUnmarshaler is much smaller and thus much easier to customize.
type ConfigAccessor interface {
ConfigUnmarshaler

String(string) string
Int(string) int
Strings(string) []string
Expand Down
1 change: 0 additions & 1 deletion ctxmeta/ctxmeta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func TestContextMeta_crud(t *testing.T) {
baggage.Delete("foo")
_, err = baggage.Get("foo")
assert.ErrorIs(t, err, ErrNotFound)

}

func TestContextMeta_ErrNoBaggae(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions di/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (f *Factory) SubscribeReloadEventFrom(dispatcher contract.Dispatcher) {
if pair.Closer == nil {
return true
}
var finalized = make(chan struct{})
finalized := make(chan struct{})
if reflect.TypeOf(pair.Conn).Kind() == reflect.Ptr {
runtime.SetFinalizer(pair.Conn, func(_ interface{}) { finalized <- struct{}{} })
}
Expand All @@ -89,7 +89,7 @@ func (f *Factory) SubscribeReloadEventFrom(dispatcher contract.Dispatcher) {

// List lists created instance in the factory.
func (f *Factory) List() map[string]Pair {
var out = make(map[string]Pair)
out := make(map[string]Pair)
f.cache.Range(func(key, value interface{}) bool {
out[key.(string)] = value.(Pair)
return true
Expand Down
Loading