-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Document how to use OTLP exporters with self-signed certificates #6882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
75aa70a
abea74a
f33b175
100a26c
9b16b57
f99d373
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,12 @@ package otlploggrpc_test | |
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"errors" | ||
"os" | ||
|
||
"google.golang.org/grpc/credentials" | ||
|
||
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc" | ||
"go.opentelemetry.io/otel/log/global" | ||
|
@@ -31,3 +37,64 @@ func Example() { | |
// From here, the provider can be used by instrumentation to collect | ||
// telemetry. | ||
} | ||
|
||
func ExampleWithTLSCredentials() { | ||
xue20xi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ctx := context.Background() | ||
var grpcExpOpt []otlploggrpc.Option | ||
// the trusted certificate to use when verifying a server's TLS credentials | ||
xue20xi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
caFile := os.Getenv("OTEL_EXPORTER_OTLP_CERTIFICATE") | ||
// the filepath to the client certificate | ||
clientCert := os.Getenv("OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE") | ||
// the filepath to the client's private key | ||
clientKey := os.Getenv("OTEL_EXPORTER_OTLP_CLIENT_KEY") | ||
xue20xi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if caFile != "" && clientCert != "" && clientKey != "" { | ||
// mTLS connection | ||
tlsCfg := tls.Config{ | ||
InsecureSkipVerify: false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it needed if we add CA certificate to root CA pool? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use new cert pool instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sill, do we need to set Is this not the only thing that is needed? pool := x509.NewCertPool()
if !pool.AppendCertsFromPEM(data) {
panic("failed to add CA certificate to root CA pool")
}
tlsCfg := &tls.Config{
RootCAs: certs,
}
creds := credentials.NewTLS(tlsCfg) |
||
} | ||
// loads CA certificate | ||
pool, _ := x509.SystemCertPool() | ||
data, err := os.ReadFile(caFile) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if !pool.AppendCertsFromPEM(data) { | ||
panic(errors.New("failed to add CA certificate to root CA pool")) | ||
} | ||
tlsCfg.RootCAs = pool | ||
// load client cert and key | ||
keypair, err := tls.LoadX509KeyPair(clientCert, clientKey) | ||
if err != nil { | ||
panic(err) | ||
} | ||
tlsCfg.Certificates = []tls.Certificate{keypair} | ||
creds := credentials.NewTLS(&tlsCfg) | ||
option := otlploggrpc.WithTLSCredentials(creds) | ||
grpcExpOpt = append(grpcExpOpt, option) | ||
} else if caFile != "" { | ||
// TLS connection | ||
creds, err := credentials.NewClientTLSFromFile(caFile, "") | ||
if err != nil { | ||
panic(err) | ||
} | ||
option := otlploggrpc.WithTLSCredentials(creds) | ||
grpcExpOpt = append(grpcExpOpt, option) | ||
} | ||
exp, err := otlploggrpc.New(ctx, grpcExpOpt...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
processor := log.NewBatchProcessor(exp) | ||
provider := log.NewLoggerProvider(log.WithProcessor(processor)) | ||
defer func() { | ||
if err := provider.Shutdown(ctx); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
global.SetLoggerProvider(provider) | ||
|
||
// From here, the provider can be used by instrumentation to collect | ||
// telemetry. | ||
} |
Uh oh!
There was an error while loading. Please reload this page.