Skip to content

Commit

Permalink
Incorporating telemetry middleware into fault handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
mye956 committed Sep 30, 2024
1 parent 38e02f0 commit c58bb59
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 19 deletions.
90 changes: 81 additions & 9 deletions agent/handlers/task_server_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,43 +211,115 @@ func registerFaultHandlers(
// Setting up handler endpoints for network blackhole port fault injections
muxRouter.Handle(
fault.NetworkFaultPath(faulttype.BlackHolePortFaultType, faulttype.StartNetworkFaultPostfix),
tollbooth.LimitFuncHandler(createRateLimiter(), handler.StartNetworkBlackholePort()),
fault.TelemetryMiddleware(
tollbooth.LimitFuncHandler(
createRateLimiter(),
handler.StartNetworkBlackholePort(),
),
metricsFactory,
faulttype.StartNetworkFaultPostfix,
faulttype.BlackHolePortFaultType,
),
).Methods("POST")
muxRouter.Handle(
fault.NetworkFaultPath(faulttype.BlackHolePortFaultType, faulttype.StopNetworkFaultPostfix),
tollbooth.LimitFuncHandler(createRateLimiter(), handler.StopNetworkBlackHolePort()),
fault.TelemetryMiddleware(
tollbooth.LimitFuncHandler(
createRateLimiter(),
handler.StopNetworkBlackHolePort(),
),
metricsFactory,
faulttype.StopNetworkFaultPostfix,
faulttype.BlackHolePortFaultType,
),
).Methods("POST")
muxRouter.Handle(
fault.NetworkFaultPath(faulttype.BlackHolePortFaultType, faulttype.CheckNetworkFaultPostfix),
tollbooth.LimitFuncHandler(createRateLimiter(), handler.CheckNetworkBlackHolePort()),
fault.TelemetryMiddleware(
tollbooth.LimitFuncHandler(
createRateLimiter(),
handler.CheckNetworkBlackHolePort(),
),
metricsFactory,
faulttype.CheckNetworkFaultPostfix,
faulttype.BlackHolePortFaultType,
),
).Methods("POST")

// Setting up handler endpoints for network latency fault injections
muxRouter.Handle(
fault.NetworkFaultPath(faulttype.LatencyFaultType, faulttype.StartNetworkFaultPostfix),
tollbooth.LimitFuncHandler(createRateLimiter(), handler.StartNetworkLatency()),
fault.TelemetryMiddleware(
tollbooth.LimitFuncHandler(
createRateLimiter(),
handler.StartNetworkLatency(),
),
metricsFactory,
faulttype.StartNetworkFaultPostfix,
faulttype.LatencyFaultType,
),
).Methods("POST")
muxRouter.Handle(
fault.NetworkFaultPath(faulttype.LatencyFaultType, faulttype.StopNetworkFaultPostfix),
tollbooth.LimitFuncHandler(createRateLimiter(), handler.StopNetworkLatency()),
fault.TelemetryMiddleware(
tollbooth.LimitFuncHandler(
createRateLimiter(),
handler.StopNetworkLatency(),
),
metricsFactory,
faulttype.StopNetworkFaultPostfix,
faulttype.LatencyFaultType,
),
).Methods("POST")
muxRouter.Handle(
fault.NetworkFaultPath(faulttype.LatencyFaultType, faulttype.CheckNetworkFaultPostfix),
tollbooth.LimitFuncHandler(createRateLimiter(), handler.CheckNetworkLatency()),
fault.TelemetryMiddleware(
tollbooth.LimitFuncHandler(
createRateLimiter(),
handler.CheckNetworkLatency(),
),
metricsFactory,
faulttype.CheckNetworkFaultPostfix,
faulttype.LatencyFaultType,
),
).Methods("POST")

// Setting up handler endpoints for network packet loss fault injections
muxRouter.Handle(
fault.NetworkFaultPath(faulttype.PacketLossFaultType, faulttype.StartNetworkFaultPostfix),
tollbooth.LimitFuncHandler(createRateLimiter(), handler.StartNetworkPacketLoss()),
fault.TelemetryMiddleware(
tollbooth.LimitFuncHandler(
createRateLimiter(),
handler.StartNetworkPacketLoss(),
),
metricsFactory,
faulttype.StartNetworkFaultPostfix,
faulttype.PacketLossFaultType,
),
).Methods("POST")
muxRouter.Handle(
fault.NetworkFaultPath(faulttype.PacketLossFaultType, faulttype.StopNetworkFaultPostfix),
tollbooth.LimitFuncHandler(createRateLimiter(), handler.StopNetworkPacketLoss()),
fault.TelemetryMiddleware(
tollbooth.LimitFuncHandler(
createRateLimiter(),
handler.StopNetworkPacketLoss(),
),
metricsFactory,
faulttype.StopNetworkFaultPostfix,
faulttype.PacketLossFaultType,
),
).Methods("POST")
muxRouter.Handle(
fault.NetworkFaultPath(faulttype.PacketLossFaultType, faulttype.CheckNetworkFaultPostfix),
tollbooth.LimitFuncHandler(createRateLimiter(), handler.CheckNetworkPacketLoss()),
fault.TelemetryMiddleware(
tollbooth.LimitFuncHandler(
createRateLimiter(),
handler.CheckNetworkPacketLoss(),
),
metricsFactory,
faulttype.CheckNetworkFaultPostfix,
faulttype.PacketLossFaultType,
),
).Methods("POST")

seelog.Debug("Successfully set up Fault TMDS handlers")
Expand Down
28 changes: 18 additions & 10 deletions agent/handlers/task_server_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ const (
tcLatencyFaultExistsCommandOutput = `[{"kind":"netem","handle":"10:","parent":"1:1","options":{"limit":1000,"delay":{"delay":123456789,"jitter":4567,"correlation":0},"ecn":false,"gap":0}}]`
tcCommandEmptyOutput = `[]`
requestTimeoutDuration = 5 * time.Second
durationMetricPrefix = "MetadataServer.%s%sDuration"
)

var (
Expand Down Expand Up @@ -3804,7 +3805,7 @@ func TestRegisterStartBlackholePortFaultHandler(t *testing.T) {
)
}
tcs := generateCommonNetworkFaultInjectionTestCases("start blackhole port", "running", setExecExpectations, happyBlackHolePortReqBody)
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.BlackHolePortFaultType, faulttype.StartNetworkFaultPostfix))
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.BlackHolePortFaultType, faulttype.StartNetworkFaultPostfix), faulttype.StartNetworkFaultPostfix, faulttype.BlackHolePortFaultType)
}

