Skip to content

Commit

Permalink
feat: add jaeger (#590)
Browse files Browse the repository at this point in the history
* feature: add jaeger

* feat: add jaeger

* feat: add jaeger

* feat: add jaeger

* feat: add jaeger

* feat: add jaeger

* fix: code style

* fix: code style

* fix: code style

* fix: ut code

* fix: ut code

* fix: ut code

* feat: add docs

* fix: docs

* fix: add quickstart

* fix: go mod

* fix: go mod

* fix: go mod

* fix: docs

* fix: docs

* fix: docs

* fix: json

* fix: docs side

* fix: docs png

* add comments

* fix: jaeger strategy check

Co-authored-by: seeflood <[email protected]>
  • Loading branch information
LXPWing and seeflood authored Jun 7, 2022
1 parent fea7d5d commit 36ed813
Show file tree
Hide file tree
Showing 10 changed files with 429 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cmd/layotto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ import (
_ "mosn.io/layotto/pkg/wasm"

_ "mosn.io/layotto/diagnostics/exporter_iml"
"mosn.io/layotto/diagnostics/jaeger"
lprotocol "mosn.io/layotto/diagnostics/protocol"
lsky "mosn.io/layotto/diagnostics/skywalking"
)
Expand Down Expand Up @@ -515,7 +516,7 @@ func ExtensionsRegister(_ *cli.Context) {
trace.RegisterTracerBuilder("SOFATracer", protocol.HTTP1, tracehttp.NewTracer)
trace.RegisterTracerBuilder("SOFATracer", lprotocol.Layotto, diagnostics.NewTracer)
trace.RegisterTracerBuilder(skywalking.SkyDriverName, lprotocol.Layotto, lsky.NewGrpcSkyTracer)

trace.RegisterTracerBuilder("Jaeger", lprotocol.Layotto, jaeger.NewGrpcJaegerTracer)
}

func main() {
Expand Down
2 changes: 2 additions & 0 deletions cmd/layotto_multiple_api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ import (
"mosn.io/layotto/pkg/runtime"

_ "mosn.io/layotto/diagnostics/exporter_iml"
"mosn.io/layotto/diagnostics/jaeger"
lprotocol "mosn.io/layotto/diagnostics/protocol"
lsky "mosn.io/layotto/diagnostics/skywalking"
)
Expand Down Expand Up @@ -528,6 +529,7 @@ func ExtensionsRegister(_ *cli.Context) {
trace.RegisterTracerBuilder("SOFATracer", protocol.HTTP1, tracehttp.NewTracer)
trace.RegisterTracerBuilder("SOFATracer", "layotto", diagnostics.NewTracer)
trace.RegisterTracerBuilder(skywalking.SkyDriverName, lprotocol.Layotto, lsky.NewGrpcSkyTracer)
trace.RegisterTracerBuilder("Jaeger", lprotocol.Layotto, jaeger.NewGrpcJaegerTracer)

}

Expand Down
86 changes: 86 additions & 0 deletions configs/config_trace_jaeger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"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": "jaeger",
"config": {
"config": {
"service_name": "layotto",
"strategy": "collector"
}
}
},
"metrics": {
"sinks": [
{
"type": "prometheus",
"config": {
"port": 34903
}
}
]
}
}



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

import (
"context"
"os"
"time"

"github.com/pkg/errors"

"github.com/opentracing/opentracing-go"
jaegerc "github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/config"
"mosn.io/api"
"mosn.io/mosn/pkg/log"
"mosn.io/mosn/pkg/trace"
"mosn.io/mosn/pkg/trace/jaeger"
"mosn.io/mosn/pkg/types"

ltrace "mosn.io/layotto/components/trace"
"mosn.io/layotto/diagnostics/grpc"
"mosn.io/layotto/diagnostics/protocol"
)

const (
serviceName = "service_name"
strategy = "strategy"
agentHost = "agent_host"
collectorEndpoint = "collector_endpoint"
defaultServiceName = "layotto"
defaultJaegerAgentHost = "127.0.0.1:6831"
jaegerAgentHostKey = "TRACE"
appIDKey = "APP_ID"
defaultCollectorEndpoint = "http://127.0.0.1:14268/api/traces"
defaultStrategy = "collector"
configs = "config"
)

type grpcJaegerTracer struct {
tracer opentracing.Tracer
}

type grpcJaegerSpan struct {
*ltrace.Span
ctx context.Context
trace *grpcJaegerTracer
jaegerSpan opentracing.Span
spanCtx jaegerc.SpanContext
}

