Skip to content

Commit ba03007

Browse files
Merge branch 'main' into fix/commonjs-in-node24-runtime-lambda-instrumentation
2 parents 4c18a20 + e5126b7 commit ba03007

File tree

8 files changed

+681
-40
lines changed

8 files changed

+681
-40
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ For more information about the maintainer role, see the [community repository](h
135135
### Approvers
136136

137137
- [Ivan Santos](https://github.com/pragmaticivan)
138+
- [Maxime David](https://github.com/maxday)
138139

139140
For more information about the approver role, see the [community repository](https://github.com/open-telemetry/community/blob/main/guides/contributor/membership.md#approver).
140141

collector/internal/telemetryapi/listener.go

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ package telemetryapi
1717
import (
1818
"context"
1919
"encoding/json"
20-
"errors"
2120
"fmt"
2221
"io"
23-
"math/rand"
2422
"net"
2523
"net/http"
2624
"os"
27-
"syscall"
2825
"time"
2926

3027
"github.com/golang-collections/go-datastructures/queue"
@@ -33,17 +30,8 @@ import (
3330

3431
const (
3532
initialQueueSize = 5
36-
maxRetries = 5
37-
// Define ephemeral port range (typical range is 49152-65535)
38-
minPort = 49152
39-
maxPort = 65535
4033
)
4134

42-
// getRandomPort returns a random port number within the ephemeral range
43-
func getRandomPort() string {
44-
return fmt.Sprintf("%d", rand.Intn(maxPort-minPort)+minPort)
45-
}
46-
4735
// Listener is used to listen to the Telemetry API
4836
type Listener struct {
4937
httpServer *http.Server
@@ -60,46 +48,39 @@ func NewListener(logger *zap.Logger) *Listener {
6048
}
6149
}
6250

63-
func (s *Listener) tryBindPort() (net.Listener, string, error) {
64-
for i := 0; i < maxRetries; i++ {
65-
port := getRandomPort()
66-
address := listenOnAddress(port)
67-
68-
l, err := net.Listen("tcp", address)
69-
if err != nil {
70-
if errors.Is(err, syscall.EADDRINUSE) {
71-
s.logger.Debug("Port in use, trying another",
72-
zap.String("address", address))
73-
continue
74-
}
75-
return nil, "", err
76-
}
77-
return l, address, nil
51+
func (s *Listener) bindListener() (net.Listener, string, error) {
52+
listenerAddr := listenOnAddress()
53+
l, err := net.Listen("tcp", listenerAddr+":0")
54+
if err != nil {
55+
return nil, "", err
7856
}
79-
80-
return nil, "", fmt.Errorf("failed to find available port after %d attempts", maxRetries)
57+
addr := fmt.Sprintf("%s:%d", listenerAddr, l.Addr().(*net.TCPAddr).Port)
58+
return l, addr, nil
8159
}
8260

83-
func listenOnAddress(port string) string {
61+
func listenOnAddress() string {
8462
envAwsLocal, ok := os.LookupEnv("AWS_SAM_LOCAL")
85-
var addr string
8663
if ok && envAwsLocal == "true" {
87-
addr = ":" + port
64+
return ""
8865
} else {
89-
addr = "sandbox.localdomain:" + port
66+
return "sandbox.localdomain"
9067
}
91-
return addr
9268
}
9369

9470
// Start the server in a goroutine where the log events will be sent
9571
func (s *Listener) Start() (string, error) {
96-
listener, address, err := s.tryBindPort()
72+
listener, address, err := s.bindListener()
9773
if err != nil {
9874
return "", fmt.Errorf("failed to find available port: %w", err)
9975
}
10076
s.logger.Info("Listening for requests", zap.String("address", address))
101-
s.httpServer = &http.Server{Addr: address}
102-
http.HandleFunc("/", s.httpHandler)
77+
mux := http.NewServeMux()
78+
s.httpServer = &http.Server{
79+
Addr: address,
80+
Handler: mux,
81+
}
82+
mux.HandleFunc("/", s.httpHandler)
83+
10384
go func() {
10485
err := s.httpServer.Serve(listener)
10586
if err != http.ErrServerClosed {

0 commit comments

Comments
 (0)