Skip to content

Commit

Permalink
[pinpoint-apm#117] Support logger, authentication adaptor pattern
Browse files Browse the repository at this point in the history
typescript tools support for .d.ts
    * https://nodejs.org/api/modules.html#enabling
    * fix license convention by https://docs.npmjs.com/cli/v8/configuring-npm/package-json#license

Logger Adaptor pattern
   * The Logger must implements pinpoint.json config loading status and a  log level. 
    * support integration test
    * if adaptor doesn't have adaptor outer method, Agent outs logs to console
  • Loading branch information
feelform committed Oct 25, 2022
1 parent bdb79c9 commit 7d5e4e9
Show file tree
Hide file tree
Showing 21 changed files with 558 additions and 59 deletions.
2 changes: 1 addition & 1 deletion lib/client/grpc-data-sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const grpc = require('@grpc/grpc-js')
const log = require('../utils/logger')
const services = require('../data/v1/Service_grpc_pb')
const dataConvertor = require('../data/grpc-data-convertor')
const pingIdGenerator = require('../context/sequence-generator').pingIdGenerator
const pingIdGenerator = require('../context/sequence-generators').pingIdGenerator
const GrpcBidirectionalStream = require('./grpc-bidirectional-stream')
const GrpcClientSideStream = require('./grpc-client-side-stream')
const GrpcUnaryRPC = require('./grpc-unary-rpc')
Expand Down
7 changes: 6 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,15 @@ function hasDockerCGroup() {
}
}

function needsLoadConfig() {
return !agentConfig
}

module.exports = {
getConfig,
clear,
readConfigJson,
readRootConfigFile,
getMainModulePath
getMainModulePath,
needsLoadConfig
}
2 changes: 1 addition & 1 deletion lib/context/api-meta-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

'use strict'

const apiMetaCacheKeyGenerator = require('../context/sequence-generator').apiMetaCacheKeyGenerator
const apiMetaCacheKeyGenerator = require('../context/sequence-generators').apiMetaCacheKeyGenerator
const SimpleCache = require('../utils/simple-cache')
const GeneralMethodDescriptor = require('../constant/method-descriptor').GeneralMethodDescriptor
const ApiMetaInfo = require('../data/dto/api-meta-info')
Expand Down
2 changes: 1 addition & 1 deletion lib/context/disable-span-event-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'use strict'

const AsyncId = require('./async-id')
const asyncIdGenerator = require('./sequence-generator').asyncIdGenerator
const asyncIdGenerator = require('./sequence-generators').asyncIdGenerator

class DisableSpanEventRecorder {
constructor() {
Expand Down
10 changes: 1 addition & 9 deletions lib/context/sequence-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,4 @@ class SequenceGenerator{
}
}

module.exports = {
SequenceGenerator,
transactionIdGenerator: new SequenceGenerator(),
asyncIdGenerator: new SequenceGenerator(1),
stringMetaCacheKeyGenerator: new SequenceGenerator(1),
apiMetaCacheKeyGenerator: new SequenceGenerator(1),
pingIdGenerator: new SequenceGenerator(),
dummyIdGenerator: new SequenceGenerator(),
}
module.exports = SequenceGenerator
11 changes: 11 additions & 0 deletions lib/context/sequence-generators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const SequenceGenerator = require('./sequence-generator')

module.exports = {
transactionIdGenerator: new SequenceGenerator(),
asyncIdGenerator: new SequenceGenerator(1),
stringMetaCacheKeyGenerator: new SequenceGenerator(1),
apiMetaCacheKeyGenerator: new SequenceGenerator(1),
pingIdGenerator: new SequenceGenerator(),
dummyIdGenerator: new SequenceGenerator(),
}

4 changes: 2 additions & 2 deletions lib/context/span-event-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const Annotation = require('./annotation')
const DefaultAnnotationKey = require('../constant/annotation-key').DefaultAnnotationKey
const StringMetaService = require('./string-meta-service')
const AsyncId = require('./async-id')
const asyncIdGenerator = require('./sequence-generator').asyncIdGenerator
const dummyIdGenerator = require('./sequence-generator').dummyIdGenerator
const asyncIdGenerator = require('./sequence-generators').asyncIdGenerator
const dummyIdGenerator = require('./sequence-generators').dummyIdGenerator
const AnnotationKeyUtils = require('./annotation-key-utils')

// https://github.com/pinpoint-apm/pinpoint/blob/master/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/context/SpanEventRecorder.java
Expand Down
13 changes: 9 additions & 4 deletions lib/context/trace-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class TraceContext {
this.dataSender = null
}

/**
* trace context singleton instance
* @param {string} options
* @param {string} dataSender
* @param {string} config
* @returns {TraceContext}
* @constructor
*/
static init (options, dataSender, config) {
if (!options.agentId || !options.applicationName) {
throw new Error('Fail to initialize pinpoint context')
Expand Down Expand Up @@ -132,7 +140,4 @@ class TraceContext {
}
}

module.exports = {
init: TraceContext.init
}

module.exports = TraceContext
2 changes: 1 addition & 1 deletion lib/context/transaction-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

'use strict'

const transactionIdGenerator = require('./sequence-generator').transactionIdGenerator
const transactionIdGenerator = require('./sequence-generators').transactionIdGenerator

const DELIMETER = '^'

Expand Down
1 change: 1 addition & 0 deletions lib/instrumentation/module/mysql2.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ module.exports = function (agent, version, mysql2) {
}
}
}
}
45 changes: 45 additions & 0 deletions lib/supports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Pinpoint Node.js Agent
* Copyright 2022-present NAVER Corp.
* Apache License v2.0
*/

