Skip to content

Commit

Permalink
add support for http export as well
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Boten <[email protected]>
  • Loading branch information
codeboten authored and mattsains committed Dec 2, 2024
1 parent 95ad7c5 commit e388ed9
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 22 deletions.
20 changes: 20 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ package config // import "go.opentelemetry.io/contrib/config"

import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"os"

"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -155,3 +158,20 @@ func toStringMap(pairs []NameStringValuePair) map[string]string {
}
return output
}

// createTLSConfig creates a tls.Config from a raw certificate bytes
// to verify a server certificate.
func createTLSConfig(certFile string) (*tls.Config, error) {
b, err := os.ReadFile(certFile)
if err != nil {
return nil, err
}
cp := x509.NewCertPool()
if ok := cp.AppendCertsFromPEM(b); !ok {
return nil, errors.New("failed to append certificate to the cert pool")
}

return &tls.Config{
RootCAs: cp,
}, nil
}
23 changes: 16 additions & 7 deletions config/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ func otlpHTTPLogExporter(ctx context.Context, otlpConfig *OTLP) (sdklog.Exporter
opts = append(opts, otlploghttp.WithHeaders(toStringMap(otlpConfig.Headers)))
}

if otlpConfig.Certificate != nil {
creds, err := createTLSConfig(*otlpConfig.Certificate)
if err != nil {
return nil, fmt.Errorf("could not create client tls credentials: %w", err)
}
opts = append(opts, otlploghttp.WithTLSClientConfig(creds))
}

return otlploghttp.New(ctx, opts...)
}

Expand All @@ -180,13 +188,6 @@ func otlpGRPCLogExporter(ctx context.Context, otlpConfig *OTLP) (sdklog.Exporter
if u.Scheme == "http" {
opts = append(opts, otlploggrpc.WithInsecure())
}
if otlpConfig.Certificate != nil {
creds, err := credentials.NewClientTLSFromFile(*otlpConfig.Certificate, "")
if err != nil {
return nil, fmt.Errorf("could not create client tls credentials: %w", err)
}
opts = append(opts, otlploggrpc.WithTLSCredentials(creds))
}
}
if otlpConfig.Compression != nil {
switch *otlpConfig.Compression {
Expand All @@ -205,5 +206,13 @@ func otlpGRPCLogExporter(ctx context.Context, otlpConfig *OTLP) (sdklog.Exporter
opts = append(opts, otlploggrpc.WithHeaders(toStringMap(otlpConfig.Headers)))
}

if otlpConfig.Certificate != nil {
creds, err := credentials.NewClientTLSFromFile(*otlpConfig.Certificate, "")
if err != nil {
return nil, fmt.Errorf("could not create client tls credentials: %w", err)
}
opts = append(opts, otlploggrpc.WithTLSCredentials(creds))
}

return otlploggrpc.New(ctx, opts...)
}
34 changes: 34 additions & 0 deletions config/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,40 @@ func TestLogProcessor(t *testing.T) {
},
wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter),
},
{
name: "batch/otlp-http-good-ca-certificate",
processor: LogRecordProcessor{
Batch: &BatchLogRecordProcessor{
Exporter: LogRecordExporter{
OTLP: &OTLP{
Protocol: ptr("http/protobuf"),
Endpoint: ptr("localhost:4317"),
Compression: ptr("gzip"),
Timeout: ptr(1000),
Certificate: ptr(filepath.Join("testdata", "ca.crt")),
},
},
},
},
wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter),
},
{
name: "batch/otlp-http-bad-ca-certificate",
processor: LogRecordProcessor{
Batch: &BatchLogRecordProcessor{
Exporter: LogRecordExporter{
OTLP: &OTLP{
Protocol: ptr("http/protobuf"),
Endpoint: ptr("localhost:4317"),
Compression: ptr("gzip"),
Timeout: ptr(1000),
Certificate: ptr(filepath.Join("testdata", "bad_cert.crt")),
},
},
},
},
wantErr: fmt.Errorf("could not create client tls credentials: %w", errors.New("failed to append certificate to the cert pool")),
},
{
name: "batch/otlp-http-exporter-with-path",
processor: LogRecordProcessor{
Expand Down
23 changes: 16 additions & 7 deletions config/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ func otlpHTTPMetricExporter(ctx context.Context, otlpConfig *OTLPMetric) (sdkmet
}
}

if otlpConfig.Certificate != nil {
creds, err := createTLSConfig(*otlpConfig.Certificate)
if err != nil {
return nil, fmt.Errorf("could not create client tls credentials: %w", err)
}
opts = append(opts, otlpmetrichttp.WithTLSClientConfig(creds))
}

return otlpmetrichttp.New(ctx, opts...)
}

Expand All @@ -206,13 +214,6 @@ func otlpGRPCMetricExporter(ctx context.Context, otlpConfig *OTLPMetric) (sdkmet
if u.Scheme == "http" {
opts = append(opts, otlpmetricgrpc.WithInsecure())
}
if otlpConfig.Certificate != nil {
creds, err := credentials.NewClientTLSFromFile(*otlpConfig.Certificate, "")
if err != nil {
return nil, fmt.Errorf("could not create client tls credentials: %w", err)
}
opts = append(opts, otlpmetricgrpc.WithTLSCredentials(creds))
}
}

