Skip to content

Commit

Permalink
cmd/telemetrygen: Add headers to gRPC metadata for logs
Browse files Browse the repository at this point in the history
  • Loading branch information
metalmatze committed Mar 28, 2024
1 parent 66a8793 commit f6308ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
2 changes: 2 additions & 0 deletions cmd/telemetrygen/internal/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Config struct {
// OTLP config
CustomEndpoint string
Insecure bool
InsecureSkipVerify bool
UseHTTP bool
HTTPPath string
Headers KeyValue
Expand Down Expand Up @@ -108,6 +109,7 @@ func (c *Config) CommonFlags(fs *pflag.FlagSet) {

fs.StringVar(&c.CustomEndpoint, "otlp-endpoint", "", "Destination endpoint for exporting logs, metrics and traces")
fs.BoolVar(&c.Insecure, "otlp-insecure", false, "Whether to enable client transport security for the exporter's grpc or http connection")
fs.BoolVar(&c.InsecureSkipVerify, "otlp-insecure-skip-verify", false, "Whether a client verifies the server's certificate chain and host name")
fs.BoolVar(&c.UseHTTP, "otlp-http", false, "Whether to use HTTP exporter rather than a gRPC one")

// custom headers
Expand Down
28 changes: 22 additions & 6 deletions cmd/telemetrygen/internal/logs/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ package logs
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net/http"

"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/plog/plogotlp"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)

type exporter interface {
Expand All @@ -28,24 +31,37 @@ func newExporter(ctx context.Context, cfg *Config) (exporter, error) {
}, nil
}

if !cfg.Insecure {
return nil, fmt.Errorf("'telemetrygen logs' only supports insecure gRPC")
// TODO: We should switch to opentelemetry-go logs exporter once available, see open-telemetry/opentelemetry-go#4696 for the current state
var dialOptions []grpc.DialOption
if cfg.Insecure {
dialOptions = append(dialOptions, grpc.WithTransportCredentials(insecure.NewCredentials()))
} else {
config := &tls.Config{
InsecureSkipVerify: cfg.InsecureSkipVerify,
}
dialOptions = append(dialOptions, grpc.WithTransportCredentials(credentials.NewTLS(config)))
}
// only support grpc in insecure mode
clientConn, err := grpc.DialContext(ctx, cfg.Endpoint(), grpc.WithTransportCredentials(insecure.NewCredentials()))

clientConn, err := grpc.DialContext(ctx, cfg.Endpoint(), dialOptions...)
if err != nil {
return nil, err
}
return &gRPCClientExporter{client: plogotlp.NewGRPCClient(clientConn)}, nil
return &gRPCClientExporter{client: plogotlp.NewGRPCClient(clientConn), cfg: cfg}, nil
}

type gRPCClientExporter struct {
client plogotlp.GRPCClient
cfg *Config
}

func (e *gRPCClientExporter) export(logs plog.Logs) error {
md := metadata.New(map[string]string{})
for k, v := range e.cfg.Headers {
md.Set(k, v)
}
ctx := metadata.NewOutgoingContext(context.Background(), md)
req := plogotlp.NewExportRequestFromLogs(logs)
if _, err := e.client.Export(context.Background(), req); err != nil {
if _, err := e.client.Export(ctx, req); err != nil {
return err
}
return nil
Expand Down

0 comments on commit f6308ce

Please sign in to comment.