Skip to content

Commit d291a32

Browse files
metalmatzesongy23
andauthored
cmd/telemetrygen: Add headers to gRPC metadata for logs (#32668)
**Description:** <Describe what has changed.> So far, sending (HTTP) headers along with gRPC requests hasn't been possible. For that reason, sending bearer tokens for authentication didn't work either. This is fixed now. Updated version of #30082 cc @mx-psi --------- Co-authored-by: Yang Song <[email protected]>
1 parent b536522 commit d291a32

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: telemetrygen
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add headers to gRPC metadata for logs
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [32668]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

cmd/telemetrygen/internal/common/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Config struct {
5858
// OTLP config
5959
CustomEndpoint string
6060
Insecure bool
61+
InsecureSkipVerify bool
6162
UseHTTP bool
6263
HTTPPath string
6364
Headers KeyValue
@@ -120,6 +121,7 @@ func (c *Config) CommonFlags(fs *pflag.FlagSet) {
120121

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

125127
// custom headers

cmd/telemetrygen/internal/logs/exporter.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"go.opentelemetry.io/collector/pdata/plog/plogotlp"
1515
"google.golang.org/grpc"
1616
"google.golang.org/grpc/credentials/insecure"
17+
"google.golang.org/grpc/metadata"
1718

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

2526
func newExporter(cfg *Config) (exporter, error) {
26-
27-
// Exporter with HTTP
2827
if cfg.UseHTTP {
2928
if cfg.Insecure {
3029
return &httpClientExporter{
@@ -60,16 +59,22 @@ func newExporter(cfg *Config) (exporter, error) {
6059
return nil, err
6160
}
6261
}
63-
return &gRPCClientExporter{client: plogotlp.NewGRPCClient(clientConn)}, nil
62+
return &gRPCClientExporter{client: plogotlp.NewGRPCClient(clientConn), cfg: cfg}, nil
6463
}
6564

6665
type gRPCClientExporter struct {
6766
client plogotlp.GRPCClient
67+
cfg *Config
6868
}
6969

7070
func (e *gRPCClientExporter) export(logs plog.Logs) error {
71+
md := metadata.New(map[string]string{})
72+
for k, v := range e.cfg.Headers {
73+
md.Set(k, v)
74+
}
75+
ctx := metadata.NewOutgoingContext(context.Background(), md)
7176
req := plogotlp.NewExportRequestFromLogs(logs)
72-
if _, err := e.client.Export(context.Background(), req); err != nil {
77+
if _, err := e.client.Export(ctx, req); err != nil {
7378
return err
7479
}
7580
return nil

0 commit comments

Comments
 (0)