Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect metrics about the active/idle connections to ES nodes #141434

Merged
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -61,6 +61,19 @@ const mockedResponse: StatusResponse = {
'15m': 0.1,
},
},
elasticsearch_client: {
protocol: 'https',
connectedNodes: 3,
nodesWithActiveSockets: 3,
nodesWithIdleSockets: 1,
totalActiveSockets: 25,
totalIdleSockets: 2,
totalQueuedRequests: 0,
mostActiveNodeSockets: 15,
averageActiveSocketsPerNode: 8,
mostIdleNodeSockets: 2,
averageIdleSocketsPerNode: 0.5,
},
process: {
pid: 1,
memory: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
export { ScopedClusterClient } from './src/scoped_cluster_client';
export { ClusterClient } from './src/cluster_client';
export { configureClient } from './src/configure_client';
export { AgentManager } from './src/agent_manager';
export { type AgentStore, AgentManager } from './src/agent_manager';
export { getRequestDebugMeta, getErrorMessage } from './src/log_query_and_deprecation';
export {
PRODUCT_RESPONSE_HEADER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ describe('AgentManager', () => {
const agentFactory = agentManager.getAgentFactory();
const agent = agentFactory({ url: new URL('http://elastic-node-1:9200') });
// eslint-disable-next-line dot-notation
expect(agentManager['httpStore'].has(agent)).toEqual(true);
expect(agentManager['agents'].has(agent)).toEqual(true);
agent.destroy();
// eslint-disable-next-line dot-notation
expect(agentManager['httpStore'].has(agent)).toEqual(false);
expect(agentManager['agents'].has(agent)).toEqual(false);
});
});

Expand All @@ -122,4 +122,21 @@ describe('AgentManager', () => {
});
});
});