'use strict'

const Logger = require('./utils/log/logger2')
const { getConfig, needsLoadConfig } = require('./config')

const noneConfigurationLog = new Logger.NoneBuilder({
output: console,
debug: function (message) {
this.output.debug(message)
},
info: function (message) {
this.output.info(message)
},
warn: function (message) {
this.output.warn(message)
},
error: function (message) {
this.output.error(message)
}
}).build()

let log = undefined
module.exports = {
getLog: function () {
if (log) {
return log
}

if (needsLoadConfig()) {
return noneConfigurationLog
}

const config = getConfig()
if (!log) {
log = Logger.makeBuilder(config.logLevel, {}).build()
}
return log
}
}
63 changes: 63 additions & 0 deletions lib/utils/log/logger-output-adaptor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Pinpoint Node.js Agent
* Copyright 2022-present NAVER Corp.
* Apache License v2.0
*/

'use strict'

class LoggerOutputAdaptor {
constructor(output) {
this.output = output
}

get console() {
return console
}

debug(message) {
if (!this.output || typeof this.output.debug != 'function') {
if (typeof this.output.error === 'function') {
this.output.error("The Adaptor doesn't has the debug function.")
} else {
this.console.error("The Adaptor doesn't has the debug function.")
}
return
}
this.output.debug(message)
}

info(message) {
if (!this.output || typeof this.output.info != 'function') {
if (typeof this.output.error === 'function') {
this.output.error("The Adaptor doesn't has the info function.")
} else {
this.console.error("The Adaptor doesn't has the info function.")
}
return
}
this.output.info(message)
}

warn(message) {
if (!this.output || typeof this.output.warn != 'function') {
if (typeof this.output.error === 'function') {
this.output.error("The Adaptor doesn't has the warn function.")
} else {
this.console.error("The Adaptor doesn't has the warn function.")
}
return
}
this.output.warn(message)
}

error(message) {
if (!this.output || typeof this.output.error != 'function') {
this.console.error("The Adaptor doesn't has the error function.")
return
}
this.output.error(message)
}
}

module.exports = LoggerOutputAdaptor
128 changes: 128 additions & 0 deletions lib/utils/log/logger2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/

'use strict'

const LoggerOutputAdaptor = require('./logger-output-adaptor')

class Logger {

constructor(type, adaptor) {
this.type = type
this.adaptor = new LoggerOutputAdaptor(adaptor)
}

static get DebugBuilder() {
return Logger.Builder(LogType.debug)
}

static Builder(type) {
return class Builder {
constructor(adaptor) {
this.type = type
this.adaptor = adaptor
}

build() {
return new Logger(this.type, this.adaptor)
}
}
}

static get InfoBuilder() {
return Logger.Builder(LogType.info)
}

static get WarnBuilder() {
return Logger.Builder(LogType.warn)
}

static get ErrorBuilder() {
return Logger.Builder(LogType.error)
}

static get NoneBuilder() {
return Logger.Builder(LogType.noneConfig)
}

static makeBuilder(name, adaptor) {
return new (Logger.Builder(LogType.valueOf(name)))(adaptor)
}

debug(message) {
this.adaptor.debug(message)
}

isDebug() {
return this.type.isDebug()
}

info(message) {
this.adaptor.info(message)
}

isInfo() {
return this.type.isInfo()
}

warn(message) {
this.adaptor.warn(message)
}

error(message) {
this.adaptor.error(message)
}
}

class LogType {

static get debug() {
return new LogType('DEBUG')
}

static get info() {
return new LogType('INFO')
}

static get warn() {
return new LogType('WARN')
}

static get error() {
return new LogType('ERROR')
}

static get noneConfig() {
return new LogType('NONE')
}

static valueOf(name) {
if (name.toUpperCase) {
name = name.toUpperCase()
}
const type = [this.debug, this.info, this.warn, this.error].find(element => element.name === name)
if (!type) {
return this.noneConfig
}

return type
}

constructor(name) {
this.name = name
}

isDebug() {
return this.name === LogType.debug.name
}

isInfo() {
return this.name === LogType.info.name
}
}


module.exports = Logger
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "pinpoint-node-agent",
"version": "0.9.0-next.2",
"main": "index.js",
"type": "commonjs",
"scripts": {
"test": "./node_modules/.bin/tape ./test/**/*.test.js",
"lint": "./node_modules/.bin/eslint -f checkstyle ./lib > checkstyle-result.xml;exit 0",
Expand All @@ -18,12 +19,6 @@
"Yongseok Kang <feelform@gmail.com> (https://github.com/feelform)"
],
"license": "Apache-2.0",
"licenses": [
{
"type": "Apache-2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0"
}
],
"bugs": {
"url": "https://github.com/pinpoint-apm/pinpoint-node-agent/issues"
},
Expand Down Expand Up @@ -91,6 +86,7 @@
"rimraf": "^2.6.2",
"tap-spec": "^5.0.0",
"tape": "^4.9.1",
"testcontainers": "^7.24.0"
"testcontainers": "^7.24.0",
"typescript": "^4.8.3"
}
}
Loading

0 comments on commit 7d5e4e9

Please sign in to comment.