Skip to content

Commit 25af694

Browse files
committed
Add access log source
1 parent fc2e857 commit 25af694

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

internal/agent/m3/m3.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ func (m3 *M3App) uploadAccessLogM3(endpoint string, pid int, appName string) {
503503

504504
accessLogPath := ""
505505
accessLogFormat := ""
506+
accessLogSource := ""
506507

507508
searchToken := "$" + appName
508509

@@ -518,6 +519,10 @@ func (m3 *M3App) uploadAccessLogM3(endpoint string, pid int, appName string) {
518519
}
519520
}
520521

522+
if accessLogPath == "" {
523+
return
524+
}
525+
521526
// Find the corresponding access log format
522527
for _, configAccessLogFmt := range config.GlobalConfig.AccessLogFormats {
523528
if !strings.Contains(string(configAccessLogFmt), "$") {
@@ -530,18 +535,27 @@ func (m3 *M3App) uploadAccessLogM3(endpoint string, pid int, appName string) {
530535
}
531536
}
532537

533-
if accessLogPath == "" {
534-
return
535-
}
536-
537538
if accessLogFormat == "" {
538539
logger.Log("WARNING: Access log format is not set for path:%s, app:%s, ignoring", accessLogPath, appName)
539540
return
540541
}
541542

543+
// Find the corresponding access log source
544+
for _, configAccessLogSrc := range config.GlobalConfig.AccessLogSources {
545+
if !strings.Contains(string(configAccessLogSrc), "$") {
546+
continue
547+
}
548+
549+
beforeSearchToken, found := strings.CutSuffix(string(configAccessLogSrc), searchToken)
550+
if found {
551+
accessLogSource = beforeSearchToken
552+
}
553+
}
554+
542555
accessLogEntries = append(accessLogEntries, capture.AccessLogEntry{
543556
Path: config.AccessLogPath(accessLogPath),
544557
Format: config.AccessLogFormat(accessLogFormat),
558+
Source: config.AccessLogSource(accessLogSource),
545559
})
546560

547561
paths := make(map[int][]capture.AccessLogEntry)

internal/capture/accesslogm3.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type AccessLogM3 struct {
3030
type AccessLogEntry struct {
3131
Path config.AccessLogPath
3232
Format config.AccessLogFormat
33+
Source config.AccessLogSource
3334
}
3435

3536
// accessLogM3ReadStat tracks the essential state needed for incremental log reading.
@@ -90,7 +91,7 @@ func (a *AccessLogM3) Run() (Result, error) {
9091
}
9192

9293
for _, match := range matches {
93-
r, e := a.CaptureSingleAccessLog(match, pid, string(path.Format))
94+
r, e := a.CaptureSingleAccessLog(match, pid, string(path.Format), string(path.Source))
9495

9596
results = append(results, r)
9697
errs = append(errs, e)
@@ -104,7 +105,7 @@ func (a *AccessLogM3) Run() (Result, error) {
104105
// captureSingleAccessLog processes a single access log file: it opens the file,
105106
// seeks to the last-read position (or initializes it on the first run),
106107
// copies new content to a uniquely named destination file, and posts it.
107-
func (a *AccessLogM3) CaptureSingleAccessLog(filePath string, pid int, format string) (Result, error) {
108+
func (a *AccessLogM3) CaptureSingleAccessLog(filePath string, pid int, format, source string) (Result, error) {
108109
fileInfo, err := os.Stat(filePath)
109110
if err != nil {
110111
return Result{}, fmt.Errorf("failed to stat accesslog %q: %w", filePath, err)
@@ -166,6 +167,11 @@ func (a *AccessLogM3) CaptureSingleAccessLog(filePath string, pid int, format st
166167
// Write the format header to the destination file)
167168
dst.WriteString(format + "\n")
168169

170+
if source != "" {
171+
// Write the source header
172+
dst.WriteString("accessLogSource: " + source + "\n")
173+
}
174+
169175
// Copy new content from the source log.
170176
bytesCopied, err := io.Copy(dst, src)
171177
if err != nil {

internal/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ type Options struct {
100100
// Access log
101101
AccessLogs AccessLogs `yaml:"accessLogs" usage:"Access log file paths"`
102102
AccessLogFormats AccessLogFormats `yaml:"accessLogFormats" usage:"Access log formats corresponding to access log files"`
103+
AccessLogSources AccessLogSources `yaml:"accessLogSources" usage:"Access log sources corresponding to access log files"`
103104
}
104105

105106
type Command struct {
@@ -216,6 +217,9 @@ type AccessLogs []AccessLogPath
216217
type AccessLogFormat string
217218
type AccessLogFormats []AccessLogFormat
218219

220+
type AccessLogSource string
221+
type AccessLogSources []AccessLogSource
222+
219223
func defaultConfig() Config {
220224
return Config{
221225
Options: Options{

0 commit comments

Comments
 (0)