Skip to content

Commit

Permalink
Added multi-stage failure recovery in logging #39
Browse files Browse the repository at this point in the history
  • Loading branch information
TanmoySG committed Jun 17, 2022
1 parent dba4f8b commit 2057a0a
Showing 1 changed file with 75 additions and 8 deletions.
83 changes: 75 additions & 8 deletions libraries/js/logsmith/monitor/monitor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import compile from "string-template/compile.js"
import fetch from 'node-fetch'
import { MonitorResponse } from "../lib/specs.js"

const Endpoints = {
API: compile("{0}/"),
Expand All @@ -16,19 +17,85 @@ function prepareRequestConfig(method, body) {
}
}

export function createNewPublisher(listener, publisher, callback) {
const publisherURI = Endpoints.Publisher(listener)
fetch(publisherURI, prepareRequestConfig("POST", publisher)).then(function (response) {
return response.json()
}).then(function (response) {
callback(response)
})
}

export function createNewContext(listener, publisher, context, callback) {
const contextURI = Endpoints.Context(listener, publisher.publisher)
fetch(contextURI, prepareRequestConfig("POST", context)).then(function (response) {
return response.json()
}).then(function (response) {
callback(response)
})
}


export function logToMonitor(listener, publisher, context, log, callback) {
const logURI = Endpoints.Log(listener, publisher, context)
const logURI = Endpoints.Log(listener, publisher.publisher, context.context)
fetch(logURI, prepareRequestConfig("POST", log)).then(function (response) {
return response.json()
}).then(function (response) {
console.log(response)
callback(response)
})
}

logToMonitor("http://localhost:8080", "testapp001", "testcontext001", {
"logLevel": "INFO",
"message": "Testing Logsmith",
"mode": "RESTClient"
})
export function monitorLogger(listener, publisher, context, log, callback) {
logToMonitor(listener, publisher, context, log, function (response) {
if (response.scope == MonitorResponse.publisher.missing) {
createNewPublisher(listener, publisher, function (response) {
if (response.scope == MonitorResponse.publisher.success) {
monitorLogger(listener, publisher, context, log, function (response) {
callback(response)
})
}
callback(response)
})
} else if (response.scope == MonitorResponse.context.missing) {
createNewContext(listener, publisher, context, function (response) {
if (response.scope == MonitorResponse.context.success) {
monitorLogger(listener, publisher, context, log, function (response) {
callback(response)
})
}
callback(response)
})
} else {
if (response.scope == MonitorResponse.log.error) {
// Add Logic
} else if (response.scope == MonitorResponse.log.success) {
callback(response)
}
}
})
}

// console.log(prepareRequestConfig("POST", { "test": "pass" }))
const sampleData = {
listener: "http://localhost:8080",
publisher: {
publisher: "testapp1",
origin: "bend.testapp1.com",
description: "test App"
},
context: {
context: "testcon1",
origin: "bend.testcon.con",
description: "Cotext",
kind: {
logs: []
}
},
log: {
loglevel: "WARN",
worker: "TEST WrKr"
}
}

monitorLogger(sampleData.listener, sampleData.publisher, sampleData.context, sampleData.log, function (response) {
console.log(response)
})

0 comments on commit 2057a0a

Please sign in to comment.