Skip to content

Commit

Permalink
hoist more test values, log() calls enqueue()
Browse files Browse the repository at this point in the history
  • Loading branch information
tlhunter committed Jun 15, 2023
1 parent e453ab7 commit 92a7558
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 43 deletions.
12 changes: 8 additions & 4 deletions packages/dd-trace/src/external-logger/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ class V2LogWriter {
return tagArray.join(',')
}

// Parses and enqueues a log
log (log, span, tags) {
const logTags = this.tagString(tags)

if (span) {
log['dd.trace_id'] = span.trace_id + ''
log['dd.span_id'] = span.span_id + ''
log['dd.trace_id'] = String(span.trace_id)
log['dd.span_id'] = String(span.span_id)
}
const toLog = {

const payload = {
...log,
'timestamp': Date.now(),
'hostname': log.hostname || this.hostname,
Expand All @@ -53,9 +56,10 @@ class V2LogWriter {
'ddtags': logTags || undefined
}

return toLog
this.enqueue(payload)
}

// Enqueues a raw, non-formatted log object
enqueue (log) {
if (this.buffer.length >= this.buffer_limit) {
this.flush()
Expand Down
54 changes: 15 additions & 39 deletions packages/dd-trace/src/external-logger/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ const tracerLogger = require('../../log')

describe('External Logger', () => {
let externalLogger
let V2LogWriter
let interceptor
let errorLog

beforeEach(() => {
const V2LogWriter = require('../src')
errorLog = sinon.spy(tracerLogger, 'error')

V2LogWriter = proxyquire('../src', {
'../../log': {
error: errorLog
}
})

externalLogger = new V2LogWriter({
ddsource: 'logging_from_space',
Expand All @@ -22,8 +29,6 @@ describe('External Logger', () => {
interval: 10000,
timeout: 5000
})

errorLog = sinon.spy(tracerLogger, 'error')
})

afterEach(() => {
Expand All @@ -39,9 +44,7 @@ describe('External Logger', () => {
.post('/api/v2/logs')
.reply((_uri, req, cb) => {
request = req
cb(null, [202, '{}', {
'Content-Type': 'application/json'
}])
cb(null, [202, '{}', { 'Content-Type': 'application/json' }])
})

const span = {
Expand All @@ -54,15 +57,14 @@ describe('External Logger', () => {
version: '1.2.3',
service: 'external'
}
const log = externalLogger.log({
externalLogger.log({
message: 'oh no, something is up',
custom: 'field',
attribute: 'funky',
service: 'outer_space',
level: 'info'
}, span, tags)

externalLogger.enqueue(log)
externalLogger.flush((err) => {
try {
expect(request[0]).to.have.property('message', 'oh no, something is up')
Expand Down Expand Up @@ -99,25 +101,12 @@ describe('External Logger', () => {
})

it('tracer logger should handle error response codes from Logs API', (done) => {

const V2LogWriter = proxyquire('../src', {
'../../log': {
error: errorLog
}
})

const logger = new V2LogWriter({
ddsource: 'logging_from_space',
hostname: 'mac_desktop',
apiKey: 'API_KEY_PLACEHOLDER',
interval: 5000
})
interceptor = nock('https://http-intake.logs.datadoghq.com:443')
.post('/api/v2/logs')
.reply(400, {})

logger.enqueue({})
logger.flush((err) => {
externalLogger.enqueue({})
externalLogger.flush((err) => {
expect(err).to.be.true
expect(errorLog.getCall(0).args[0]).to.be.equal(
'failed to send 1 logs, received response code 400'
Expand All @@ -126,26 +115,13 @@ describe('External Logger', () => {
})
})

it('tracer logger should handle client side error', (done) => {
const V2LogWriter = proxyquire('../src', {
'../../log': {
error: errorLog
}
})

const logger = new V2LogWriter({
ddsource: 'logging_from_space',
hostname: 'mac_desktop',
apiKey: 'API_KEY_PLACEHOLDER',
interval: 5000
})

it('tracer logger should handle simulated network error', (done) => {
interceptor = nock('https://http-intake.logs.datadoghq.com:443')
.post('/api/v2/logs')
.replyWithError('missing API key')

logger.enqueue({})
logger.flush((err) => {
externalLogger.enqueue({})
externalLogger.flush((err) => {
expect(err).to.be.an.instanceOf(Error)
expect(errorLog.getCall(0).args[0]).to.be.equal(
'failed to send 1 log(s), with error missing API key'
Expand Down

0 comments on commit 92a7558

Please sign in to comment.