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: add zipkin #642

Merged
merged 40 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
b0b32c3
feat:add zipkin
LXPWing May 26, 2022
af1449c
Merge branch 'main' into feature/add_zipkin
LXPWing May 30, 2022
64ac2de
feat: add zipkin
LXPWing Jun 4, 2022
90b188a
feat: add zipkin
LXPWing Jun 9, 2022
ed53e22
feat: add zipkin
LXPWing Jun 10, 2022
b7f8eed
Merge branch 'main' into feature/add_zipkin
LXPWing Jun 10, 2022
1b4a8cd
feat: add jaeger
LXPWing Jun 10, 2022
6654a78
feat: add jaeger
LXPWing Jun 11, 2022
7f22f02
feat: add zipkin
LXPWing Jun 12, 2022
df5470e
fix: zipkin json style
LXPWing Jun 12, 2022
b4136f0
fix: zipkin code
LXPWing Jun 12, 2022
1d35623
fix: zipkin code
LXPWing Jun 13, 2022
eb7064c
Merge remote-tracking branch 'origin/main' into feature/add_zipkin
LXPWing Jun 13, 2022
dc4da91
fix: zipkin code
LXPWing Jun 13, 2022
0d83f25
fix: zipkin code
LXPWing Jun 13, 2022
4d87b74
Merge branch 'main' into feature/add_zipkin
Xunzhuo Jun 17, 2022
bc2178c
fix: zipkin docs
LXPWing Jun 18, 2022
7de5bdc
Merge remote-tracking branch 'origin/feature/add_zipkin' into feature…
LXPWing Jun 18, 2022
d324071
fix: zipkin go mod
LXPWing Jun 18, 2022
927c47f
fix: zipkin go mod
LXPWing Jun 18, 2022
84651fd
fix: zipkin go mod
LXPWing Jun 18, 2022
3cd6cfe
fix: zipkin go mod
LXPWing Jun 18, 2022
0203a53
fix: zipkin quickstart
LXPWing Jun 18, 2022
a37049f
fix: zipkin go mod
LXPWing Jun 18, 2022
c249d0d
fix: zipkin go mod
LXPWing Jun 18, 2022
00827ca
fix: zipkin go mod
LXPWing Jun 18, 2022
358918d
fix: zipkin go mod
LXPWing Jun 18, 2022
a6f4858
fix: zipkin docs
LXPWing Jun 18, 2022
0e4f572
Merge branch 'main' into feature/add_zipkin
seeflood Jun 20, 2022
ffe852d
fix: zipkin json
LXPWing Jun 20, 2022
28523f8
Update config_trace_zipkin.json
LXPWing Jun 20, 2022
5079626
fix: zipkin json
LXPWing Jun 21, 2022
387f742
Merge remote-tracking branch 'origin/feature/add_zipkin' into feature…
LXPWing Jun 21, 2022
2178e6a
fix: zipkin docs
LXPWing Jun 21, 2022
91b57d4
Merge branch 'main' into feature/add_zipkin
LXPWing Jun 21, 2022
0c064b5
fix: zipkin docs
LXPWing Jun 21, 2022
efb6abf
Merge remote-tracking branch 'origin/feature/add_zipkin' into feature…
LXPWing Jun 21, 2022
4dcc937
fix: zipkin docs
LXPWing Jun 21, 2022
6669503
fix: zipkin docs
LXPWing Jun 21, 2022
18502fe
upload img to cdn
seeflood Jun 21, 2022
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
5 changes: 5 additions & 0 deletions cmd/layotto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
"strconv"
"time"

mosn_zipkin "mosn.io/mosn/pkg/trace/zipkin"

"mosn.io/layotto/diagnostics/zipkin"

mosn_jaeger "mosn.io/mosn/pkg/trace/jaeger"

"github.com/dapr/components-contrib/secretstores"
Expand Down Expand Up @@ -519,6 +523,7 @@ func ExtensionsRegister(_ *cli.Context) {
trace.RegisterTracerBuilder("SOFATracer", lprotocol.Layotto, diagnostics.NewTracer)
trace.RegisterTracerBuilder(skywalking.SkyDriverName, lprotocol.Layotto, lsky.NewGrpcSkyTracer)
trace.RegisterTracerBuilder(mosn_jaeger.DriverName, lprotocol.Layotto, jaeger.NewGrpcJaegerTracer)
trace.RegisterTracerBuilder(mosn_zipkin.DriverName, lprotocol.Layotto, zipkin.NewGrpcZipTracer)
}

