forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: opentelemetry integration, removed self designed tracing (zerom…
…icro#1111) * feat: opentelemetry integration, removed self designed tracing * feat: support zipkin on opentelemetry integration * feat: support zipkin on opentelemetry integration, enable it in conf * style: format code * fix: support logx without exporter configured * fix: check return values * refactor: simplify code * refactor: simplify opentelemetry integration * ci: fix staticcheck errors
- Loading branch information
Showing
53 changed files
with
605 additions
and
1,519 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,9 +15,6 @@ | |
**/.DS_Store | ||
**/logs | ||
|
||
# ignore adhoc test code | ||
**/adhoc | ||
|
||
# gitlab ci | ||
.cache | ||
|
||
|
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
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,69 @@ | ||
package trace | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/tal-tech/go-zero/core/logx" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/exporters/jaeger" | ||
"go.opentelemetry.io/otel/exporters/zipkin" | ||
"go.opentelemetry.io/otel/propagation" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0" | ||
) | ||
|
||
const ( | ||
kindJaeger = "jaeger" | ||
kindZipkin = "zipkin" | ||
) | ||
|
||
var once sync.Once | ||
|
||
// StartAgent starts a opentelemetry agent. | ||
func StartAgent(c Config) { | ||
once.Do(func() { | ||
startAgent(c) | ||
}) | ||
} | ||
|
||
func createExporter(c Config) (sdktrace.SpanExporter, error) { | ||
// Just support jaeger now, more for later | ||
switch c.Batcher { | ||
case kindJaeger: | ||
return jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(c.Endpoint))) | ||
case kindZipkin: | ||
return zipkin.New(c.Endpoint) | ||
default: | ||
return nil, fmt.Errorf("unknown exporter: %s", c.Batcher) | ||
} | ||
} | ||
|
||
func startAgent(c Config) { | ||
opts := []sdktrace.TracerProviderOption{ | ||
// Set the sampling rate based on the parent span to 100% | ||
sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(c.Sampler))), | ||
// Record information about this application in an Resource. | ||
sdktrace.WithResource(resource.NewSchemaless(semconv.ServiceNameKey.String(c.Name))), | ||
} | ||
|
||
if len(c.Endpoint) > 0 { | ||
exp, err := createExporter(c) | ||
if err != nil { | ||
logx.Error(err) | ||
return | ||
} | ||
|
||
// Always be sure to batch in production. | ||
opts = append(opts, sdktrace.WithBatcher(exp)) | ||
} | ||
|
||
tp := sdktrace.NewTracerProvider(opts...) | ||
otel.SetTracerProvider(tp) | ||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator( | ||
propagation.TraceContext{}, propagation.Baggage{})) | ||
otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) { | ||
logx.Errorf("[otel] error: %v", err) | ||
})) | ||
} |
2 changes: 1 addition & 1 deletion
2
core/trace/opentelemetry/attributes.go → core/trace/attributes.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package opentelemetry | ||
package trace | ||
|
||
import ( | ||
"go.opentelemetry.io/otel/attribute" | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.