applicationinsights
Advanced tools
Comparing version 0.12.2 to 0.12.3
@@ -13,2 +13,3 @@ /// <reference path="Scripts/typings/node/node.d.ts" /> | ||
private _exceptionListenerHandle; | ||
private _enableCacheOnError; | ||
constructor(config?: AppInsights.IConfig); | ||
@@ -59,2 +60,3 @@ /** | ||
diagnosticLogInterval?: number; | ||
enableCacheOnError?: boolean; | ||
} | ||
@@ -61,0 +63,0 @@ var NodeAppInsights: typeof AppInsights; |
@@ -48,2 +48,3 @@ ///<reference path='Scripts\typings\node\node.d.ts' /> | ||
}; | ||
this._enableCacheOnError = !!config.enableCacheOnError; | ||
// initialize base class | ||
@@ -66,2 +67,5 @@ _super.call(this, this.config); | ||
var sender = new Sender(browserSender._config); | ||
if (this._enableCacheOnError) { | ||
sender.enableCacheOnError(); | ||
} | ||
browserSender._sender = function (payload) { return sender.send(payload); }; | ||
@@ -68,0 +72,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"bugs": "https://github.com/Microsoft/AppInsights-node.js/issues", | ||
"version": "0.12.2", | ||
"version": "0.12.3", | ||
"description": "Microsoft Application Insights module for Node.JS", | ||
@@ -14,5 +14,4 @@ "repository": { | ||
"keywords": [ | ||
"request", | ||
"exception monitoring", | ||
"request monitoring", | ||
"monitoring", | ||
"application insights", | ||
@@ -24,10 +23,2 @@ "microsoft", | ||
{ | ||
"name": "t-hajohn", | ||
"email": "t-hajohn@microsoft.com" | ||
}, | ||
{ | ||
"name": "t-andrea", | ||
"email": "t-andrea@microsoft.com" | ||
}, | ||
{ | ||
"name": "scsouthw", | ||
@@ -34,0 +25,0 @@ "email": "scsouthw@microsoft.com" |
@@ -6,2 +6,3 @@ // Type definitions for Async 0.1.23 | ||
interface AsyncAction<T> { (callback:AsyncSingleResultCallback<T>):void } | ||
interface AsyncMultipleResultsCallback<T> { (err: Error, results: T[]): any; } | ||
@@ -82,2 +83,3 @@ interface AsyncSingleResultCallback<T> { (err: Error, result: T): void; } | ||
// Control Flow | ||
series<T>(tasks: AsyncAction<T>[], callback?: AsyncMultipleResultsCallback<T>): void; | ||
series<T>(tasks: T[], callback?: AsyncMultipleResultsCallback<T>): void; | ||
@@ -84,0 +86,0 @@ series<T>(tasks: T, callback?: AsyncMultipleResultsCallback<T>): void; |
@@ -6,10 +6,33 @@ /** | ||
declare class Sender { | ||
static TEMPDIR: string; | ||
private _config; | ||
private _onSuccess; | ||
private _onError; | ||
private _enableCacheOnError; | ||
private _http; | ||
private _url; | ||
private _os; | ||
private _path; | ||
private _fs; | ||
constructor(config: Microsoft.ApplicationInsights.ISenderConfig, onSuccess?: (response: string) => void, onError?: (error: Error) => void); | ||
send(payload: string): void; | ||
/** | ||
* enable caching events locally on error | ||
*/ | ||
enableCacheOnError(): void; | ||
/** | ||
* disable caching events locally on error | ||
*/ | ||
disableCacheOnError(): void; | ||
/** | ||
* Stores the payload as a json file on disk in the temp direcotry | ||
*/ | ||
private _storeToDisk(payload); | ||
/** | ||
* Check for temp telemetry files | ||
* reads the first file if exist, deletes it and tries to send its load | ||
*/ | ||
private _sendFirstFileOnDisk(); | ||
private _onErrorHelper(error); | ||
} | ||
export = Sender; |
@@ -10,4 +10,8 @@ /** | ||
this._onError = onError; | ||
this._enableCacheOnError = false; | ||
this._http = require("http"); | ||
this._url = require("url"); | ||
this._os = require('os'); | ||
this._path = require('path'); | ||
this._fs = require('fs'); | ||
} | ||
@@ -42,7 +46,18 @@ Sender.prototype.send = function (payload) { | ||
} | ||
if (_this._enableCacheOnError) { | ||
// try to send any cached events if the user is back online | ||
if (res.statusCode === 200) { | ||
_this._sendFirstFileOnDisk(); | ||
} | ||
else { | ||
// cache the payload to send it later | ||
_this._storeToDisk(payload); | ||
} | ||
} | ||
}); | ||
}); | ||
req.on('error', function (error) { | ||
if (typeof _this._onError === "function") { | ||
_this._onError(error); | ||
_this._onErrorHelper(error); | ||
if (_this._enableCacheOnError) { | ||
_this._storeToDisk(payload); | ||
} | ||
@@ -53,4 +68,84 @@ }); | ||
}; | ||
/** | ||
* enable caching events locally on error | ||
*/ | ||
Sender.prototype.enableCacheOnError = function () { | ||
this._enableCacheOnError = true; | ||
}; | ||
/** | ||
* disable caching events locally on error | ||
*/ | ||
Sender.prototype.disableCacheOnError = function () { | ||
this._enableCacheOnError = false; | ||
}; | ||
/** | ||
* Stores the payload as a json file on disk in the temp direcotry | ||
*/ | ||
Sender.prototype._storeToDisk = function (payload) { | ||
var _this = this; | ||
//ensure directory is created | ||
var direcotry = this._path.join(this._os.tmpDir(), Sender.TEMPDIR); | ||
if (!this._fs.existsSync(direcotry)) { | ||
try { | ||
this._fs.mkdirSync(direcotry); | ||
} | ||
catch (error) { | ||
// failing to create the temp direcotry | ||
this._onErrorHelper(error); | ||
return; | ||
} | ||
} | ||
//create file - file name for now is the timestamp, a better approach would be a UUID but that | ||
//would require an external dependency | ||
var fileName = new Date().getTime() + '.json'; | ||
var fileFullPath = this._path.join(direcotry, fileName); | ||
// if the file already exist, replace the content | ||
this._fs.writeFile(fileFullPath, payload, function (error) { return _this._onErrorHelper(error); }); | ||
}; | ||
/** | ||
* Check for temp telemetry files | ||
* reads the first file if exist, deletes it and tries to send its load | ||
*/ | ||
Sender.prototype._sendFirstFileOnDisk = function () { | ||
var _this = this; | ||
var tempDir = this._path.join(this._os.tmpDir(), Sender.TEMPDIR); | ||
if (!this._fs.existsSync(tempDir)) { | ||
return; | ||
} | ||
this._fs.readdir(tempDir, function (error, files) { | ||
if (!error) { | ||
if (files.length > 0) { | ||
var firstFile = files[0]; | ||
var filePath = _this._path.join(tempDir, firstFile); | ||
_this._fs.readFile(filePath, function (error, payload) { | ||
if (!error) { | ||
// delete the file first to prevent double sending | ||
_this._fs.unlink(filePath, function (error) { | ||
if (!error) { | ||
_this.send(payload); | ||
} | ||
else { | ||
_this._onErrorHelper(error); | ||
} | ||
}); | ||
} | ||
else { | ||
_this._onErrorHelper(error); | ||
} | ||
}); | ||
} | ||
} | ||
else { | ||
_this._onErrorHelper(error); | ||
} | ||
}); | ||
}; | ||
Sender.prototype._onErrorHelper = function (error) { | ||
if (typeof this._onError === "function") { | ||
this._onError(error); | ||
} | ||
}; | ||
Sender.TEMPDIR = 'appInsights-node'; | ||
return Sender; | ||
})(); | ||
module.exports = Sender; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4633
222486
15
8