Skip to content

Commit

Permalink
Merge pull request #732 from LiZhenCheng9527/accesslog
Browse files Browse the repository at this point in the history
add accesslog to enhance obversiblity of kmesh
  • Loading branch information
kmesh-bot authored Aug 29, 2024
2 parents 581231d + 45ba817 commit c843b66
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 19 deletions.
4 changes: 2 additions & 2 deletions bpf/kmesh/probes/tcp_probe.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ struct tcp_probe_info {
__u32 received_bytes;
__u32 conn_success;
__u32 direction;
__u32 state; /* tcp state */
__u32 protocol;
__u64 duration; // ns
__u64 close_ns;
__u32 state; /* tcp state */
__u32 protocol;
__u32 srtt_us; /* smoothed round trip time << 3 in usecs */
__u32 rtt_min;
__u32 mss_cache; /* Cached effective mss, not including SACKS */
Expand Down
74 changes: 74 additions & 0 deletions pkg/controller/telemetry/accesslog.go
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
}
81 changes: 81 additions & 0 deletions pkg/controller/telemetry/accesslog_test.go
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)
}
Loading

0 comments on commit c843b66

Please sign in to comment.