-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/telemetrygen: add HTTP export for logs (#29078)
**Description:** Closes #18867 **Testing:** Ran opentelemetry-collector locally with debug exporter, then used telemetrygen with `--otlp-http` with and without `--otlp-insecure`. **Documentation:** None
- Loading branch information
Showing
5 changed files
with
127 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: cmd/telemetrygen | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add support for --otlp-http for telemetrygen logs | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [18867] | ||
|
||
# (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: [user] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package logs | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"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/insecure" | ||
) | ||
|
||
type exporter interface { | ||
export(plog.Logs) error | ||
} | ||
|
||
func newExporter(ctx context.Context, cfg *Config) (exporter, error) { | ||
if cfg.UseHTTP { | ||
return &httpClientExporter{ | ||
client: http.DefaultClient, | ||
cfg: cfg, | ||
}, nil | ||
} | ||
|
||
if !cfg.Insecure { | ||
return nil, fmt.Errorf("'telemetrygen logs' only supports insecure gRPC") | ||
} | ||
// only support grpc in insecure mode | ||
clientConn, err := grpc.DialContext(ctx, cfg.Endpoint(), grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &gRPCClientExporter{client: plogotlp.NewGRPCClient(clientConn)}, nil | ||
} | ||
|
||
type gRPCClientExporter struct { | ||
client plogotlp.GRPCClient | ||
} | ||
|
||
func (e *gRPCClientExporter) export(logs plog.Logs) error { | ||
req := plogotlp.NewExportRequestFromLogs(logs) | ||
if _, err := e.client.Export(context.Background(), req); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
type httpClientExporter struct { | ||
client *http.Client | ||
cfg *Config | ||
} | ||
|
||
func (e *httpClientExporter) export(logs plog.Logs) error { | ||
scheme := "https" | ||
if e.cfg.Insecure { | ||
scheme = "http" | ||
} | ||
path := e.cfg.HTTPPath | ||
url := fmt.Sprintf("%s://%s%s", scheme, e.cfg.Endpoint(), path) | ||
|
||
req := plogotlp.NewExportRequestFromLogs(logs) | ||
body, err := req.MarshalProto() | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal logs to protobuf: %w", err) | ||
} | ||
|
||
httpReq, err := http.NewRequestWithContext(context.Background(), "POST", url, bytes.NewReader(body)) | ||
if err != nil { | ||
return fmt.Errorf("failed to create logs HTTP request: %w", err) | ||
} | ||
for k, v := range e.cfg.Headers { | ||
httpReq.Header.Set(k, v) | ||
} | ||
httpReq.Header.Set("Content-Type", "application/x-protobuf") | ||
resp, err := e.client.Do(httpReq) | ||
if err != nil { | ||
return fmt.Errorf("failed to execute logs HTTP request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
var respData bytes.Buffer | ||
_, _ = io.Copy(&respData, resp.Body) | ||
return fmt.Errorf("log request failed with status %s (%s)", resp.Status, respData.String()) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters