Skip to content

Commit

Permalink
feat: add zipkin (#642)
Browse files Browse the repository at this point in the history
Co-authored-by: Xunzhuo <[email protected]>
Co-authored-by: seeflood <[email protected]>
  • Loading branch information
3 people authored Jun 22, 2022
1 parent 66e64e9 commit 34da00a
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 3 deletions.
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
83 changes: 83 additions & 0 deletions configs/config_trace_zipkin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"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": {
"service_name": "layotto",
"reporter_endpoint": "http://127.0.0.1:9411/api/v2/spans",
"recorder_host_post": "127.0.0.1:34904"
}
},
"metrics": {
"sinks": [
{
"type": "prometheus",
"config": {
"port": 34903
}
}
]
}
}

143 changes: 143 additions & 0 deletions diagnostics/zipkin/grpc_tracer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* 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"
"fmt"
"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"
)

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) {
point, err := getReporterEndpoint(traceCfg)
if err != nil {
return nil, err
}

reporter := reporterhttp.NewReporter(point)

name, err := getServerName(traceCfg)
if err != nil {
return nil, err
}

host_post, err := getRecorderHostPort(traceCfg)
if err != nil {
return nil, err
}

endpoint, err := zipkin.NewEndpoint(name, host_post)
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, error) {
if recorder, ok := traceCfg[recorder_host_post]; ok {
return recorder.(string), nil
}

return "", fmt.Errorf("[layotto] [zipkin] [tracer] no config zipkin server host and port")
}

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

return "", fmt.Errorf("[layotto] [zipkin] [tracer] no config zipkin reporter endpoint")
}

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

return "", fmt.Errorf("[layotto] [zipkin] [tracer] no config zipkin server name")
}

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"
1 change: 1 addition & 0 deletions docs/zh/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- 可观测性
- [Trace, Metrics](zh/start/trace/trace.md)
- [Trace 接入 Skywalking](zh/start/trace/skywalking.md)
- [Trace 接入 Zipkin](zh/start/trace/zipkin.md)
- [Trace 接入 Jaeger](zh/start/trace/jaeger.md)
- [Metrics 接入 Prometheus](zh/start/trace/prometheus.md)
- [将业务逻辑通过 WASM 下沉进sidecar](zh/start/wasm/start.md)
Expand Down
Loading

0 comments on commit 34da00a

Please sign in to comment.