Skip to content
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

cmd/telemetrygen: Add headers to gRPC metadata for logs #30082

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions cmd/telemetrygen/internal/logs/exporter.go
mx-psi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"go.opentelemetry.io/collector/pdata/plog/plogotlp"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)

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

if !cfg.Insecure {
return nil, fmt.Errorf("'telemetrygen logs' only supports insecure gRPC")
// TODO: Use an otlplogs grpc package in the future
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you file an issue for this, explaining why we can't do this yet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to find such a package. Does it exist?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not, I just want to have an issue in this repository tracking this work, since TODOs are often missed :) You can just say "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()))
}
// 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
Loading