Skip to content

Commit 9e1e86c

Browse files
authored
Merge pull request #62 from reproio/handle-processing-time-properly
set TargetProcessingTime to -1
2 parents 77f7056 + eeeaa19 commit 9e1e86c

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: CI
22

33
on:
44
push:
5-
branches: [ master ]
5+
branches: [main]
66
pull_request:
7-
branches: [ master ]
7+
branches: [main]
88

99
jobs:
1010
test:

log_file_reader.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,13 @@ func (p *LogFileReader) Read(r io.Reader) (map[string]*Metric, error) {
5555
metric.RequestCountMap[r.Timestamp()] = RequestCount(1)
5656
}
5757

58-
// TargetProcessingTime is -1 when load balancer can't dispatch request to target or target doesn't respond until idle timeout.
58+
// Note: TargetProcessingTime is -1 when load balancer can't dispatch request to target or target doesn't respond until idle timeout.
5959
// see: https://docs.aws.amazon.com/ja_jp/elasticloadbalancing/latest/application/load-balancer-access-logs.html
60-
if r.TargetProcessingTime != -1 {
61-
if _, exist := metric.RequestCountMap[r.Timestamp()]; exist {
62-
metric.TargetProcessingTimesMap[r.Timestamp()] = append(metric.TargetProcessingTimesMap[r.Timestamp()], TargetProcessingTime(r.TargetProcessingTime))
63-
} else {
64-
metric.TargetProcessingTimesMap[r.Timestamp()] = TargetProcessingTimes{TargetProcessingTime(r.TargetProcessingTime)}
65-
}
60+
targetProcessingTime := TargetProcessingTime(r.TargetProcessingTime)
61+
if _, exist := metric.RequestCountMap[r.Timestamp()]; exist {
62+
metric.TargetProcessingTimesMap[r.Timestamp()] = append(metric.TargetProcessingTimesMap[r.Timestamp()], targetProcessingTime)
63+
} else {
64+
metric.TargetProcessingTimesMap[r.Timestamp()] = TargetProcessingTimes{targetProcessingTime}
6665
}
6766
metricMap[metricKey] = metric
6867
}

log_file_reader_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"strings"
7+
"testing"
8+
"time"
9+
)
10+
11+
func TestLogFileReader_Read(t *testing.T) {
12+
var pathRules []PathTransformingRule
13+
targetPaths := []string{"/"}
14+
logFileReader := NewLogFileReader(pathRules, targetPaths)
15+
16+
logTimeString := "2022-06-13T00:26:00.071316Z"
17+
logTime, err := time.Parse(time.RFC3339, "2022-06-13T00:26:00.071316Z")
18+
if err != nil {
19+
fmt.Printf("failed to parse %s: %s", logTimeString, err.Error())
20+
}
21+
22+
cases := []struct {
23+
log string
24+
wantTargetProcessingTimesMap map[Timestamp]TargetProcessingTimes
25+
wantTargetRequestCountMap map[Timestamp]RequestCount
26+
}{
27+
{
28+
log: fmt.Sprintf(`https %s app/my-loadbalancer/50dc6c495c0c9188 192.168.131.39:2817 10.0.0.1:80 0.086 0.048 0.037 200 200 0 57 "GET https://www.example.com:443/ HTTP/1.1" "curl/7.46.0" ECDHE-RSA-AES128-GCM-SHA256 TLSv1.2 arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067 "Root=1-58337281-1d84f3d73c47ec4e58577259" "www.example.com" "arn:aws:acm:us-east-2:123456789012:certificate/12345678-1234-1234-1234-123456789012" 1 2018-07-02T22:22:48.364000Z "authenticate,forward" "-" "-" "10.0.0.1:80" "200" "-" "-"`, logTimeString),
29+
wantTargetProcessingTimesMap: map[Timestamp]TargetProcessingTimes{
30+
Timestamp(logTime.Unix()): {0.048},
31+
},
32+
wantTargetRequestCountMap: map[Timestamp]RequestCount{
33+
Timestamp(logTime.Unix()): 1,
34+
},
35+
},
36+
{
37+
log: fmt.Sprintf(`https %s app/my-loadbalancer/50dc6c495c0c9188 192.168.131.39:2817 - -1 -1 -1 400 - 235 772 "GET https://www.example.com:443/ HTTP/1.1" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36" - - - "-" "-" "-" - 2022-06-13T00:25:59.856000Z "-" "-" "-" "-" "-" "-" "-"`, logTimeString),
38+
wantTargetProcessingTimesMap: map[Timestamp]TargetProcessingTimes{
39+
Timestamp(logTime.Unix()): {-1},
40+
},
41+
wantTargetRequestCountMap: map[Timestamp]RequestCount{
42+
Timestamp(logTime.Unix()): 1,
43+
},
44+
},
45+
}
46+
for _, tt := range cases {
47+
metricMap, err := logFileReader.Read(
48+
strings.NewReader(tt.log),
49+
)
50+
if err != nil {
51+
t.Fatalf("failed to read the log: %v\n%s", err, tt.log)
52+
}
53+
if len(metricMap) != 1 {
54+
t.Errorf("expected 1 metric, got %d", len(metricMap))
55+
}
56+
57+
for _, metric := range metricMap {
58+
if reflect.DeepEqual(metric.TargetProcessingTimesMap, tt.wantTargetProcessingTimesMap) == false {
59+
t.Errorf("unexpected got %v, want, %v", metric.TargetProcessingTimesMap, tt.wantTargetProcessingTimesMap)
60+
}
61+
if reflect.DeepEqual(metric.RequestCountMap, tt.wantTargetRequestCountMap) == false {
62+
t.Errorf("unexpected got %v, want, %v", metric.RequestCountMap, tt.wantTargetRequestCountMap)
63+
}
64+
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)