Skip to content

Commit dca08e8

Browse files
committed
Patch listener.go to use random port
1 parent f6d6301 commit dca08e8

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

listener.patch

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
diff --git a/collector/internal/telemetryapi/listener.go b/collector/internal/telemetryapi/listener.go
2+
index 8499d4e..93ff0dc 100644
3+
--- a/collector/internal/telemetryapi/listener.go
4+
+++ b/collector/internal/telemetryapi/listener.go
5+
@@ -17,18 +17,32 @@ package telemetryapi
6+
import (
7+
"context"
8+
"encoding/json"
9+
+ "errors"
10+
"fmt"
11+
"io"
12+
+ "math/rand"
13+
+ "net"
14+
"net/http"
15+
"os"
16+
+ "syscall"
17+
"time"
18+
19+
"github.com/golang-collections/go-datastructures/queue"
20+
"go.uber.org/zap"
21+
)
22+
23+
-const defaultListenerPort = "53612"
24+
-const initialQueueSize = 5
25+
+const (
26+
+ initialQueueSize = 5
27+
+ maxRetries = 5
28+
+ // Define ephemeral port range (typical range is 49152-65535)
29+
+ minPort = 49152
30+
+ maxPort = 65535
31+
+)
32+
+
33+
+// getRandomPort returns a random port number within the ephemeral range
34+
+func getRandomPort() string {
35+
+ return fmt.Sprintf("%d", rand.Intn(maxPort-minPort)+minPort)
36+
+}
37+
38+
// Listener is used to listen to the Telemetry API
39+
type Listener struct {
40+
@@ -46,21 +60,44 @@ func NewListener(logger *zap.Logger) *Listener {
41+
}
42+
}
43+
44+
-func listenOnAddress() string {
45+
+func (s *Listener) tryBindPort() (string, error) {
46+
+ for i := 0; i < maxRetries; i++ {
47+
+ port := getRandomPort()
48+
+ address := listenOnAddress(port)
49+
+
50+
+ l, err := net.Listen("tcp", address)
51+
+ if err != nil {
52+
+ if errors.Is(err, syscall.EADDRINUSE) {
53+
+ s.logger.Debug("Port in use, trying another",
54+
+ zap.String("address", address))
55+
+ continue
56+
+ }
57+
+ return "", err
58+
+ }
59+
+ l.Close()
60+
+ return address, nil
61+
+ }
62+
+
63+
+ return "", fmt.Errorf("failed to find available port after %d attempts", maxRetries)
64+
+}
65+
+
66+
+func listenOnAddress(port string) string {
67+
envAwsLocal, ok := os.LookupEnv("AWS_SAM_LOCAL")
68+
var addr string
69+
if ok && envAwsLocal == "true" {
70+
- addr = ":" + defaultListenerPort
71+
+ addr = ":" + port
72+
} else {
73+
- addr = "sandbox.localdomain:" + defaultListenerPort
74+
+ addr = "sandbox.localdomain:" + port
75+
}
76+
-
77+
return addr
78+
}
79+
80+
// Start the server in a goroutine where the log events will be sent
81+
func (s *Listener) Start() (string, error) {
82+
- address := listenOnAddress()
83+
+ address, err := s.tryBindPort()
84+
+ if err != nil {
85+
+ return "", fmt.Errorf("failed to find available port: %w", err)
86+
+ }
87+
s.logger.Info("Listening for requests", zap.String("address", address))
88+
s.httpServer = &http.Server{Addr: address}
89+
http.HandleFunc("/", s.httpHandler)

patch-upstream.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ patch -p2 < ../../collector.patch
4242
# patch manager.go to remove lambdacomponents attribute
4343
patch -p2 < ../../manager.patch
4444

45+
46+
# patch listener.go to patch port usage
47+
patch -p2 < ../../listener.patch
48+
4549
# Replace OTel Collector with ADOT Collector
4650
go mod edit -replace github.com/open-telemetry/opentelemetry-lambda/collector/lambdacomponents=${CURRENT_DIR}/adot/collector/lambdacomponents
4751

0 commit comments

Comments
 (0)