-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
10 changed files
with
429 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
] | ||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: <race.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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.