Inspect Connection (#784)
Handles `console.log` and `utils.inspect` invocations for a better debugging experience. `agent` and `ssl` are hidden since they made the logs very hard to read. The user can still access them with `instance.agent` and `instance.ssl`.
This commit is contained in:
committed by
GitHub
parent
3b41c555ae
commit
5c60d98180
2
lib/Connection.d.ts
vendored
2
lib/Connection.d.ts
vendored
@ -20,6 +20,7 @@
|
|||||||
/// <reference types="node" />
|
/// <reference types="node" />
|
||||||
|
|
||||||
import { URL } from 'url';
|
import { URL } from 'url';
|
||||||
|
import { inspect, InspectOptions } from 'util';
|
||||||
import * as http from 'http';
|
import * as http from 'http';
|
||||||
import { SecureContextOptions } from 'tls';
|
import { SecureContextOptions } from 'tls';
|
||||||
|
|
||||||
@ -73,6 +74,7 @@ export default class Connection {
|
|||||||
setRole(role: string, enabled: boolean): Connection;
|
setRole(role: string, enabled: boolean): Connection;
|
||||||
status: string;
|
status: string;
|
||||||
buildRequestObject(params: any): http.ClientRequestArgs;
|
buildRequestObject(params: any): http.ClientRequestArgs;
|
||||||
|
[inspect.custom](object: any, options: InspectOptions): string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export {};
|
export {};
|
||||||
|
|||||||
@ -20,6 +20,7 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const assert = require('assert')
|
const assert = require('assert')
|
||||||
|
const { inspect } = require('util')
|
||||||
const http = require('http')
|
const http = require('http')
|
||||||
const https = require('https')
|
const https = require('https')
|
||||||
const debug = require('debug')('elasticsearch')
|
const debug = require('debug')('elasticsearch')
|
||||||
@ -54,7 +55,7 @@ class Connection {
|
|||||||
maxSockets: keepAliveFalse ? Infinity : 256,
|
maxSockets: keepAliveFalse ? Infinity : 256,
|
||||||
maxFreeSockets: 256
|
maxFreeSockets: 256
|
||||||
}, opts.agent)
|
}, opts.agent)
|
||||||
this._agent = this.url.protocol === 'http:'
|
this.agent = this.url.protocol === 'http:'
|
||||||
? new http.Agent(agentOptions)
|
? new http.Agent(agentOptions)
|
||||||
: new https.Agent(Object.assign({}, agentOptions, this.ssl))
|
: new https.Agent(Object.assign({}, agentOptions, this.ssl))
|
||||||
|
|
||||||
@ -146,7 +147,7 @@ class Connection {
|
|||||||
if (this._openRequests > 0) {
|
if (this._openRequests > 0) {
|
||||||
setTimeout(() => this.close(callback), 1000)
|
setTimeout(() => this.close(callback), 1000)
|
||||||
} else {
|
} else {
|
||||||
this._agent.destroy()
|
this.agent.destroy()
|
||||||
callback()
|
callback()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -193,7 +194,7 @@ class Connection {
|
|||||||
auth: !!url.username === true || !!url.password === true
|
auth: !!url.username === true || !!url.password === true
|
||||||
? `${url.username}:${url.password}`
|
? `${url.username}:${url.password}`
|
||||||
: undefined,
|
: undefined,
|
||||||
agent: this._agent
|
agent: this.agent
|
||||||
}
|
}
|
||||||
|
|
||||||
const paramsKeys = Object.keys(params)
|
const paramsKeys = Object.keys(params)
|
||||||
@ -218,6 +219,23 @@ class Connection {
|
|||||||
|
|
||||||
return request
|
return request
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handles console.log and utils.inspect invocations.
|
||||||
|
// We want to hide `agent` and `ssl` since they made
|
||||||
|
// the logs very hard to read. The user can still
|
||||||
|
// access them with `instance.agent` and `instance.ssl`.
|
||||||
|
[inspect.custom] (depth, options) {
|
||||||
|
return {
|
||||||
|
url: this.url,
|
||||||
|
id: this.id,
|
||||||
|
headers: this.headers,
|
||||||
|
deadCount: this.deadCount,
|
||||||
|
resurrectTimeout: this.resurrectTimeout,
|
||||||
|
_openRequests: this._openRequests,
|
||||||
|
status: this.status,
|
||||||
|
roles: this.roles
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection.statuses = {
|
Connection.statuses = {
|
||||||
|
|||||||
@ -20,6 +20,7 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const { test } = require('tap')
|
const { test } = require('tap')
|
||||||
|
const { inspect } = require('util')
|
||||||
const { createGzip, createDeflate } = require('zlib')
|
const { createGzip, createDeflate } = require('zlib')
|
||||||
const { URL } = require('url')
|
const { URL } = require('url')
|
||||||
const intoStream = require('into-stream')
|
const intoStream = require('into-stream')
|
||||||
@ -670,3 +671,45 @@ test('setRole', t => {
|
|||||||
|
|
||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('Util.inspect Connection class should hide agent and ssl', t => {
|
||||||
|
t.plan(1)
|
||||||
|
|
||||||
|
const connection = new Connection({
|
||||||
|
url: new URL('http://localhost:9200'),
|
||||||
|
id: 'node-id',
|
||||||
|
headers: { foo: 'bar' }
|
||||||
|
})
|
||||||
|
|
||||||
|
// Removes spaces and new lines because
|
||||||
|
// utils.inspect is handled differently
|
||||||
|
// between major versions of Node.js
|
||||||
|
function cleanStr (str) {
|
||||||
|
return str
|
||||||
|
.replace(/\s/g, '')
|
||||||
|
.replace(/(\r\n|\n|\r)/gm, '')
|
||||||
|
}
|
||||||
|
|
||||||
|
t.strictEqual(cleanStr(inspect(connection)), cleanStr(`{ url:
|
||||||
|
URL {
|
||||||
|
href: 'http://localhost:9200/',
|
||||||
|
origin: 'http://localhost:9200',
|
||||||
|
protocol: 'http:',
|
||||||
|
username: '',
|
||||||
|
password: '',
|
||||||
|
host: 'localhost:9200',
|
||||||
|
hostname: 'localhost',
|
||||||
|
port: '9200',
|
||||||
|
pathname: '/',
|
||||||
|
search: '',
|
||||||
|
searchParams: URLSearchParams {},
|
||||||
|
hash: '' },
|
||||||
|
id: 'node-id',
|
||||||
|
headers: { foo: 'bar' },
|
||||||
|
deadCount: 0,
|
||||||
|
resurrectTimeout: 0,
|
||||||
|
_openRequests: 0,
|
||||||
|
status: 'alive',
|
||||||
|
roles: { master: true, data: true, ingest: true, ml: false } }`)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user