func init() {
trace.RegisterTracerBuilder(jaeger.DriverName, protocol.Layotto, NewGrpcJaegerTracer)
}

func NewGrpcJaegerTracer(traceCfg map[string]interface{}) (api.Tracer, error) {
// 1. construct the ReporterConfig, which is used to communicate with jaeger
var reporter *config.ReporterConfig

// Determining whether to start the agent
strategy, err := getStrategy(traceCfg)

if err != nil {
return nil, err
}

if strategy == defaultStrategy {
reporter = &config.ReporterConfig{
LogSpans: false,
BufferFlushInterval: 1 * time.Second,
CollectorEndpoint: getCollectorEndpoint(traceCfg),
}
} else {
reporter = &config.ReporterConfig{
LogSpans: false,
BufferFlushInterval: 1 * time.Second,
LocalAgentHostPort: getAgentHost(traceCfg),
}
}
// 2. construct the Configuration
cfg := config.Configuration{
Disabled: false,
Sampler: &config.SamplerConfig{
Type: "const",
Param: 1,
},
Reporter: reporter,
}

cfg.ServiceName = getServiceName(traceCfg)

// 3. use the Configuration to construct a new tracer
tracer, _, err := cfg.NewTracer()

log.DefaultLogger.Infof("[layotto] [jaeger] [tracer] report service name:%s", getServiceName(traceCfg))

if err != nil {
log.DefaultLogger.Errorf("[layotto] [jaeger] [tracer] cannot initialize Jaeger Tracer")
return nil, err
}

// 4. adapt to the `api.Tracer`
return &grpcJaegerTracer{
tracer: tracer,
}, nil
}

func getAgentHost(traceCfg map[string]interface{}) string {
if cfg, ok := traceCfg[configs]; ok {
host := cfg.(map[string]interface{})
if agentHost, ok := host[agentHost]; ok {
return agentHost.(string)
}
}

//if TRACE is not set, get it from the env variable
if host := os.Getenv(jaegerAgentHostKey); host != "" {
return host
}

return defaultJaegerAgentHost
}

func getStrategy(traceCfg map[string]interface{}) (string, error) {
if cfg, ok := traceCfg[configs]; ok {
str := cfg.(map[string]interface{})
k, ok := str[strategy]
if ok && (k.(string) == defaultStrategy || k.(string) == "agent") {
return k.(string), nil
} else if ok {
return "", errors.New("Unknown Strategy")
}
}

return defaultStrategy, nil
}

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

return defaultCollectorEndpoint
}

func getServiceName(traceCfg map[string]interface{}) string {
if cfg, ok := traceCfg[configs]; ok {
name := cfg.(map[string]interface{})
if service, ok := name[serviceName]; ok {
return service.(string)
}
}

//if service_name is not set, get it from the env variable
if appID := os.Getenv(appIDKey); appID != "" {
return appID + "_sidecar"
}

return defaultServiceName
}

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

//create entry span (downstream)
sp, _ := opentracing.StartSpanFromContextWithTracer(ctx, t.tracer, header.FullMethod)

//renew span context
newSpanCtx, _ := sp.Context().(jaegerc.SpanContext)

return &grpcJaegerSpan{
trace: t,
ctx: ctx,
Span: &ltrace.Span{},
spanCtx: newSpanCtx,
jaegerSpan: sp,
}
}

func (s *grpcJaegerSpan) TraceId() string {
return s.spanCtx.TraceID().String()
}

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

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

func (s *grpcJaegerSpan) FinishSpan() {
s.jaegerSpan.Finish()
}
29 changes: 29 additions & 0 deletions diagnostics/jaeger/jaeger-docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#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:
jaegertracing-all-in-one:
image: jaegertracing/all-in-one:latest
ports:
- "5775:5775/udp"
- "16686:16686"
- "14250:14250"
- "14268:14268"
- "6831:6831"




1 change: 1 addition & 0 deletions docs/zh/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [Trace, Metrics](zh/start/trace/trace.md)
- [Metrics 接入 Prometheus](zh/start/trace/prometheus.md)
- [Trace 接入 Skywalking](zh/start/trace/skywalking.md)
- [Trace 接入 Jaeger](zh/start/trace/jaeger.md)
- [将业务逻辑通过 WASM 下沉进sidecar](zh/start/wasm/start.md)
- [基于 WASM 跟 Runtime 实现的 Faas 模型](zh/start/faas/start.md)
- [用户手册](zh/building_blocks/)
Expand Down
Loading

0 comments on commit 36ed813

Please sign in to comment.