Skip to content

Commit

Permalink
Merge pull request #4 from sxwei123/develop
Browse files Browse the repository at this point in the history
Change response to proper object that contain error code and error message
  • Loading branch information
sxwei123 committed Dec 21, 2020
2 parents 7620393 + 774a9fd commit 9a8ce8c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,12 @@ client
```

To get your `WORKSPACE_ID` and `PRIMARY_KEY_OR_SECONDARY_KEY`, in your workspace go to `advanced settings` -> `connected resources` -> `Agents management`.

## Response Object

| Property | Optional | Description |
| --------- | :------: | ----------------------------------------------------------------------------------------------------------------------: |
| code | no | HTTP response code |
| status | no | HTTP response status |
| errorCode | yes | [Error code](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/data-collector-api#return-codes) from server |
| errorMsg | yes | Error message from server |
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azure-log-analytics-data-collector-client",
"version": "1.2.0",
"version": "2.0.0",
"description": "Node.js wrapper for Azure Log Analytics data collector API",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
10 changes: 10 additions & 0 deletions src/Response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface APIResponse {
// HTTP response code
code: number;
// HTTP response status
status: string;
// error code as documented in https://docs.microsoft.com/en-us/azure/azure-monitor/platform/data-collector-api#return-codes
errorCode?: string;
// error message from server
errorMsg?: string;
}
45 changes: 43 additions & 2 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,54 @@ describe("The data collector client", () => {
);

it("is able to send logs to Azure", async () => {
const statusCode = await client.send("MyLogs", [
const res = await client.send("MyLogs", [
{
level: "info",
message: "server starts",
},
]);

expect(statusCode).toEqual(200);
expect(res.code).toEqual(200);
expect(res.status).toEqual("OK");
});

it("will return MissingLogType error when log type is not provided", async () => {
const res = await client.send("", [
{
level: "info",
message: "server starts",
},
]);

expect(res.code).toEqual(400);
expect(res.errorCode).toEqual("MissingLogType");
});

it("will return InvalidLogType error when log type contains invalid character", async () => {
const res = await client.send("My-Logs", [
{
level: "info",
message: "server starts",
},
]);

expect(res.code).toEqual(400);
expect(res.errorCode).toEqual("InvalidLogType");
});

it("will return InvalidAuthorization error when authorization code is invalid", async () => {
const client = new DataCollectorClient(
LOG_ANALYTICS_WORKSPACE_ID!,
"X" + LOG_ANALYTICS_AGENT_KEY
);
const res = await client.send("MyLogs", [
{
level: "info",
message: "server starts",
},
]);

expect(res.code).toEqual(403);
expect(res.errorCode).toEqual("InvalidAuthorization");
});
});
22 changes: 19 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createHmac } from "crypto";
import fetch from "node-fetch";

import { APIResponse } from "./Response";

// Refer to https://docs.microsoft.com/en-us/azure/azure-monitor/platform/data-collector-api

const API_VERSION = "2016-04-01";
Expand All @@ -23,7 +25,7 @@ export class DataCollectorClient {
logType: string,
logs: any[],
timeGenerated?: string
): Promise<number> {
): Promise<APIResponse> {
const postPayload = JSON.stringify(logs);
const contentLength = Buffer.byteLength(postPayload, "utf8");
const gmtTime = new Date().toUTCString();
Expand Down Expand Up @@ -56,12 +58,26 @@ export class DataCollectorClient {

const url = `https://${this.workspaceId}.ods.opinsights.azure.com/api/logs?api-version=${API_VERSION}`;

const result = await fetch(url, {
const res = await fetch(url, {
method: "post",
body: postPayload,
headers,
});

return result.status;
if (res.status === 200) {
return {
code: 200,
status: "OK",
};
}

const errorDetail = await res.json();

return {
code: res.status,
status: res.statusText,
errorCode: errorDetail.Error,
errorMsg: errorDetail.Message,
};
}
}

0 comments on commit 9a8ce8c

Please sign in to comment.