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 Apr 24, 2024
1 parent 816b1f9 commit f528611
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
27 changes: 27 additions & 0 deletions .chloggen/telemetrygen-logs-grpc-metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: telemetrygen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add headers to gRPC metadata for logs

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: 32668

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
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 @@ -120,6 +121,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
13 changes: 9 additions & 4 deletions cmd/telemetrygen/internal/logs/exporter.go
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"

"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/internal/common"
)
Expand All @@ -23,8 +24,6 @@ type exporter interface {
}

func newExporter(cfg *Config) (exporter, error) {

// Exporter with HTTP
if cfg.UseHTTP {
if cfg.Insecure {
return &httpClientExporter{
Expand Down Expand Up @@ -60,16 +59,22 @@ func newExporter(cfg *Config) (exporter, error) {
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 f528611

Please sign in to comment.