-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathpostgresql.go
More file actions
71 lines (55 loc) · 1.81 KB
/
Copy pathpostgresql.go
File metadata and controls
71 lines (55 loc) · 1.81 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package postgresql
import (
"context"
"log/slog"
"strings"
"github.com/aws/amazon-cloudwatch-agent/internal/detector"
"github.com/aws/amazon-cloudwatch-agent/internal/detector/postgresql/extract"
"github.com/aws/amazon-cloudwatch-agent/internal/detector/util"
)
const (
exeName = "postgres"
)
type postgresqlDetector struct {
logger *slog.Logger
portExtractor detector.PortExtractor
}
var _ detector.ProcessDetector = (*postgresqlDetector)(nil)
// NewDetector creates a new process detector that identifies PostgreSQL processes.
func NewDetector(logger *slog.Logger) detector.ProcessDetector {
return &postgresqlDetector{
logger: logger,
portExtractor: extract.NewPortExtractor(),
}
}
// Detect identifies PostgreSQL processes and returns metadata.
// Only detects the main postgres process, not worker processes.
func (d *postgresqlDetector) Detect(ctx context.Context, process detector.Process) (*detector.Metadata, error) {
exe, err := process.ExeWithContext(ctx)
if err != nil {
return nil, err
}
base := util.BaseExe(exe)
if base != exeName {
return nil, detector.ErrIncompatibleDetector
}
// Check if this is the main postgres process or a worker
args, err := process.CmdlineSliceWithContext(ctx)
if err != nil {
return nil, err
}
if len(args) > 0 && strings.HasPrefix(strings.TrimSpace(args[0]), exeName+":") {
return nil, detector.ErrIncompatibleDetector
}
d.logger.Debug("PostgreSQL process detected", "pid", process.PID())
md := &detector.Metadata{
Name: "postgresql",
Categories: []detector.Category{detector.CategoryPostgreSQL},
}
port, _ := d.portExtractor.Extract(ctx, process)
md.Status = detector.StatusReady
md.TelemetryPort = port
return md, nil
}