func main() {
Expand Down
3 changes: 3 additions & 0 deletions cmd/layotto_multiple_api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
secretstore_env "github.com/dapr/components-contrib/secretstores/local/env"
secretstore_file "github.com/dapr/components-contrib/secretstores/local/file"
mosn_jaeger "mosn.io/mosn/pkg/trace/jaeger"
mosn_zipkin "mosn.io/mosn/pkg/trace/zipkin"

secretstores_loader "mosn.io/layotto/pkg/runtime/secretstores"

Expand Down Expand Up @@ -179,6 +180,7 @@ import (
"mosn.io/layotto/diagnostics/jaeger"
lprotocol "mosn.io/layotto/diagnostics/protocol"
lsky "mosn.io/layotto/diagnostics/skywalking"
"mosn.io/layotto/diagnostics/zipkin"
)

// loggerForDaprComp is constructed for reusing dapr's components.
Expand Down Expand Up @@ -531,6 +533,7 @@ func ExtensionsRegister(_ *cli.Context) {
trace.RegisterTracerBuilder("SOFATracer", "layotto", diagnostics.NewTracer)
trace.RegisterTracerBuilder(skywalking.SkyDriverName, lprotocol.Layotto, lsky.NewGrpcSkyTracer)
trace.RegisterTracerBuilder(mosn_jaeger.DriverName, lprotocol.Layotto, jaeger.NewGrpcJaegerTracer)
trace.RegisterTracerBuilder(mosn_zipkin.DriverName, lprotocol.Layotto, zipkin.NewGrpcZipTracer)
}

func main() {
Expand Down
87 changes: 87 additions & 0 deletions configs/config_trace_zipkin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"servers": [
{
"default_log_path": "stdout",
"default_log_level": "INFO",
"listeners": [
{
"name": "grpc",
"address": "0.0.0.0:34904",
"bind_port": true,
"filter_chains": [
{
"filters": [
{
"type": "grpc",
"config": {
"server_name": "runtime",
"grpc_config": {
"hellos": {
"quick_start_demo": {
"type": "helloworld",
"hello": "greeting"
}
},
"config_store": {
"config_demo": {
"type": "etcd",
"address": [
"127.0.0.1:2379"
],
"timeout": "10"
}
}
}
}
}
]
}
],
"stream_filters": [
{
"type": "flowControlFilter",
"config": {
"global_switch": true,
"limit_key_type": "PATH",
"rules": [
{
"resource": "/spec.proto.runtime.v1.Runtime/SayHello",
"grade": 1,
"threshold": 5
}
]
}
},
{
"type": "grpc_metric"
}
]
}
]
}
],
"tracing": {
"enable": true,
"driver": "Zipkin",
"config": {
"config": {
"config": {
"service_name": "layotto",
"reporter_endpoint": "http://127.0.0.1:9411/api/v2/spans",
"recorder_host_post": "127.0.0.1:9000"
}
}
}
},
"metrics": {
"sinks": [
{
"type": "prometheus",
"config": {
"port": 34903
}
}
]
}
}

141 changes: 141 additions & 0 deletions diagnostics/zipkin/grpc_tracer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright 2021 Layotto 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 zipkin

import (
"context"
"time"

"mosn.io/layotto/diagnostics/grpc"

"github.com/openzipkin/zipkin-go"
reporterhttp "github.com/openzipkin/zipkin-go/reporter/http"
"mosn.io/api"
"mosn.io/mosn/pkg/types"
"mosn.io/pkg/log"

ltrace "mosn.io/layotto/components/trace"
)

const (
service_name = "service_name"
reporter_endpoint = "reporter_endpoint"
recorder_host_post = "recorder_host_post"
configs = "config"

defaultReporterEndpoint = "http://127.0.0.1:9411/api/v2/spans"
defaultServiceName = "layotto"
defaultRecorderHostPost = "127.0.0.1:9000"
)

type grpcZipTracer struct {
*zipkin.Tracer
}

type grpcZipSpan struct {
*ltrace.Span
tracer *grpcZipTracer
ctx context.Context
span zipkin.Span
}

func NewGrpcZipTracer(traceCfg map[string]interface{}) (api.Tracer, error) {
reporter := reporterhttp.NewReporter(getReporterEndpoint(traceCfg))

endpoint, err := zipkin.NewEndpoint(getServerName(traceCfg), getRecorderHostPort(traceCfg))
if err != nil {
log.DefaultLogger.Errorf("[layotto] [zipkin] [tracer] unable to create zipkin reporter endpoint")
return nil, err
}

tracer, err := zipkin.NewTracer(reporter, zipkin.WithLocalEndpoint(endpoint), zipkin.WithTraceID128Bit(true))
if err != nil {
log.DefaultLogger.Errorf("[layotto] [zipkin] [tracer] cannot initialize zipkin Tracer")
return nil, err
}

log.DefaultLogger.Infof("[layotto] [zipkin] [tracer] create success")

return &grpcZipTracer{
tracer,
}, nil
}

func getRecorderHostPort(traceCfg map[string]interface{}) string {
if cfg, ok := traceCfg[configs]; ok {
recorderHostPort := cfg.(map[string]interface{})
if point, ok := recorderHostPort[recorder_host_post]; ok {
return point.(string)
}
}

return defaultRecorderHostPost
LXPWing marked this conversation as resolved.
Show resolved Hide resolved
}

func getReporterEndpoint(traceCfg map[string]interface{}) string {
if cfg, ok := traceCfg[configs]; ok {
endpoint := cfg.(map[string]interface{})
if point, ok := endpoint[reporter_endpoint]; ok {
return point.(string)
}
}

return defaultReporterEndpoint
LXPWing marked this conversation as resolved.
Show resolved Hide resolved
}

func getServerName(traceCfg map[string]interface{}) string {
if cfg, ok := traceCfg[configs]; ok {
serverName := cfg.(map[string]interface{})
if name, ok := serverName[service_name]; ok {
return name.(string)
}
}

return defaultServiceName
LXPWing marked this conversation as resolved.
Show resolved Hide resolved
}

func (t *grpcZipTracer) Start(ctx context.Context, request interface{}, _ time.Time) api.Span {
info, ok := request.(*grpc.RequestInfo)
if !ok {
log.DefaultLogger.Debugf("[layotto] [zipkin] [tracer] unable to get request header, downstream trace ignored")
return nil
}

// start span
span := t.StartSpan(info.FullMethod)

return &grpcZipSpan{
tracer: t,
ctx: ctx,
Span: &ltrace.Span{},
span: span,
}
}

func (s *grpcZipSpan) TraceId() string {
return s.span.Context().TraceID.String()
}

func (s *grpcZipSpan) InjectContext(requestHeaders types.HeaderMap, requestInfo api.RequestInfo) {
}

func (s *grpcZipSpan) SetRequestInfo(requestInfo api.RequestInfo) {
}

func (s *grpcZipSpan) FinishSpan() {
s.span.Finish()
}
23 changes: 23 additions & 0 deletions diagnostics/zipkin/zipkin-docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#Licensed to the Apache Software Foundation (ASF) under one or more
#contributor license agreements. See the NOTICE file distributed with
#this work for additional information regarding copyright ownership.
#The ASF licenses this file to You 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.

version: '3.3'
services:
zipkin:
image: openzipkin/zipkin:latest
container_name: zipkin
restart: always
ports:
- "9411:9411"
Binary file added docs/img/trace/zipkin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/zh/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- [Metrics 接入 Prometheus](zh/start/trace/prometheus.md)
- [Trace 接入 Skywalking](zh/start/trace/skywalking.md)
- [Trace 接入 Jaeger](zh/start/trace/jaeger.md)
- [Trace 接入 Zipkin](zh/start/trace/zipkin.md)
- [将业务逻辑通过 WASM 下沉进sidecar](zh/start/wasm/start.md)
- [基于 WASM 跟 Runtime 实现的 Faas 模型](zh/start/faas/start.md)
- [用户手册](zh/building_blocks/)
Expand Down
93 changes: 93 additions & 0 deletions docs/zh/start/trace/zipkin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# ZipKin trace 接入

## 配置

示例:configs/config_trace_zipkin.json

```json
{
"tracing": {
"enable": true,
"driver": "Zipkin",
"config": {
"config": {
"config": {
"service_name": "layotto",
"reporter_endpoint": "http://127.0.0.1:9411/api/v2/spans",
"recorder_host_post": "127.0.0.1:9000"
}
}
}
}
}

```
| 字段 | 必填 | 说明 |
|------|-----|----------------------------------------------|
| service_name | Y | 服务名称,默认为layotto |
| reporter_endpoint | Y | 链路日志输出,默认为http://127.0.0.1:9411/api/v2/spans |
| recorder_host_post | Y | 当前服务端口号,默认为127.0.0.1:9000 |
LXPWing marked this conversation as resolved.
Show resolved Hide resolved

注意:目前只支持Http方式的Reporter。

## 运行ZipKin

```shell
cd ${project_path}/diagnostics/zipkin

docker-compose -f zipkin-docker-compose.yaml up -d
```

## 运行layotto

可以按照如下方式启动一个layotto的server:
LXPWing marked this conversation as resolved.
Show resolved Hide resolved

切换目录:

```shell
cd ${project_path}/cmd/layotto_multiple_api
```

构建:

```shell @if.not.exist layotto
go build -o layotto
```

运行:

```shell @background
./layotto start -c ../../configs/config_trace_zipkin.json
```

## 运行 Demo

对应的调用端代码在[client.go](https://github.com/mosn/layotto/blob/main/demo/flowcontrol/client.go) 中,运行它会调用layotto的SayHello接口:

切换目录:

```shell
cd ${project_path}/demo/flowcontrol/
```

构建:

```shell @if.not.exist client
go build -o client
```
运行:

```shell
./client
```
访问:http://localhost:9411/zipkin/?serviceName=layotto&lookback=15m&endTs=1655559536414&limit=10

![](../../../img/trace/zipkin.png)

## 清理资源

```shell
cd ${project_path}/diagnostics/zipkin

docker-compose -f zipkin-docker-compose.yaml down
```
Loading