Skip to content

Commit

Permalink
otlp add config for enable secure connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Hkesd committed Nov 16, 2023
1 parent 3a10f78 commit 4e3f78b
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions core/etrace/otel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package otel
import (
"context"

"github.com/gotomicro/ego/core/eapp"
"github.com/gotomicro/ego/core/econf"
"github.com/gotomicro/ego/core/elog"
"github.com/gotomicro/ego/internal/ienv"
jaegerv2 "go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/resource"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
"go.opentelemetry.io/otel/trace"

"github.com/gotomicro/ego/core/eapp"
"github.com/gotomicro/ego/core/econf"
"github.com/gotomicro/ego/core/elog"
"github.com/gotomicro/ego/internal/ienv"
)

// Config ...
Expand All @@ -29,10 +30,11 @@ type Config struct {

// otlpConfig otlp上报协议配置
type otlpConfig struct {
Endpoint string // oltp endpoint
Headers map[string]string // 默认提供一个 请求头的参数配置
options []otlptracegrpc.Option // 预留自定义配置: 例如 grpc WithGRPCConn
resOptions []resource.Option // res 预留自定以配置
Endpoint string // oltp endpoint
Headers map[string]string // 默认提供一个 请求头的参数配置
EnableInsecure bool // 是否启用不安全连接 default: true
options []otlptracegrpc.Option // 预留自定义配置: 例如 grpc WithGRPCConn
resOptions []resource.Option // res 预留自定以配置
}

// jaegerConfig jaeger上报协议配置
Expand Down Expand Up @@ -68,7 +70,8 @@ func DefaultConfig() *Config {
EndpointType: "collector",
},
Otlp: otlpConfig{
Endpoint: ienv.EnvOrStr("OTEL_EXPORTER_OTLP_ENDPOINT", "localhost:4317"),
Endpoint: ienv.EnvOrStr("OTEL_EXPORTER_OTLP_ENDPOINT", "localhost:4317"),
EnableInsecure: true,
},
OtelType: "otlp",
PanicOnError: true,
Expand Down Expand Up @@ -150,11 +153,15 @@ func (config *Config) buildJaegerTP() trace.TracerProvider {
func (config *Config) buildOtlpTP() trace.TracerProvider {
// otlp exporter
options := []otlptracegrpc.Option{
otlptracegrpc.WithInsecure(), // WithInsecure disables client transport security for the exporter's gRPC
otlptracegrpc.WithHeaders(config.Otlp.Headers), // WithHeaders will send the provided headers with each gRPC requests.
otlptracegrpc.WithEndpoint(config.Otlp.Endpoint), // WithEndpoint sets the target endpoint the exporter will connect to. If unset, localhost:4317 will be used as a default.
// otlptracegrpc.WithDialOption(grpc.WithBlock()), //默认不设置 同步状态,会产生阻塞等待 Ready
}
if config.Otlp.EnableInsecure {
// WithInsecure disables client transport security for the exporter's gRPC
options = append(options, otlptracegrpc.WithInsecure())
}

options = append(options, config.Otlp.options...)
traceClient := otlptracegrpc.NewClient(options...)
ctx := context.Background()
Expand Down

0 comments on commit 4e3f78b

Please sign in to comment.