Skip to content

Commit

Permalink
Beholder: add support for TLS connection to OTel Collector
Browse files Browse the repository at this point in the history
  • Loading branch information
pkcll committed Aug 9, 2024
1 parent 559ea06 commit 0bdc6d2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
24 changes: 17 additions & 7 deletions pkg/beholder/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"go.opentelemetry.io/otel/sdk/trace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
oteltrace "go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)

type Emitter interface {
Expand Down Expand Up @@ -91,9 +93,17 @@ func newOtelClient(cfg Config, errorHandler errorHandlerFunc, otlploggrpcNew otl
if err != nil {
return nil, err
}
var creds credentials.TransportCredentials
creds = insecure.NewCredentials()
if !cfg.InsecureConnection && cfg.TLSCertFile != "" {
creds, err = credentials.NewClientTLSFromFile(cfg.TLSCertFile, "")
if err != nil {
return nil, err
}
}
sharedLogExporter, err := otlploggrpcNew(
ctx,
otlploggrpc.WithInsecure(),
otlploggrpc.WithTLSCredentials(creds),
otlploggrpc.WithEndpoint(cfg.OtelExporterGRPCEndpoint),
)
if err != nil {
Expand Down Expand Up @@ -125,7 +135,7 @@ func newOtelClient(cfg Config, errorHandler errorHandlerFunc, otlploggrpcNew otl
otelglobal.SetLoggerProvider(loggerProvider)

// Tracer
tracerProvider, err := newTracerProvider(cfg, baseResource)
tracerProvider, err := newTracerProvider(cfg, baseResource, creds)
if err != nil {
return nil, err
}
Expand All @@ -134,7 +144,7 @@ func newOtelClient(cfg Config, errorHandler errorHandlerFunc, otlploggrpcNew otl
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))

// Meter
meterProvider, err := newMeterProvider(cfg, baseResource)
meterProvider, err := newMeterProvider(cfg, baseResource, creds)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -276,11 +286,11 @@ func closeFunc(ctx context.Context, providers ...otelProvider) func() error {
}
}

func newTracerProvider(config Config, resource *sdkresource.Resource) (*sdktrace.TracerProvider, error) {
func newTracerProvider(config Config, resource *sdkresource.Resource, creds credentials.TransportCredentials) (*sdktrace.TracerProvider, error) {
ctx := context.Background()

exporter, err := otlptracegrpc.New(ctx,
otlptracegrpc.WithInsecure(),
otlptracegrpc.WithTLSCredentials(creds),
otlptracegrpc.WithEndpoint(config.OtelExporterGRPCEndpoint),
)
if err != nil {
Expand All @@ -299,12 +309,12 @@ func newTracerProvider(config Config, resource *sdkresource.Resource) (*sdktrace
return tp, nil
}

func newMeterProvider(config Config, resource *sdkresource.Resource) (*sdkmetric.MeterProvider, error) {
func newMeterProvider(config Config, resource *sdkresource.Resource, creds credentials.TransportCredentials) (*sdkmetric.MeterProvider, error) {
ctx := context.Background()

exporter, err := otlpmetricgrpc.New(
ctx,
otlpmetricgrpc.WithInsecure(),
otlpmetricgrpc.WithTLSCredentials(creds),
otlpmetricgrpc.WithEndpoint(config.OtelExporterGRPCEndpoint),
)
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions pkg/beholder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
)

type Config struct {
Enabled bool
InsecureConnection bool
TLSCertFile string
OtelExporterGRPCEndpoint string
PackageName string

PackageName string
// OTel Resource
ResourceAttributes map[string]string
// EventEmitter
Expand All @@ -29,7 +31,8 @@ var defaultOtelAttributes = map[string]string{

func DefaultConfig() Config {
return Config{
Enabled: true,
InsecureConnection: true,
TLSCertFile: "",
OtelExporterGRPCEndpoint: "localhost:4317",
PackageName: "beholder",
// Resource
Expand Down
5 changes: 3 additions & 2 deletions pkg/beholder/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const (

func ExampleConfig() {
config := beholder.Config{
Enabled: true,
InsecureConnection: true,
TLSCertFile: "",
OtelExporterGRPCEndpoint: "localhost:4317",
PackageName: packageName,
// Resource
Expand All @@ -33,5 +34,5 @@ func ExampleConfig() {
}
fmt.Printf("%+v", config)
// Output:
// {Enabled:true OtelExporterGRPCEndpoint:localhost:4317 PackageName:beholder ResourceAttributes:map[package_name:beholder sender:beholdeclient] EmitterExportTimeout:1s TraceSampleRate:1 TraceBatchTimeout:1s MetricReaderInterval:1s LogExportTimeout:1s}
// {InsecureConnection:true TLSCertFile: OtelExporterGRPCEndpoint:localhost:4317 PackageName:beholder ResourceAttributes:map[package_name:beholder sender:beholdeclient] EmitterExportTimeout:1s TraceSampleRate:1 TraceBatchTimeout:1s MetricReaderInterval:1s LogExportTimeout:1s}
}

0 comments on commit 0bdc6d2

Please sign in to comment.