describe('#getAgents()', () => {
it('returns the created HTTP and HTTPs Agent instances', () => {
const agentManager = new AgentManager();
const agentFactory1 = agentManager.getAgentFactory();
const agentFactory2 = agentManager.getAgentFactory();
const agent1 = agentFactory1({ url: new URL('http://elastic-node-1:9200') });
const agent2 = agentFactory2({ url: new URL('http://elastic-node-1:9200') });
const agent3 = agentFactory1({ url: new URL('https://elastic-node-1:9200') });
const agent4 = agentFactory2({ url: new URL('https://elastic-node-1:9200') });

const agents = agentManager.getAgents();

expect(agents.size).toEqual(4);
expect([...agents]).toEqual(expect.arrayContaining([agent1, agent2, agent3, agent4]));
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { Agent as HttpAgent } from 'http';
import { Agent as HttpsAgent } from 'https';
import { ConnectionOptions, HttpAgentOptions } from '@elastic/elasticsearch';
import type { ConnectionOptions, HttpAgentOptions } from '@elastic/elasticsearch';

const HTTPS = 'https:';
const DEFAULT_CONFIG: HttpAgentOptions = {
Expand All @@ -22,6 +22,14 @@ const DEFAULT_CONFIG: HttpAgentOptions = {
export type NetworkAgent = HttpAgent | HttpsAgent;
export type AgentFactory = (connectionOpts: ConnectionOptions) => NetworkAgent;

export interface AgentFactoryProvider {
getAgentFactory(agentOptions?: HttpAgentOptions): AgentFactory;
}

export interface AgentStore {
getAgents(): Set<NetworkAgent>;
}

/**
* Allows obtaining Agent factories, which can then be fed into elasticsearch-js's Client class.
* Ideally, we should obtain one Agent factory for each ES Client class.
Expand All @@ -33,15 +41,11 @@ export type AgentFactory = (connectionOpts: ConnectionOptions) => NetworkAgent;
* exposes methods that can modify the underlying pools, effectively impacting the connections of other Clients.
* @internal
**/
export class AgentManager {
// Stores Https Agent instances
private httpsStore: Set<HttpsAgent>;
// Stores Http Agent instances
private httpStore: Set<HttpAgent>;
export class AgentManager implements AgentFactoryProvider, AgentStore {
private agents: Set<HttpAgent>;

constructor(private agentOptions: HttpAgentOptions = DEFAULT_CONFIG) {
this.httpsStore = new Set();
this.httpStore = new Set();
this.agents = new Set();
}

public getAgentFactory(agentOptions?: HttpAgentOptions): AgentFactory {
Expand All @@ -61,8 +65,8 @@ export class AgentManager {
connectionOpts.tls
);
httpsAgent = new HttpsAgent(config);
this.httpsStore.add(httpsAgent);
dereferenceOnDestroy(this.httpsStore, httpsAgent);
this.agents.add(httpsAgent);
dereferenceOnDestroy(this.agents, httpsAgent);
}

return httpsAgent;
Expand All @@ -71,19 +75,23 @@ export class AgentManager {
if (!httpAgent) {
const config = Object.assign({}, DEFAULT_CONFIG, this.agentOptions, agentOptions);
httpAgent = new HttpAgent(config);
this.httpStore.add(httpAgent);
dereferenceOnDestroy(this.httpStore, httpAgent);
this.agents.add(httpAgent);
dereferenceOnDestroy(this.agents, httpAgent);
}

return httpAgent;
};
}

public getAgents(): Set<NetworkAgent> {
return this.agents;
}
}

const dereferenceOnDestroy = (protocolStore: Set<NetworkAgent>, agent: NetworkAgent) => {
const dereferenceOnDestroy = (store: Set<NetworkAgent>, agent: NetworkAgent) => {
const doDestroy = agent.destroy.bind(agent);
agent.destroy = () => {
protocolStore.delete(agent);
store.delete(agent);
doDestroy();
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ describe('ClusterClient', () => {
let authHeaders: ReturnType<typeof httpServiceMock.createAuthHeaderStorage>;
let internalClient: jest.Mocked<Client>;
let scopedClient: jest.Mocked<Client>;
let agentManager: AgentManager;
let agentFactoryProvider: AgentManager;

const mockTransport = { mockTransport: true };

beforeEach(() => {
logger = loggingSystemMock.createLogger();
internalClient = createClient();
scopedClient = createClient();
agentManager = new AgentManager();
agentFactoryProvider = new AgentManager();

authHeaders = httpServiceMock.createAuthHeaderStorage();
authHeaders.get.mockImplementation(() => ({
Expand Down Expand Up @@ -84,21 +84,21 @@ describe('ClusterClient', () => {
authHeaders,
type: 'custom-type',
getExecutionContext: getExecutionContextMock,
agentManager,
agentFactoryProvider,
kibanaVersion,
});

expect(configureClientMock).toHaveBeenCalledTimes(2);
expect(configureClientMock).toHaveBeenCalledWith(config, {
logger,
agentManager,
agentFactoryProvider,
kibanaVersion,
type: 'custom-type',
getExecutionContext: getExecutionContextMock,
});
expect(configureClientMock).toHaveBeenCalledWith(config, {
logger,
agentManager,
agentFactoryProvider,
kibanaVersion,
type: 'custom-type',
getExecutionContext: getExecutionContextMock,
Expand All @@ -113,7 +113,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});

Expand All @@ -128,7 +128,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = httpServerMock.createKibanaRequest();
Expand All @@ -155,7 +155,7 @@ describe('ClusterClient', () => {
authHeaders,
getExecutionContext,
getUnauthorizedErrorHandler,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = httpServerMock.createKibanaRequest();
Expand All @@ -179,7 +179,7 @@ describe('ClusterClient', () => {
authHeaders,
getExecutionContext,
getUnauthorizedErrorHandler,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = httpServerMock.createKibanaRequest();
Expand Down Expand Up @@ -212,7 +212,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = httpServerMock.createKibanaRequest();
Expand All @@ -237,7 +237,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = httpServerMock.createKibanaRequest({
Expand Down Expand Up @@ -271,7 +271,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = httpServerMock.createKibanaRequest({});
Expand Down Expand Up @@ -305,7 +305,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = httpServerMock.createKibanaRequest({
Expand Down Expand Up @@ -344,7 +344,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = httpServerMock.createKibanaRequest({});
Expand Down Expand Up @@ -373,7 +373,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = httpServerMock.createKibanaRequest({
Expand Down Expand Up @@ -410,7 +410,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = httpServerMock.createKibanaRequest({});
Expand Down Expand Up @@ -445,7 +445,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = httpServerMock.createKibanaRequest({
Expand Down Expand Up @@ -482,7 +482,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = httpServerMock.createKibanaRequest();
Expand Down Expand Up @@ -513,7 +513,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = httpServerMock.createKibanaRequest({
Expand Down Expand Up @@ -547,7 +547,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = httpServerMock.createKibanaRequest({
Expand Down Expand Up @@ -579,7 +579,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = {
Expand Down Expand Up @@ -612,7 +612,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});
const request = {
Expand Down Expand Up @@ -640,7 +640,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});

Expand All @@ -658,7 +658,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});

Expand Down Expand Up @@ -703,7 +703,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});

Expand All @@ -720,7 +720,7 @@ describe('ClusterClient', () => {
logger,
type: 'custom-type',
authHeaders,
agentManager,
agentFactoryProvider,
kibanaVersion,
});

Expand Down
Loading