mirror of
https://github.com/linka-cloud/grpc.git
synced 2025-10-18 19:31:46 +00:00
add otel module based on uptrace-go
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
64
otel/logging.go
Normal file
64
otel/logging.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package otel
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
|
||||
"go.opentelemetry.io/otel/log/global"
|
||||
sdklog "go.opentelemetry.io/otel/sdk/log"
|
||||
)
|
||||
|
||||
func configureLogging(ctx context.Context, conf *config) *sdklog.LoggerProvider {
|
||||
var opts []sdklog.LoggerProviderOption
|
||||
if res := conf.newResource(); res != nil {
|
||||
opts = append(opts, sdklog.WithResource(res))
|
||||
}
|
||||
|
||||
for _, dsn := range conf.dsn {
|
||||
dsn, err := ParseDSN(dsn)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("ParseDSN failed")
|
||||
continue
|
||||
}
|
||||
|
||||
exp, err := newOtlpLogExporter(ctx, conf, dsn)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("otlploghttp.New failed")
|
||||
continue
|
||||
}
|
||||
|
||||
queueSize := queueSize()
|
||||
bspOptions := []sdklog.BatchProcessorOption{
|
||||
sdklog.WithMaxQueueSize(queueSize),
|
||||
sdklog.WithExportMaxBatchSize(queueSize),
|
||||
sdklog.WithExportInterval(10 * time.Second),
|
||||
sdklog.WithExportTimeout(10 * time.Second),
|
||||
}
|
||||
bsp := sdklog.NewBatchProcessor(exp, bspOptions...)
|
||||
opts = append(opts, sdklog.WithProcessor(bsp))
|
||||
}
|
||||
|
||||
provider := sdklog.NewLoggerProvider(opts...)
|
||||
global.SetLoggerProvider(provider)
|
||||
|
||||
return provider
|
||||
}
|
||||
|
||||
func newOtlpLogExporter(
|
||||
ctx context.Context, conf *config, dsn *DSN,
|
||||
) (*otlploghttp.Exporter, error) {
|
||||
options := []otlploghttp.Option{
|
||||
otlploghttp.WithEndpoint(dsn.OTLPHttpEndpoint()),
|
||||
otlploghttp.WithHeaders(dsn.Headers()),
|
||||
otlploghttp.WithCompression(otlploghttp.GzipCompression),
|
||||
}
|
||||
|
||||
if conf.tlsConf != nil {
|
||||
options = append(options, otlploghttp.WithTLSClientConfig(conf.tlsConf))
|
||||
} else if dsn.Scheme == "http" {
|
||||
options = append(options, otlploghttp.WithInsecure())
|
||||
}
|
||||
|
||||
return otlploghttp.New(ctx, options...)
|
||||
}
|
Reference in New Issue
Block a user