func TestRegisterStopBlackholePortFaultHandler(t *testing.T) {
Expand All @@ -3824,7 +3825,7 @@ func TestRegisterStopBlackholePortFaultHandler(t *testing.T) {
)
}
tcs := generateCommonNetworkFaultInjectionTestCases("stop blackhole port", "stopped", setExecExpectations, happyBlackHolePortReqBody)
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.BlackHolePortFaultType, faulttype.StopNetworkFaultPostfix))
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.BlackHolePortFaultType, faulttype.StopNetworkFaultPostfix), faulttype.StopNetworkFaultPostfix, faulttype.BlackHolePortFaultType)
}

func TestRegisterCheckBlackholePortFaultHandler(t *testing.T) {
Expand All @@ -3838,7 +3839,7 @@ func TestRegisterCheckBlackholePortFaultHandler(t *testing.T) {
)
}
tcs := generateCommonNetworkFaultInjectionTestCases("check blackhole port", "running", setExecExpectations, happyBlackHolePortReqBody)
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.BlackHolePortFaultType, faulttype.CheckNetworkFaultPostfix))
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.BlackHolePortFaultType, faulttype.CheckNetworkFaultPostfix), faulttype.CheckNetworkFaultPostfix, faulttype.BlackHolePortFaultType)
}

func TestRegisterStartLatencyFaultHandler(t *testing.T) {
Expand All @@ -3854,7 +3855,7 @@ func TestRegisterStartLatencyFaultHandler(t *testing.T) {
mockCMD.EXPECT().CombinedOutput().Times(4).Return([]byte(tcCommandEmptyOutput), nil)
}
tcs := generateCommonNetworkFaultInjectionTestCases("start latency", "running", setExecExpectations, happyNetworkLatencyReqBody)
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.LatencyFaultType, faulttype.StartNetworkFaultPostfix))
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.LatencyFaultType, faulttype.StartNetworkFaultPostfix), faulttype.StartNetworkFaultPostfix, faulttype.LatencyFaultType)
}