if otlpConfig.Compression != nil {
Expand Down Expand Up @@ -244,6 +245,14 @@ func otlpGRPCMetricExporter(ctx context.Context, otlpConfig *OTLPMetric) (sdkmet
}
}

if otlpConfig.Certificate != nil {
creds, err := credentials.NewClientTLSFromFile(*otlpConfig.Certificate, "")
if err != nil {
return nil, fmt.Errorf("could not create client tls credentials: %w", err)
}
opts = append(opts, otlpmetricgrpc.WithTLSCredentials(creds))
}

return otlpmetricgrpc.New(ctx, opts...)
}

Expand Down
34 changes: 34 additions & 0 deletions config/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,40 @@ func TestReader(t *testing.T) {
},
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter),
},
{
name: "periodic/otlp-http-good-ca-certificate",
reader: MetricReader{
Periodic: &PeriodicMetricReader{
Exporter: PushMetricExporter{
OTLP: &OTLPMetric{
Protocol: ptr("http/protobuf"),
Endpoint: ptr("https://localhost:4317"),
Compression: ptr("gzip"),
Timeout: ptr(1000),
Certificate: ptr(filepath.Join("testdata", "ca.crt")),
},
},
},
},
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter),
},
{
name: "periodic/otlp-http-bad-ca-certificate",
reader: MetricReader{
Periodic: &PeriodicMetricReader{
Exporter: PushMetricExporter{
OTLP: &OTLPMetric{
Protocol: ptr("http/protobuf"),
Endpoint: ptr("https://localhost:4317"),
Compression: ptr("gzip"),
Timeout: ptr(1000),
Certificate: ptr(filepath.Join("testdata", "bad_cert.crt")),
},
},
},
},
wantErr: fmt.Errorf("could not create client tls credentials: %w", errors.New("failed to append certificate to the cert pool")),
},
{
name: "periodic/otlp-http-exporter-with-path",
reader: MetricReader{
Expand Down
24 changes: 16 additions & 8 deletions config/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,6 @@ func otlpGRPCSpanExporter(ctx context.Context, otlpConfig *OTLP) (sdktrace.SpanE
if u.Scheme == "http" {
opts = append(opts, otlptracegrpc.WithInsecure())
}

if otlpConfig.Certificate != nil {
creds, err := credentials.NewClientTLSFromFile(*otlpConfig.Certificate, "")
if err != nil {
return nil, fmt.Errorf("could not create client tls credentials: %w", err)
}
opts = append(opts, otlptracegrpc.WithTLSCredentials(creds))
}
}

if otlpConfig.Compression != nil {
Expand All @@ -137,6 +129,14 @@ func otlpGRPCSpanExporter(ctx context.Context, otlpConfig *OTLP) (sdktrace.SpanE
opts = append(opts, otlptracegrpc.WithHeaders(toStringMap(otlpConfig.Headers)))
}

if otlpConfig.Certificate != nil {
creds, err := credentials.NewClientTLSFromFile(*otlpConfig.Certificate, "")
if err != nil {
return nil, fmt.Errorf("could not create client tls credentials: %w", err)
}
opts = append(opts, otlptracegrpc.WithTLSCredentials(creds))
}

return otlptracegrpc.New(ctx, opts...)
}

Expand Down Expand Up @@ -174,6 +174,14 @@ func otlpHTTPSpanExporter(ctx context.Context, otlpConfig *OTLP) (sdktrace.SpanE
opts = append(opts, otlptracehttp.WithHeaders(toStringMap(otlpConfig.Headers)))
}

if otlpConfig.Certificate != nil {
creds, err := createTLSConfig(*otlpConfig.Certificate)
if err != nil {
return nil, fmt.Errorf("could not create client tls credentials: %w", err)
}
opts = append(opts, otlptracehttp.WithTLSClientConfig(creds))
}

return otlptracehttp.New(ctx, opts...)
}

Expand Down
34 changes: 34 additions & 0 deletions config/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,40 @@ func TestSpanProcessor(t *testing.T) {
},
wantProcessor: sdktrace.NewBatchSpanProcessor(otlpHTTPExporter),
},
{
name: "batch/otlp-http-good-ca-certificate",
processor: SpanProcessor{
Batch: &BatchSpanProcessor{
Exporter: SpanExporter{
OTLP: &OTLP{
Protocol: ptr("http/protobuf"),
Endpoint: ptr("localhost:4317"),
Compression: ptr("gzip"),
Timeout: ptr(1000),
Certificate: ptr(filepath.Join("testdata", "ca.crt")),
},
},
},
},
wantProcessor: sdktrace.NewBatchSpanProcessor(otlpHTTPExporter),
},
{
name: "batch/otlp-http-bad-ca-certificate",
processor: SpanProcessor{
Batch: &BatchSpanProcessor{
Exporter: SpanExporter{
OTLP: &OTLP{
Protocol: ptr("http/protobuf"),
Endpoint: ptr("localhost:4317"),
Compression: ptr("gzip"),
Timeout: ptr(1000),
Certificate: ptr(filepath.Join("testdata", "bad_cert.crt")),
},
},
},
},
wantErr: fmt.Errorf("could not create client tls credentials: %w", errors.New("failed to append certificate to the cert pool")),
},
{
name: "batch/otlp-http-exporter-with-path",
processor: SpanProcessor{
Expand Down

0 comments on commit e388ed9

Please sign in to comment.