-
Notifications
You must be signed in to change notification settings - Fork 232
refactor: improve telemetry api receiver listener initialization logic #2096
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
Open
herin049
wants to merge
1
commit into
open-telemetry:main
Choose a base branch
from
herin049:refactor/improve-telemetryreceiver-listener
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+43
−26
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -18,11 +18,12 @@ import ( | |
| "context" | ||
| crand "crypto/rand" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
@@ -75,30 +76,52 @@ type telemetryAPIReceiver struct { | |
| logReport bool | ||
| } | ||
|
|
||
| func (r *telemetryAPIReceiver) Start(ctx context.Context, host component.Host) error { | ||
| address := listenOnAddress(r.port) | ||
| r.logger.Info("Listening for requests", zap.String("address", address)) | ||
| func (r *telemetryAPIReceiver) Start(ctx context.Context, _ component.Host) error { | ||
| if len(r.types) == 0 { | ||
| return fmt.Errorf("no telemetry event types provided") | ||
| } | ||
| listener, address, err := r.bindListener(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to bind listener: %w", err) | ||
| } | ||
| r.logger.Info("Starting telemetry API listener", zap.String("address", address)) | ||
|
|
||
| mux := http.NewServeMux() | ||
| mux.HandleFunc("/", r.httpHandler) | ||
| r.httpServer = &http.Server{Addr: address, Handler: mux} | ||
| go func() { | ||
| _ = r.httpServer.ListenAndServe() | ||
| err := r.httpServer.Serve(listener) | ||
| if !errors.Is(err, http.ErrServerClosed) { | ||
| r.logger.Error("Telemetry API server stopped unexpectedly", zap.Error(err)) | ||
| } else { | ||
| r.logger.Info("Telemetry API server stopped", zap.String("address", address)) | ||
| } | ||
| }() | ||
|
|
||
| telemetryClient := telemetryapi.NewClient(r.logger) | ||
| if len(r.types) > 0 { | ||
| _, err := telemetryClient.Subscribe(ctx, r.types, r.extensionID, fmt.Sprintf("http://%s/", address)) | ||
| if err != nil { | ||
| r.logger.Info("Listening for requests", zap.String("address", address), zap.String("extensionID", r.extensionID)) | ||
| return err | ||
| } | ||
| if _, err := telemetryClient.Subscribe(ctx, r.types, r.extensionID, fmt.Sprintf("http://%s/", address)); err != nil { | ||
| r.logger.Error("Failed to subscribe to telemetry", zap.Error(err)) | ||
| _ = r.Shutdown(ctx) | ||
| return err | ||
| } | ||
| r.logger.Info("Successfully subscribed to telemetry", zap.String("address", address)) | ||
| return nil | ||
| } | ||
|
|
||
| func (r *telemetryAPIReceiver) bindListener(ctx context.Context) (net.Listener, string, error) { | ||
| listenerAddr := listenOnAddress() | ||
| var lc net.ListenConfig | ||
| l, err := lc.Listen(ctx, "tcp", fmt.Sprintf("%s:%d", listenerAddr, r.port)) | ||
| if err != nil { | ||
| return nil, "", err | ||
| } | ||
| addr := fmt.Sprintf("%s:%d", l.Addr().Network(), l.Addr().(*net.TCPAddr).Port) | ||
| return l, addr, nil | ||
| } | ||
|
|
||
| func (r *telemetryAPIReceiver) Shutdown(ctx context.Context) error { | ||
| return nil | ||
| err := r.httpServer.Shutdown(ctx) | ||
| return err | ||
| } | ||
|
Comment on lines
122
to
125
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again looking at some "prior art" in internal/telemetryapi/listener.go it looks like just to be safe we might want to also nil check |
||
|
|
||
| func newSpanID() pcommon.SpanID { | ||
|
|
@@ -192,9 +215,6 @@ func (r *telemetryAPIReceiver) httpHandler(w http.ResponseWriter, req *http.Requ | |
| } | ||
| } | ||
| } | ||
|
|
||
| r.logger.Debug("logEvents received", zap.Int("count", len(slice)), zap.Int64("queue_length", r.queue.Len())) | ||
| slice = nil | ||
| } | ||
|
|
||
| func (r *telemetryAPIReceiver) getRecordRequestId(record map[string]interface{}) string { | ||
|
|
@@ -534,14 +554,11 @@ func newTelemetryAPIReceiver( | |
| }, nil | ||
| } | ||
|
|
||
| func listenOnAddress(port int) string { | ||
| func listenOnAddress() string { | ||
| envAwsLocal, ok := os.LookupEnv("AWS_SAM_LOCAL") | ||
| var addr string | ||
| if ok && envAwsLocal == "true" { | ||
| addr = ":" + strconv.Itoa(port) | ||
| return "" | ||
| } else { | ||
| addr = "sandbox.localdomain:" + strconv.Itoa(port) | ||
| return "sandbox.localdomain" | ||
| } | ||
|
|
||
| return addr | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like
l.Addr().Network()returns the network type string "tcp". So wouldn't this produce an address liketcp:53421instead ofsandbox.localdomain:53421?That would mean the subscribe URI passed to the Telemetry API ends up as http://tcp:53421/, which Lambda wouldn't be able to resolve to deliver events to I think?
For reference, the internal listener at internal/telemetryapi/listener.go uses the
listenerAddrvariable directly for this and I think a lot of the logic we already have there is directly applicable here as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, this must have been a typo, or I made this change very late at night. I can update this, and add a test, since I'm surprised this managed to slip through.