Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export class RuleExecutionPipeline implements RuleExecutionPipelineContract {
constructor(
@inject(LoggerServiceToken) private readonly logger: LoggerServiceContract,
@multiInject(RuleExecutionStepsToken) private readonly steps: RuleExecutionStep[],
@inject(RuleExecutionMiddlewaresToken) private readonly middlewares: RuleExecutionMiddleware[]
@multiInject(RuleExecutionMiddlewaresToken)
private readonly middlewares: RuleExecutionMiddleware[]
) {}

public async execute(input: RuleExecutionInput): Promise<RuleExecutionPipelineResult> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { withSpan } from '@kbn/apm-utils';
import { ApmMiddleware } from './apm_middleware';
import { createRuleExecutionMiddlewareContext } from './test_utils';

jest.mock('@kbn/apm-utils', () => ({
withSpan: jest.fn(<T>(_opts: unknown, cb: () => Promise<T>) => cb()),
}));

const withSpanMock = withSpan as jest.MockedFunction<typeof withSpan>;

describe('ApmSpanMiddleware', () => {
const middleware = new ApmMiddleware();

it('wraps next() in withSpan and returns result on success', async () => {
const expectedResult = { type: 'continue' };
const next = jest.fn().mockResolvedValue(expectedResult);
const context = createRuleExecutionMiddlewareContext();

const result = await middleware.execute(context, next);

expect(result).toEqual(expectedResult);
expect(next).toHaveBeenCalledTimes(1);
expect(withSpanMock).toHaveBeenCalledTimes(1);
expect(withSpanMock).toHaveBeenCalledWith(
expect.objectContaining({
name: 'rule_executor:test_step',
type: 'rule_executor',
labels: { plugin: 'alerting_v2' },
}),
expect.any(Function)
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { withSpan } from '@kbn/apm-utils';
import type { RuleExecutionMiddlewareContext, RuleExecutionMiddleware } from './types';
import type { RuleStepOutput } from '../types';
import { APP_ID } from '../../constants';

/**
* Middleware that wraps each step execution in an APM span for tracing.
*/
export class ApmMiddleware implements RuleExecutionMiddleware {
public readonly name = 'apm_span';

public async execute(
ctx: RuleExecutionMiddlewareContext,
next: () => Promise<RuleStepOutput>
): Promise<RuleStepOutput> {
return withSpan(
{
name: `rule_executor:${ctx.step.name}`,
type: 'rule_executor',
labels: { plugin: APP_ID },
},
() => next()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
*/

export type { RuleExecutionMiddleware, RuleExecutionMiddlewareContext } from './types';
export { ApmMiddleware } from './apm_middleware';
export { ErrorHandlingMiddleware } from './error_handling_middleware';
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ export const RuleExecutionStepsToken = Symbol.for(
) as ServiceIdentifier<RuleExecutionStep>;

/**
* DI token for the array of step middleware.
* Middleware are executed in order (first middleware is outermost).
* Token for multi-injecting the ordered execution middlewares.
*/
export const RuleExecutionMiddlewaresToken = Symbol.for(
'alerting_v2.RuleExecutionMiddlewares'
) as ServiceIdentifier<RuleExecutionMiddleware[]>;
) as ServiceIdentifier<RuleExecutionMiddleware>;
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,23 @@ import {
CreateAlertEventsStep,
CreateRecoveryEventsStep,
} from '../lib/rule_executor/steps';
import { ErrorHandlingMiddleware } from '../lib/rule_executor/middleware';
import { ApmMiddleware, ErrorHandlingMiddleware } from '../lib/rule_executor/middleware';
import { DirectorStep } from '../lib/rule_executor/steps/director_step';
import { StoreAlertEventsStep } from '../lib/rule_executor/steps/store_alert_events';

export const bindRuleExecutionServices = ({ bind }: ContainerModuleLoadOptions) => {
/**
* Middlewares
*/
bind(ApmMiddleware).toSelf().inSingletonScope();
bind(ErrorHandlingMiddleware).toSelf().inSingletonScope();

/**
* Middleware list
* Middleware list via multi-injection.
* Binding order defines execution order.
*/
bind(RuleExecutionMiddlewaresToken)
.toDynamicValue(({ get }) => [
// Add more middleware here as needed
get(ErrorHandlingMiddleware),
])
.inSingletonScope();
bind(RuleExecutionMiddlewaresToken).to(ApmMiddleware).inSingletonScope();
bind(RuleExecutionMiddlewaresToken).to(ErrorHandlingMiddleware).inSingletonScope();

/**
* Rule execution steps via multi-injection.
Expand Down