func TestRegisterStopLatencyFaultHandler(t *testing.T) {
Expand All @@ -3868,7 +3869,7 @@ func TestRegisterStopLatencyFaultHandler(t *testing.T) {
)
}
tcs := generateCommonNetworkFaultInjectionTestCases("stop latency", "stopped", setExecExpectations, happyNetworkLatencyReqBody)
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.LatencyFaultType, faulttype.StopNetworkFaultPostfix))
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.LatencyFaultType, faulttype.StopNetworkFaultPostfix), faulttype.StopNetworkFaultPostfix, faulttype.LatencyFaultType)
}

func TestRegisterCheckLatencyFaultHandler(t *testing.T) {
Expand All @@ -3882,7 +3883,7 @@ func TestRegisterCheckLatencyFaultHandler(t *testing.T) {
)
}
tcs := generateCommonNetworkFaultInjectionTestCases("check latency", "running", setExecExpectations, happyNetworkLatencyReqBody)
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.LatencyFaultType, faulttype.CheckNetworkFaultPostfix))
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.LatencyFaultType, faulttype.CheckNetworkFaultPostfix), faulttype.CheckNetworkFaultPostfix, faulttype.LatencyFaultType)
}

func TestRegisterStartPacketLossFaultHandler(t *testing.T) {
Expand All @@ -3898,7 +3899,7 @@ func TestRegisterStartPacketLossFaultHandler(t *testing.T) {
mockCMD.EXPECT().CombinedOutput().Times(4).Return([]byte(tcCommandEmptyOutput), nil)
}
tcs := generateCommonNetworkFaultInjectionTestCases("start packet loss", "running", setExecExpectations, happyNetworkPacketLossReqBody)
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.PacketLossFaultType, faulttype.StartNetworkFaultPostfix))
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.PacketLossFaultType, faulttype.StartNetworkFaultPostfix), faulttype.StartNetworkFaultPostfix, faulttype.PacketLossFaultType)
}

func TestRegisterStopPacketLossFaultHandler(t *testing.T) {
Expand All @@ -3912,7 +3913,7 @@ func TestRegisterStopPacketLossFaultHandler(t *testing.T) {
)
}
tcs := generateCommonNetworkFaultInjectionTestCases("stop packet loss", "stopped", setExecExpectations, happyNetworkPacketLossReqBody)
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.PacketLossFaultType, faulttype.StopNetworkFaultPostfix))
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.PacketLossFaultType, faulttype.StopNetworkFaultPostfix), faulttype.StopNetworkFaultPostfix, faulttype.PacketLossFaultType)
}

func TestRegisterCheckPacketLossFaultHandler(t *testing.T) {
Expand All @@ -3926,10 +3927,10 @@ func TestRegisterCheckPacketLossFaultHandler(t *testing.T) {
)
}
tcs := generateCommonNetworkFaultInjectionTestCases("check packet loss", "running", setExecExpectations, happyNetworkPacketLossReqBody)
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.PacketLossFaultType, faulttype.CheckNetworkFaultPostfix))
testRegisterFaultHandler(t, tcs, faulthandler.NetworkFaultPath(faulttype.PacketLossFaultType, faulttype.CheckNetworkFaultPostfix), faulttype.CheckNetworkFaultPostfix, faulttype.PacketLossFaultType)
}

func testRegisterFaultHandler(t *testing.T, tcs []networkFaultTestCase, tmdsEndpoint string) {
func testRegisterFaultHandler(t *testing.T, tcs []networkFaultTestCase, tmdsEndpoint, faultOperation, faultType string) {
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
// Mocks
Expand All @@ -3942,6 +3943,13 @@ func testRegisterFaultHandler(t *testing.T, tcs []networkFaultTestCase, tmdsEndp

agentState := agentV4.NewTMDSAgentState(state, statsEngine, ecsClient, clusterName, availabilityzone, vpcID, containerInstanceArn)
metricsFactory := mock_metrics.NewMockEntryFactory(ctrl)
durationMetricEntry := mock_metrics.NewMockEntry(ctrl)
gomock.InOrder(
metricsFactory.EXPECT().New(fmt.Sprintf(durationMetricPrefix, faultOperation, faultType)).Return(durationMetricEntry).Times(1),
durationMetricEntry.EXPECT().WithFields(gomock.Any()).Return(durationMetricEntry).Times(1),
durationMetricEntry.EXPECT().WithGauge(gomock.Any()).Return(durationMetricEntry).Times(1),
durationMetricEntry.EXPECT().Done(nil).Times(1),
)
execWrapper := mock_execwrapper.NewMockExec(ctrl)

if tc.setStateExpectations != nil {
Expand Down

0 comments on commit c58bb59

Please sign in to comment.