-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #732 from LiZhenCheng9527/accesslog
add accesslog to enhance obversiblity of kmesh
- Loading branch information
Showing
5 changed files
with
211 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright The Kmesh Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package telemetry | ||
|
||
import ( | ||
"fmt" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
type logInfo struct { | ||
direction string | ||
sourceAddress string | ||
sourceWorkload string | ||
sourceNamespace string | ||
|
||
destinationAddress string | ||
destinationService string | ||
destinationWorkload string | ||
destinationNamespace string | ||
} | ||
|
||
func OutputAccesslog(data requestMetric, accesslog logInfo) { | ||
logStr := buildAccesslog(data, accesslog) | ||
fmt.Println("accesslog:", logStr) | ||
} | ||
|
||
func buildAccesslog(data requestMetric, accesslog logInfo) string { | ||
closeTime := data.closeTime | ||
uptime := calculateUptime(osStartTime, closeTime) | ||
|
||
timeInfo := fmt.Sprintf("%v", uptime) | ||
sourceInfo := fmt.Sprintf("src.addr=%s, src.workload=%s, src.namespace=%s", accesslog.sourceAddress, accesslog.sourceWorkload, accesslog.sourceNamespace) | ||
destinationInfo := fmt.Sprintf("dst.addr=%s, dst.service=%s, dst.workload=%s, dst.namespace=%s", accesslog.destinationAddress, accesslog.destinationService, accesslog.destinationWorkload, accesslog.destinationNamespace) | ||
connectionInfo := fmt.Sprintf("direction=%s, sent_bytes=%d, received_bytes=%d, duration=%vms", accesslog.direction, data.sentBytes, data.receivedBytes, (float64(data.duration) / 1000000.0)) | ||
|
||
logResult := fmt.Sprintf("%s %s, %s, %s", timeInfo, sourceInfo, destinationInfo, connectionInfo) | ||
return logResult | ||
} | ||
|
||
func getOSBootTime() (time.Time, error) { | ||
now := time.Now() | ||
now = now.Round(time.Duration(now.Second())) | ||
|
||
sysinfo := &syscall.Sysinfo_t{} | ||
if err := syscall.Sysinfo(sysinfo); err != nil { | ||
return time.Time{}, err | ||
} | ||
|
||
uptime := time.Duration(sysinfo.Uptime) * time.Second | ||
lastRebootTime := now.Add(-uptime) | ||
|
||
return lastRebootTime, nil | ||
} | ||
|
||
func calculateUptime(startTime time.Time, elapsedTimeNs uint64) time.Time { | ||
elapsedDuration := time.Duration(elapsedTimeNs) * time.Nanosecond | ||
currentTime := startTime.Add(elapsedDuration) | ||
return currentTime | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright The Kmesh Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package telemetry | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_buildAccesslog(t *testing.T) { | ||
type args struct { | ||
data requestMetric | ||
accesslog logInfo | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{ | ||
name: "build accesslog", | ||
args: args{ | ||
data: requestMetric{ | ||
sentBytes: uint32(60), | ||
receivedBytes: uint32(172), | ||
duration: uint64(2236000), | ||
closeTime: uint64(3506247005837715), | ||
}, | ||
accesslog: logInfo{ | ||
direction: "INBOUND", | ||
sourceAddress: "10.244.0.10:47667", | ||
sourceWorkload: "sleep-7656cf8794-9v2gv", | ||
sourceNamespace: "kmesh-system", | ||
destinationAddress: "10.244.0.7:8080", | ||
destinationService: "httpbin.ambient-demo.svc.cluster.local", | ||
destinationWorkload: "httpbin-86b8ffc5ff-bhvxx", | ||
destinationNamespace: "kmesh-system", | ||
}, | ||
}, | ||
want: "2024-08-14 10:11:27.005837715 +0000 UTC src.addr=10.244.0.10:47667, src.workload=sleep-7656cf8794-9v2gv, src.namespace=kmesh-system, dst.addr=10.244.0.7:8080, dst.service=httpbin.ambient-demo.svc.cluster.local, dst.workload=httpbin-86b8ffc5ff-bhvxx, dst.namespace=kmesh-system, direction=INBOUND, sent_bytes=60, received_bytes=172, duration=2.236ms", | ||
}, | ||
} | ||
osStartTime = time.Date(2024, 7, 4, 20, 14, 0, 0, time.UTC) | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := buildAccesslog(tt.args.data, tt.args.accesslog) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func Test_getOSBootTime(t *testing.T) { | ||
t.Run("function test", func(t *testing.T) { | ||
_, err := getOSBootTime() | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
|
||
func Test_calculateUptime(t *testing.T) { | ||
startTime := time.Date(2024, 7, 4, 20, 42, 0, 0, time.UTC) | ||
elapsedTimeNs := uint64(3506247005837715) | ||
want := time.Date(2024, 8, 14, 10, 39, 27, 5837715, time.UTC) | ||
uptime := calculateUptime(startTime, elapsedTimeNs) | ||
assert.Equal(t, want, uptime) | ||
} |
Oops, something went wrong.