-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathport.go
More file actions
94 lines (79 loc) · 2.32 KB
/
Copy pathport.go
File metadata and controls
94 lines (79 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package extract
import (
"context"
"strconv"
"strings"
"github.com/aws/amazon-cloudwatch-agent/internal/detector"
"github.com/aws/amazon-cloudwatch-agent/internal/detector/util"
)
const (
portFlag = "-p"
portEnvVar = "PGPORT"
defaultPostgresPort = 5432
)
type portExtractor struct {
subExtractors []detector.PortExtractor
}
// NewPortExtractor creates a port extractor that attempts to find the PostgreSQL port
// from command line arguments (-p flag) or environment variables (PGPORT).
// Falls back to the default PostgreSQL port 5432.
func NewPortExtractor() detector.PortExtractor {
return &portExtractor{
subExtractors: []detector.PortExtractor{
&cmdlinePortExtractor{},
&envPortExtractor{},
},
}
}
func (e *portExtractor) Extract(ctx context.Context, process detector.Process) (int, error) {
for _, sub := range e.subExtractors {
port, err := sub.Extract(ctx, process)
if err == nil {
return port, nil
}
}
return defaultPostgresPort, nil
}
// cmdlinePortExtractor extracts port from -p flag
type cmdlinePortExtractor struct{}
func (e *cmdlinePortExtractor) Extract(ctx context.Context, process detector.Process) (int, error) {
args, err := process.CmdlineSliceWithContext(ctx)
if err != nil {
return 0, err
}
for i, arg := range args {
if arg == portFlag && i+1 < len(args) {
port, err := strconv.Atoi(args[i+1])
if err == nil && util.IsValidPort(port) {
return port, nil
}
}
if strings.HasPrefix(arg, portFlag) && len(arg) > len(portFlag) {
port, err := strconv.Atoi(arg[len(portFlag):])
if err == nil && util.IsValidPort(port) {
return port, nil
}
}
}
return 0, detector.ErrExtractPort
}
// envPortExtractor extracts port from PGPORT environment variable
type envPortExtractor struct{}
func (e *envPortExtractor) Extract(ctx context.Context, process detector.Process) (int, error) {
env, err := process.EnvironWithContext(ctx)
if err != nil {
return 0, err
}
for _, entry := range env {
parts := strings.SplitN(entry, "=", 2)
if len(parts) == 2 && parts[0] == portEnvVar {
port, err := strconv.Atoi(strings.TrimSpace(parts[1]))
if err == nil && util.IsValidPort(port) {
return port, nil
}
}
}
return 0, detector.ErrExtractPort
}