Skip to content

Commit

Permalink
fix by comments
Browse files Browse the repository at this point in the history
Signed-off-by: LiZhenCheng9527 <[email protected]>
  • Loading branch information
LiZhenCheng9527 committed Sep 2, 2024
1 parent 72dfc95 commit 9eb6ad2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
15 changes: 7 additions & 8 deletions pkg/controller/telemetry/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,8 @@ func (m *MetricController) Run(ctx context.Context, mapOfTcpInfo *ebpf.Map) {
return
default:

Check warning on line 211 in pkg/controller/telemetry/metric.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/telemetry/metric.go#L206-L211

Added lines #L206 - L211 were not covered by tests
// Metrics updated every 3 seconds
time.Sleep(3 * time.Second)
time.Sleep(5 * time.Second)
m.updatePrometheusMetric()

Check warning on line 214 in pkg/controller/telemetry/metric.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/telemetry/metric.go#L213-L214

Added lines #L213 - L214 were not covered by tests
if err != nil {
log.Errorf("update Kmesh metrics failed: %v\n", err)
}
}
}
}()
Expand Down Expand Up @@ -265,8 +262,10 @@ func (m *MetricController) Run(ctx context.Context, mapOfTcpInfo *ebpf.Map) {
if data.state == TCP_CLOSTED {
OutputAccesslog(data, accesslog)
}
m.buildWorkloadMetricsToPrometheus(data, workloadLabels)
m.buildServiceMetricsToPrometheus(data, serviceLabels)
m.mutex.RLock()
m.updateWorkloadMetricCache(data, workloadLabels)
m.updateServiceMetricCache(data, serviceLabels)
m.mutex.RUnlock()

Check warning on line 268 in pkg/controller/telemetry/metric.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/telemetry/metric.go#L265-L268

Added lines #L265 - L268 were not covered by tests
}
}
}
Expand Down Expand Up @@ -463,7 +462,7 @@ func buildPrincipal(workload *workloadapi.Workload) string {
return "-"
}

func (m *MetricController) buildWorkloadMetricsToPrometheus(data requestMetric, labels workloadMetricLabels) {
func (m *MetricController) updateWorkloadMetricCache(data requestMetric, labels workloadMetricLabels) {
v, ok := m.workloadMetricCache[labels]
if ok {
if data.state == TCP_ESTABLISHED {
Expand Down Expand Up @@ -494,7 +493,7 @@ func (m *MetricController) buildWorkloadMetricsToPrometheus(data requestMetric,
}
}

func (m *MetricController) buildServiceMetricsToPrometheus(data requestMetric, labels serviceMetricLabels) {
func (m *MetricController) updateServiceMetricCache(data requestMetric, labels serviceMetricLabels) {
v, ok := m.serviceMetricCache[labels]
if ok {
if data.state == TCP_ESTABLISHED {
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/telemetry/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func TestBuildMetricsToPrometheus(t *testing.T) {
workloadMetricCache: map[workloadMetricLabels]workloadMetricInfo{},
serviceMetricCache: map[serviceMetricLabels]serviceMetricInfo{},
}
m.buildWorkloadMetricsToPrometheus(tt.args.data, tt.args.labels)
m.updateWorkloadMetricCache(tt.args.data, tt.args.labels)
assert.Equal(t, m.workloadMetricCache[tt.args.labels].WorkloadConnClosed, tt.want[0])
assert.Equal(t, m.workloadMetricCache[tt.args.labels].WorkloadConnOpened, tt.want[1])
assert.Equal(t, m.workloadMetricCache[tt.args.labels].WorkloadConnReceivedBytes, tt.want[2])
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestBuildServiceMetricsToPrometheus(t *testing.T) {
workloadMetricCache: map[workloadMetricLabels]workloadMetricInfo{},
serviceMetricCache: map[serviceMetricLabels]serviceMetricInfo{},
}
m.buildServiceMetricsToPrometheus(tt.args.data, tt.args.labels)
m.updateServiceMetricCache(tt.args.data, tt.args.labels)
assert.Equal(t, m.serviceMetricCache[tt.args.labels].ServiceConnClosed, tt.want[0])
assert.Equal(t, m.serviceMetricCache[tt.args.labels].ServiceConnOpened, tt.want[1])
assert.Equal(t, m.serviceMetricCache[tt.args.labels].ServiceConnReceivedBytes, tt.want[2])
Expand Down

0 comments on commit 9eb6ad2

Please sign in to comment.