Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
blueww committed Dec 12, 2024
1 parent f7b2aba commit f4abf9d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 221 deletions.
3 changes: 1 addition & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"name": "Azurite Service - Loki",
"cwd": "${workspaceFolder}",
"runtimeArgs": ["-r", "ts-node/register"],
"args": ["${workspaceFolder}/src/azurite.ts", "-d", "debug.log"],//,"--DisableTelemetry"],
//"args": ["${workspaceFolder}/src/azurite.ts", "-d", "debug.log","--DisableTelemetry"],
"args": ["${workspaceFolder}/src/azurite.ts", "-d", "debug.log"],
"env": {
"AZURITE_ACCOUNTS": ""
},
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- [OAuth Configuration](#oauth-configuration)
- [Skip API Version Check](#skip-api-version-check)
- [Disable Product Style Url](#disable-product-style-url)
- [Disable Telemetry Collection](#disable-telemetry-collection)
- [Use in-memory storage](#use-in-memory-storage)
- [Command Line Options Differences between Azurite V2](#command-line-options-differences-between-azurite-v2)
- [Supported Environment Variable Options](#supported-environment-variable-options)
Expand Down
6 changes: 2 additions & 4 deletions src/blob/middlewares/telemetry.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import ExpressRequestAdapter from '../generated/ExpressRequestAdapter';
import ExpressResponseAdapter from '../generated/ExpressResponseAdapter';

/**
* End middleware is used to send out final HTTP response.
* TelemetryMiddleware is used to send telemetry for requests.
*
* @export
* @param {Context} context
* @param {Request} req An express compatible Request object
* @param {Response} res An express compatible Response object
* @param {ILogger} logger A valid logger
* @param {NextFunction} next An express middleware next callback
*/
function telemetryMiddleware(
context: Context,
Expand Down
4 changes: 0 additions & 4 deletions src/common/Environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ args
export default class Environment implements IEnvironment {
private flags = args.parse(process.argv);

//public ToString(): string | undefined {
// return Object.getOwnPropertyNames(this.flags).join(", ");
//}

public blobHost(): string | undefined {
return this.flags.blobHost;
}
Expand Down
230 changes: 27 additions & 203 deletions src/common/Telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class AzuriteTelemetryClient {

// Debug options
private static isDebug = false; // false in production, true in development
private static requestCollectPercentage = AzuriteTelemetryClient.isDebug ? 100 : 100;
private static requestCollectPercentage = AzuriteTelemetryClient.isDebug ? 100 : 1;
private static enableAppInsightLog = AzuriteTelemetryClient.isDebug? true : false;
private static cloudRole = AzuriteTelemetryClient.isDebug ? "AzuriteTest" : "Azurite_V1.0";
// 0 means send as soon as it's collected, use it in both debug and release mode, since set any other value will make Azurite exist slower
Expand Down Expand Up @@ -197,122 +197,34 @@ export class AzuriteTelemetryClient {
}

AzuriteTelemetryClient.requestClient.trackRequest(
{
name:reqName,
url:context.request !== undefined ? AzuriteTelemetryClient.GetRequestUri(context.request.getEndpoint()) : "",
duration:context.startTime?((new Date()).getTime() - context.startTime?.getTime()):0,
resultCode:context.response?.getStatusCode()??0,
success:(context.response?.getStatusCode() ?? 500) <= 399,
id: context.contextId, // Request ID
source: context.request?.getHeader("user-agent"), // User Agent
properties: requestProperties,
contextObjects:
{
name:reqName,
url:context.request !== undefined ? AzuriteTelemetryClient.GetRequestUri(context.request.getEndpoint()) : "",
duration:context.startTime?((new Date()).getTime() - context.startTime?.getTime()):0,
resultCode:context.response?.getStatusCode()??0,
success:(context.response?.getStatusCode() ?? 500) <= 399,
id: context.contextId, // Request ID
source: context.request?.getHeader("user-agent"), // User Agent
properties: requestProperties,
contextObjects:
{
operationId: "",
operationParentId: "",
operationName: "test",
operation_Name: "test",
appName: ""
}
});
operationId: "",
operationParentId: "",
operationName: "test",
operation_Name: "test",
appName: ""
}
});

logger.verbose(`Send ${serviceType} telemetry: ` + reqName, context.contextId === undefined ? context.contextID : context.contextId);
}
logger.verbose(`Send ${serviceType} telemetry: ` + reqName, context.contextId === undefined ? context.contextID : context.contextId);
}
catch (e)
{
logger.warn(`Fail to telemetry a ${serviceType} request, error: ` + e.message);
}
}

// public static TraceQueueRequest(context: QueueContext) {
// try{
// if (AzuriteTelemetryClient.enableTelemetry && AzuriteTelemetryClient.requestClient !== undefined)
// {
// AzuriteTelemetryClient._totalQueueRequestCount++;
// let requestProperties: { [key: string]: any } = {
// apiVersion: "v"+context.request?.getHeader("x-ms-version"),
// authorization: context.request !== undefined ? AzuriteTelemetryClient.GetRequestAuthentication(context.request.getHeader("authorization"), context.request.getQuery("sig")) : "",
// instanceID: AzuriteTelemetryClient.instanceID,
// sessionID: AzuriteTelemetryClient.sessionID,
// totalReqs:AzuriteTelemetryClient._totalQueueRequestCount,
// };
// if (context.request?.getHeader("content-length") !== undefined)
// {
// requestProperties["requestContentSize"] = context.request?.getHeader("content-length");
// }
// // Responds "content-length" Not work, as responds normally don't have "content-length" header even has body.
// AzuriteTelemetryClient.requestClient.trackRequest(
// {
// name:"Q_" + QueueOperation[context.operation??0],
// url:context.request !== undefined ? AzuriteTelemetryClient.GetRequestUri(context.request.getEndpoint()) : "",
// duration:context.startTime?((new Date()).getTime() - context.startTime?.getTime()):0,
// resultCode:context.response?.getStatusCode()??0,
// success:context.response?.getStatusCode()?.toString().startsWith("2")??false,
// id: context.contextID,
// source: context.request?.getHeader("user-agent"),
// properties: requestProperties,
// contextObjects:
// {
// operationId: "",
// operationParentId: "",
// operationName: "test",
// operation_Name: "test",
// appName: ""
// }
// });
// }
// logger.verbose('Send queue telemetry: ' + QueueOperation[context.operation??0], context.contextID);
// }
// catch (e)
// {
// logger.warn('Fail to telemetry a queue request, error: ' + e.message);
// }
// }

// public static TraceTableRequest(context: TableContext) {
// try{
// if (AzuriteTelemetryClient.enableTelemetry && AzuriteTelemetryClient.requestClient !== undefined)
// {
// AzuriteTelemetryClient._totalTableRequestCount++;
// let requestProperties: { [key: string]: any } = {
// apiVersion: "v"+context.request?.getHeader("x-ms-version"),
// authorization: context.request !== undefined ? AzuriteTelemetryClient.GetRequestAuthentication(context.request.getHeader("authorization"), context.request.getQuery("sig")) : "",
// instanceID: AzuriteTelemetryClient.instanceID,
// sessionID: AzuriteTelemetryClient.sessionID,
// totalReqs:AzuriteTelemetryClient._totalTableRequestCount,
// };
// if (context.request?.getHeader("content-length") !== undefined)
// {
// requestProperties["requestContentSize"] = context.request?.getHeader("content-length");
// }
// AzuriteTelemetryClient.requestClient.trackRequest(
// {
// name:"T_" + TableOperation[context.operation??0],
// url:context.request !== undefined ? AzuriteTelemetryClient.GetRequestUri(context.request.getEndpoint()) : "",
// duration:context.startTime?((new Date()).getTime() - context.startTime?.getTime()):0,
// resultCode:context.response?.getStatusCode()??0,
// success:context.response?.getStatusCode()?.toString().startsWith("2")??false,
// id: context.contextID,
// source: context.request?.getHeader("user-agent"),
// properties: requestProperties,
// contextObjects:
// {
// operationId: "",
// operationParentId: "",
// operationName: "test",
// operation_Name: "test",
// appName: ""
// }
// });
// }
// logger.verbose('Send table telemetry: ' + TableOperation[context.operation??0], context.contextID);
// }
// catch (e)
// {
// logger.warn('Fail to telemetry a table request, error: ' + e.message);
// }
// }

public static async TraceStartEvent(serviceType: string = "") {
try{
if (AzuriteTelemetryClient.enableTelemetry && AzuriteTelemetryClient.eventClient !== undefined)
Expand All @@ -325,8 +237,8 @@ export class AzuriteTelemetryClient {
parameters: await AzuriteTelemetryClient.GetAllParameterString()
}
});
logger.verbose('Send start telemetry');
}
logger.verbose('Send start telemetry');
}
catch (e)
{
Expand All @@ -350,8 +262,8 @@ export class AzuriteTelemetryClient {
totalEgress: AzuriteTelemetryClient._totalEgressSize,
}
});
logger.verbose('Send stop telemetry');
}
logger.verbose('Send stop telemetry');
}
catch (e)
{
Expand Down Expand Up @@ -447,15 +359,15 @@ export class AzuriteTelemetryClient {
{
parameters += "AZURITE_DB,";
}
if (AzuriteTelemetryClient.env === undefined)
{
return parameters;
}
let longParameters = ["blobHost","queueHost","tableHost","blobPort","queuePort","tablePort","blobKeepAliveTimeout","queueKeepAliveTimeout","tableKeepAliveTimeout","location","cert","key","pwd","oauth","extentMemoryLimit","debug","silent","loose","skipApiVersionCheck","disableProductStyleUrl","inMemoryPersistence","disableTelemetry"];
let shortParameters: { [string: string]: any } = {"d": "debug", "l": "location", "L": "loose", "s": "silent"};

if (AzuriteTelemetryClient.isVSC) // VSC
{
if (AzuriteTelemetryClient.env === undefined)
{
return parameters;
}
let workspaceConfiguration = AzuriteTelemetryClient.env;
if (workspaceConfiguration === undefined)
{
Expand Down Expand Up @@ -497,95 +409,7 @@ export class AzuriteTelemetryClient {
}
});
}

// if (typeof AzuriteTelemetryClient.env?.blobHost === "function" && AzuriteTelemetryClient.env?.blobHost() !== undefined && AzuriteTelemetryClient.env?.blobHost() !== "127.0.0.1")
// {
// parameters += "blobHost,";
// }
// if (typeof AzuriteTelemetryClient.env?.queueHost === "function" && AzuriteTelemetryClient.env?.queueHost() !== undefined && AzuriteTelemetryClient.env?.queueHost() !== "127.0.0.1")
// {
// parameters += "queueHost,";
// }
// if (typeof AzuriteTelemetryClient.env?.tableHost === "function" && AzuriteTelemetryClient.env?.tableHost() !== undefined && AzuriteTelemetryClient.env?.tableHost() !== "127.0.0.1")
// {
// parameters += "tableHost,";
// }
// if (typeof AzuriteTelemetryClient.env?.blobPort === "function" && AzuriteTelemetryClient.env?.blobPort() !== undefined && AzuriteTelemetryClient.env?.blobPort() !== 10000)
// {
// parameters += "blobPort,";
// }
// if (typeof AzuriteTelemetryClient.env?.queuePort === "function" && AzuriteTelemetryClient.env?.queuePort() !== undefined && AzuriteTelemetryClient.env?.queuePort() !== 10001)
// {
// parameters += "queuePort,";
// }
// if (typeof AzuriteTelemetryClient.env?.tablePort === "function" && AzuriteTelemetryClient.env?.tablePort() !== undefined && AzuriteTelemetryClient.env?.tablePort() !== 10002)
// {
// parameters += "tablePort,";
// }
// if (typeof AzuriteTelemetryClient.env?.blobKeepAliveTimeout === "function" && AzuriteTelemetryClient.env?.blobKeepAliveTimeout() !== undefined && AzuriteTelemetryClient.env?.blobKeepAliveTimeout() !== 5)
// {
// parameters += "blobKeepAliveTimeout,";
// }
// if (typeof AzuriteTelemetryClient.env?.queueKeepAliveTimeout === "function" && AzuriteTelemetryClient.env?.queueKeepAliveTimeout() !== undefined && AzuriteTelemetryClient.env?.queueKeepAliveTimeout() !== 5)
// {
// parameters += "queueKeepAliveTimeout,";
// }
// if (typeof AzuriteTelemetryClient.env?.tableKeepAliveTimeout === "function" && AzuriteTelemetryClient.env?.tableKeepAliveTimeout() !== undefined && AzuriteTelemetryClient.env?.tableKeepAliveTimeout() !== 5)
// {
// parameters += "tableKeepAliveTimeout,";
// }
// if (typeof AzuriteTelemetryClient.env?.location === "function" && (await AzuriteTelemetryClient.env?.location()) !== undefined)
// {
// parameters += "location,";
// }
// if (typeof AzuriteTelemetryClient.env?.silent === "function" && AzuriteTelemetryClient.env?.silent())
// {
// parameters += "silent,";
// }
// if (typeof AzuriteTelemetryClient.env?.loose === "function" && AzuriteTelemetryClient.env?.loose())
// {
// parameters += "loose,";
// }
// if (typeof AzuriteTelemetryClient.env?.skipApiVersionCheck === "function" && AzuriteTelemetryClient.env?.skipApiVersionCheck())
// {
// parameters += "skipApiVersionCheck,";
// }
// if (typeof AzuriteTelemetryClient.env?.disableProductStyleUrl === "function" && AzuriteTelemetryClient.env?.disableProductStyleUrl())
// {
// parameters += "disableProductStyleUrl,";
// }
// if (typeof AzuriteTelemetryClient.env?.cert === "function" && AzuriteTelemetryClient.env?.cert() !== undefined)
// {
// parameters += "cert,";
// }
// if (typeof AzuriteTelemetryClient.env?.key === "function" && AzuriteTelemetryClient.env?.key() !== undefined)
// {
// parameters += "key,";
// }
// if (typeof AzuriteTelemetryClient.env?.pwd === "function" && AzuriteTelemetryClient.env?.pwd() !== undefined)
// {
// parameters += "pwd,";
// }
// if (typeof AzuriteTelemetryClient.env?.oauth === "function" && AzuriteTelemetryClient.env?.oauth() !== undefined)
// {
// parameters += "oauth,";
// }
// if (typeof AzuriteTelemetryClient.env?.inMemoryPersistence === "function" && AzuriteTelemetryClient.env?.inMemoryPersistence())
// {
// parameters += "inMemoryPersistence,";
// }
// if (typeof AzuriteTelemetryClient.env?.extentMemoryLimit === "function" && AzuriteTelemetryClient.env?.extentMemoryLimit() !== undefined)
// {
// parameters += "extentMemoryLimit,";
// }
// if (typeof AzuriteTelemetryClient.env?.disableTelemetry === "function" && AzuriteTelemetryClient.env?.disableTelemetry())
// {
// parameters += "disableTelemetry,";
// }
// if (typeof AzuriteTelemetryClient.env?.debug === "function" && (await AzuriteTelemetryClient.env?.debug()) !== undefined)
// {
// parameters += "debug,";
// }

return parameters.endsWith(",") ? parameters.substring(0, parameters.length - 1) : parameters;
}
}
6 changes: 2 additions & 4 deletions src/queue/middlewares/telemetry.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import ExpressRequestAdapter from '../generated/ExpressRequestAdapter';
import ExpressResponseAdapter from '../generated/ExpressResponseAdapter';

/**
* End middleware is used to send out final HTTP response.
* TelemetryMiddleware is used to send telemetry for requests.
*
* @export
* @param {Context} context
* @param {Request} req An express compatible Request object
* @param {Response} res An express compatible Response object
* @param {ILogger} logger A valid logger
* @param {NextFunction} next An express middleware next callback
*/
function telemetryMiddleware(
context: Context,
Expand Down
6 changes: 2 additions & 4 deletions src/table/middleware/telemetry.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import ExpressRequestAdapter from '../generated/ExpressRequestAdapter';
import ExpressResponseAdapter from '../generated/ExpressResponseAdapter';

/**
* End middleware is used to send out final HTTP response.
* TelemetryMiddleware is used to send telemetry for requests.
*
* @export
* @param {Context} context
* @param {Request} req An express compatible Request object
* @param {Response} res An express compatible Response object
* @param {ILogger} logger A valid logger
* @param {NextFunction} next An express middleware next callback
*/
function telemetryMiddleware(
context: Context,
Expand Down

0 comments on commit f4abf9d

Please sign in to comment.