Skip to content

Commit 1a2e2b4

Browse files
committed
telemetry metrics OTLP GRPC Exporter: add CA certificate support
1 parent d39dd7a commit 1a2e2b4

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

service/telemetry/internal/otelinit/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
3030
"go.opentelemetry.io/otel/sdk/metric/metricdata"
3131
"go.opentelemetry.io/otel/sdk/resource"
32+
"google.golang.org/grpc/credentials"
3233

3334
semconv "go.opentelemetry.io/collector/semconv/v1.18.0"
3435
)
@@ -224,6 +225,14 @@ func initOTLPgRPCExporter(ctx context.Context, otlpConfig *config.OTLPMetric) (s
224225
opts = append(opts, otlpmetricgrpc.WithEndpoint(u.Host))
225226
if u.Scheme == "http" {
226227
opts = append(opts, otlpmetricgrpc.WithInsecure())
228+
} else {
229+
if otlpConfig.Certificate != nil {
230+
creds, err := credentials.NewClientTLSFromFile(*otlpConfig.Certificate, "")
231+
if err != nil {
232+
return nil, fmt.Errorf("could not create client tls credentials: %w", err)
233+
}
234+
opts = append(opts, otlpmetricgrpc.WithTLSCredentials(creds))
235+
}
227236
}
228237
}
229238

service/telemetry/internal/otelinit/config_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ package otelinit
66
import (
77
"context"
88
"errors"
9+
"fmt"
910
"net/url"
11+
"path/filepath"
1012
"reflect"
1113
"sync"
1214
"testing"
@@ -527,6 +529,46 @@ func TestMetricReader(t *testing.T) {
527529
},
528530
wantErr: errors.New("unsupported temporality preference \"invalid\""),
529531
},
532+
{
533+
name: "periodic/otlp-grpc-good-ca-certificate",
534+
reader: config.MetricReader{
535+
Periodic: &config.PeriodicMetricReader{
536+
Exporter: config.MetricExporter{
537+
OTLP: &config.OTLPMetric{
538+
Protocol: "grpc/protobuf",
539+
Endpoint: "https://localhost:4317",
540+
Compression: strPtr("gzip"),
541+
Timeout: intPtr(1000),
542+
Certificate: strPtr(filepath.Join("testdata", "ca.crt")),
543+
Headers: map[string]string{
544+
"test": "test1",
545+
},
546+
},
547+
},
548+
},
549+
},
550+
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter),
551+
},
552+
{
553+
name: "periodic/otlp-grpc-bad-ca-certificate",
554+
reader: config.MetricReader{
555+
Periodic: &config.PeriodicMetricReader{
556+
Exporter: config.MetricExporter{
557+
OTLP: &config.OTLPMetric{
558+
Protocol: "grpc/protobuf",
559+
Endpoint: "https://localhost:4317",
560+
Compression: strPtr("gzip"),
561+
Timeout: intPtr(1000),
562+
Certificate: strPtr(filepath.Join("testdata", "bad_cert.crt")),
563+
Headers: map[string]string{
564+
"test": "test1",
565+
},
566+
},
567+
},
568+
},
569+
},
570+
wantErr: fmt.Errorf("could not create client tls credentials: %w", errors.New("credentials: failed to append certificates")),
571+
},
530572
}
531573
for _, tt := range testCases {
532574
t.Run(tt.name, func(t *testing.T) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is intentionally not a PEM formatted cert file.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDNjCCAh4CCQC0I5IQT7eziDANBgkqhkiG9w0BAQsFADBdMQswCQYDVQQGEwJB
3+
VTESMBAGA1UECAwJQXVzdHJhbGlhMQ8wDQYDVQQHDAZTeWRuZXkxEjAQBgNVBAoM
4+
CU15T3JnTmFtZTEVMBMGA1UEAwwMTXlDb21tb25OYW1lMB4XDTIyMDgwMzA0MTky
5+
MVoXDTMyMDczMTA0MTkyMVowXTELMAkGA1UEBhMCQVUxEjAQBgNVBAgMCUF1c3Ry
6+
YWxpYTEPMA0GA1UEBwwGU3lkbmV5MRIwEAYDVQQKDAlNeU9yZ05hbWUxFTATBgNV
7+
BAMMDE15Q29tbW9uTmFtZTC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