diff --git a/example/trace/config.toml b/example/trace/config.toml new file mode 100644 index 0000000000..2f1611ffb2 --- /dev/null +++ b/example/trace/config.toml @@ -0,0 +1,7 @@ +[jupiter.trace.jaeger] +EnableRPCMetrics= true +[jupiter.trace.jaeger.Reporter] +LocalAgentHostPort = "127.0.0.1:6831" +LogSpans = true +[jupiter.trace.jaeger.Sampler] +Param = 0.0001 \ No newline at end of file diff --git a/example/trace/main.go b/example/trace/main.go new file mode 100644 index 0000000000..b1d9580fdc --- /dev/null +++ b/example/trace/main.go @@ -0,0 +1,85 @@ +// Copyright 2020 Douyu +// +// 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 main + +import ( + "context" + "fmt" + "time" + + "github.com/douyu/jupiter/pkg/trace" + + "github.com/douyu/jupiter" + "github.com/douyu/jupiter/pkg/xlog" +) + +// run: go run main.go -config=config.toml +type Engine struct { + jupiter.Application +} + +func NewEngine() *Engine { + eng := &Engine{} + if err := eng.Startup(); err != nil { + xlog.Panic("startup", xlog.Any("err", err)) + } + return eng +} + +func main() { + app := NewEngine() + + for k := 0; k < 10; k++ { + time.Sleep(time.Second) + traceTest() + + } + + if err := app.Run(); err != nil { + panic(err) + } +} + +func traceTest() { + // 1. 从配置文件中初始化 + process1 := func(ctx context.Context) { + span, ctx := trace.StartSpanFromContext(ctx, "process1") + defer span.Finish() + // todo something + time.Sleep(time.Second) + fmt.Println("finish", "process1") + + } + + process2 := func(ctx context.Context) { + span, ctx := trace.StartSpanFromContext(ctx, "process2") + defer span.Finish() + process1(ctx) + time.Sleep(time.Second) + fmt.Println("finish", "process2") + + } + + process3 := func(ctx context.Context) { + span, ctx := trace.StartSpanFromContext(ctx, "process3") + defer span.Finish() + process2(ctx) + time.Sleep(time.Second) + fmt.Println("finish", "process3") + } + + process3(context.Background()) + return +} diff --git a/pkg/trace/jaeger/config.go b/pkg/trace/jaeger/config.go index 357c7f25ab..d6fbd96803 100644 --- a/pkg/trace/jaeger/config.go +++ b/pkg/trace/jaeger/config.go @@ -40,7 +40,7 @@ type Config struct { } // StdConfig ... -func StdConfig(name string) *Config { +func StdConfig() *Config { return RawConfig("jupiter.trace.jaeger") } @@ -111,6 +111,8 @@ func (config *Config) Build(options ...jconfig.Option) opentracing.Tracer { Headers: config.Headers, Tags: config.tags, } + xlog.Info("trace", xlog.Any("configuration", configuration)) + tracer, closer, err := configuration.NewTracer(config.options...) if err != nil { if config.PanicOnError {