|
| 1 | +# Azure Monitor Ingestion client library for JS |
| 2 | + |
| 3 | +The Azure Monitor Ingestion client library is used to send custom logs to [Azure Monitor][azure_monitor_overview]. |
| 4 | + |
| 5 | +This library allows you to send data from virtually any source to supported built-in tables or to custom tables that you create in Log Analytics workspace. You can even extend the schema of built-in tables with custom columns. |
| 6 | + |
| 7 | +**Resources:** |
| 8 | +* [Source code](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-ingestion/src) |
| 9 | +* [Package (NPM)](https://www.npmjs.com/) |
| 10 | +* [Service documentation][azure_monitor_overview] |
| 11 | +* [Change log](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-ingestion/CHANGELOG.md) |
| 12 | + |
| 13 | +## Getting started |
| 14 | + |
| 15 | +### Prerequisites |
| 16 | + |
| 17 | +- An [Azure subscription](https://azure.microsoft.com/free) |
| 18 | +- A [Data Collection Endpoint](https://docs.microsoft.com/azure/azure-monitor/essentials/data-collection-endpoint-overview) |
| 19 | +- A [Data Collection Rule](https://docs.microsoft.com/azure/azure-monitor/essentials/data-collection-rule-overview) |
| 20 | +- A [Log Analytics workspace](https://docs.microsoft.com/azure/azure-monitor/logs/log-analytics-workspace-overview) |
| 21 | + |
| 22 | +### Install the package |
| 23 | + |
| 24 | +Install the Azure Monitor Ingestion client library for JS with [npm](https://www.npmjs.com/): |
| 25 | + |
| 26 | +```bash |
| 27 | +npm install @azure/monitor-ingestion |
| 28 | +``` |
| 29 | + |
| 30 | +### Authenticate the client |
| 31 | + |
| 32 | +An authenticated client is required to ingest data. To authenticate, create an instance of a [TokenCredential](https://docs.microsoft.com/javascript/api/@azure/core-auth/tokencredential?view=azure-node-latest) class (see [@azure/identity](https://www.npmjs.com/package/@azure/identity) for `DefaultAzureCredential` and other `TokenCredential` implementations). Pass it to the constructor of your client class. |
| 33 | + |
| 34 | +To authenticate, the following example uses `DefaultAzureCredential` from the [@azure/identity](https://www.npmjs.com/package/@azure/identity) package: |
| 35 | + |
| 36 | +```ts |
| 37 | +import { DefaultAzureCredential } from "@azure/identity"; |
| 38 | +import { LogsIngestionClient } from "@azure/monitor-ingestion"; |
| 39 | + |
| 40 | +import * as dotenv from "dotenv"; |
| 41 | +dotenv.config(); |
| 42 | + |
| 43 | +const logsIngestionEndpoint = process.env.LOGS_INGESTION_ENDPOINT || "logs_ingestion_endpoint"; |
| 44 | + |
| 45 | +const credential = new DefaultAzureCredential(); |
| 46 | +const logsIngestionClient = new LogsIngestionClient(logsIngestionEndpoint, credential); |
| 47 | +``` |
| 48 | + |
| 49 | +## Key concepts |
| 50 | + |
| 51 | +### Data Collection Endpoint |
| 52 | + |
| 53 | +Data Collection Endpoints (DCEs) allow you to uniquely configure ingestion settings for Azure Monitor. [This |
| 54 | +article][data_collection_endpoint] provides an overview of data collection endpoints including their contents and |
| 55 | +structure and how you can create and work with them. |
| 56 | + |
| 57 | +### Data Collection Rule |
| 58 | + |
| 59 | +Data collection rules (DCR) define data collected by Azure Monitor and specify how and where that data should be sent or |
| 60 | +stored. The REST API call must specify a DCR to use. A single DCE can support multiple DCRs, so you can specify a |
| 61 | +different DCR for different sources and target tables. |
| 62 | + |
| 63 | +The DCR must understand the structure of the input data and the structure of the target table. If the two don't match, |
| 64 | +it can use a transformation to convert the source data to match the target table. You may also use the transform to |
| 65 | +filter source data and perform any other calculations or conversions. |
| 66 | + |
| 67 | +For more details, refer to [Data collection rules in Azure Monitor][data_collection_rule]. |
| 68 | + |
| 69 | +### Log Analytics workspace tables |
| 70 | + |
| 71 | +Custom logs can send data to any custom table that you create and to certain built-in tables in your Log Analytics |
| 72 | +workspace. The target table must exist before you can send data to it. The following built-in tables are currently supported: |
| 73 | + |
| 74 | +- [CommonSecurityLog](https://docs.microsoft.com/azure/azure-monitor/reference/tables/commonsecuritylog) |
| 75 | +- [SecurityEvents](https://docs.microsoft.com/azure/azure-monitor/reference/tables/securityevent) |
| 76 | +- [Syslog](https://docs.microsoft.com/azure/azure-monitor/reference/tables/syslog) |
| 77 | +- [WindowsEvents](https://docs.microsoft.com/azure/azure-monitor/reference/tables/windowsevent) |
| 78 | + |
| 79 | +## Examples |
| 80 | + |
| 81 | +- [Upload custom logs](#upload-custom-logs) |
| 82 | +- [Verify logs](#verify-logs) |
| 83 | + |
| 84 | +You can familiarize yourself with different APIs using [Samples](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/monitor/Azure.Monitor.Ingestion/samples). |
| 85 | + |
| 86 | +### Upload custom logs |
| 87 | + |
| 88 | +You can create a client and call the client's `Upload` method. Take note of the data ingestion [limits](https://docs.microsoft.com/azure/azure-monitor/service-limits#custom-logs). |
| 89 | + |
| 90 | +```js |
| 91 | +const { DefaultAzureCredential } = require("@azure/identity"); |
| 92 | +const { LogsIngestionClient } = require("@azure/monitor-ingestion"); |
| 93 | + |
| 94 | +require("dotenv").config(); |
| 95 | + |
| 96 | +async function main() { |
| 97 | + const logsIngestionEndpoint = process.env.LOGS_INGESTION_ENDPOINT || "logs_ingestion_endpoint"; |
| 98 | + const ruleId = process.env.DATA_COLLECTION_RULE_ID || "data_collection_rule_id"; |
| 99 | + const streamName = process.env.STREAM_NAME || "data_stream_name"; |
| 100 | + const credential = new DefaultAzureCredential(); |
| 101 | + const client = new LogsIngestionClient(logsIngestionEndpoint, credential); |
| 102 | + const logs = [ |
| 103 | + { |
| 104 | + Time: "2021-12-08T23:51:14.1104269Z", |
| 105 | + Computer: "Computer1", |
| 106 | + AdditionalContext: "context-2", |
| 107 | + }, |
| 108 | + { |
| 109 | + Time: "2021-12-08T23:51:14.1104269Z", |
| 110 | + Computer: "Computer2", |
| 111 | + AdditionalContext: "context", |
| 112 | + }, |
| 113 | + ]; |
| 114 | + const result = await client.upload(ruleId, streamName, logs); |
| 115 | + if (result.uploadStatus !== "Success") { |
| 116 | + console.log("Some logs have failed to complete ingestion. Upload status=", result.uploadStatus); |
| 117 | + for (const errors of result.errors) { |
| 118 | + console.log(`Error - ${JSON.stringify(errors.responseError)}`); |
| 119 | + console.log(`Log - ${JSON.stringify(errors.failedLogs)}`); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | +main().catch((err) => { |
| 124 | + console.error("The sample encountered an error:", err); |
| 125 | + process.exit(1); |
| 126 | +}); |
| 127 | + |
| 128 | +module.exports = { main }; |
| 129 | +``` |
| 130 | +### Verify logs |
| 131 | + |
| 132 | +You can verify that your data has been uploaded correctly by using the [@azure/monitor-query](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-query/README.md#install-the-package) library. Run the [Upload custom logs](#upload-custom-logs) sample first before verifying the logs. |
| 133 | + |
| 134 | +```js |
| 135 | +// Copyright (c) Microsoft Corporation. |
| 136 | +// Licensed under the MIT license. |
| 137 | + |
| 138 | +/** |
| 139 | + * @summary Demonstrates how to run query against a Log Analytics workspace to verify if the logs were uploaded |
| 140 | + */ |
| 141 | + |
| 142 | +const { DefaultAzureCredential } = require("@azure/identity"); |
| 143 | +const { LogsQueryClient } = require("@azure/monitor-query"); |
| 144 | + |
| 145 | +const monitorWorkspaceId = process.env.MONITOR_WORKSPACE_ID || "workspace_id"; |
| 146 | +const tableName = process.env.TABLE_NAME || "table_name"; |
| 147 | +require("dotenv").config(); |
| 148 | + |
| 149 | +async function main() { |
| 150 | + const credential = new DefaultAzureCredential(); |
| 151 | + const logsQueryClient = new LogsQueryClient(credential); |
| 152 | + const queriesBatch = [ |
| 153 | + { |
| 154 | + workspaceId: monitorWorkspaceId, |
| 155 | + query: tableName + " | count;", |
| 156 | + timespan: { duration: "P1D" }, |
| 157 | + }, |
| 158 | + ]; |
| 159 | + |
| 160 | + const result = await logsQueryClient.queryBatch(queriesBatch); |
| 161 | + if (result[0].status === "Success") { |
| 162 | + console.log("Table entry count: ", JSON.stringify(result[0].tables)); |
| 163 | + } else { |
| 164 | + console.log( |
| 165 | + `Some error encountered while retrieving the count. Status = ${result[0].status}`, |
| 166 | + JSON.stringify(result[0]) |
| 167 | + ); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +main().catch((err) => { |
| 172 | + console.error("The sample encountered an error:", err); |
| 173 | + process.exit(1); |
| 174 | +}); |
| 175 | + |
| 176 | +module.exports = { main }; |
| 177 | + |
| 178 | +``` |
| 179 | +## Troubleshooting |
| 180 | + |
| 181 | +### Logging |
| 182 | + |
| 183 | +Enabling logging may help uncover useful information about failures. To see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`: |
| 184 | + |
| 185 | +```ts |
| 186 | +import { setLogLevel } from "@azure/logger"; |
| 187 | + |
| 188 | +setLogLevel("info"); |
| 189 | +``` |
| 190 | + |
| 191 | +For detailed instructions on how to enable logs, see the [@azure/logger package docs](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/core/logger). |
| 192 | + |
| 193 | +## Next steps |
| 194 | +To learn more about Azure Monitor, see the [Azure Monitor service documentation][azure_monitor_overview]. |
| 195 | + |
| 196 | +## Contributing |
| 197 | + |
| 198 | +If you'd like to contribute to this library, please read the [contributing guide](https://github.com/Azure/azure-sdk-for-js/blob/main/CONTRIBUTING.md) to learn more about how to build and test the code. |
| 199 | + |
| 200 | +<!-- LINKS --> |
| 201 | +[azure_monitor_overview]: https://docs.microsoft.com/azure/azure-monitor/overview |
| 202 | +[data_collection_endpoint]: https://docs.microsoft.com/azure/azure-monitor/essentials/data-collection-endpoint-overview |
| 203 | +[data_collection_rule]: https://docs.microsoft.com/azure/azure-monitor/essentials/data-collection-rule-overview |
| 204 | + |
| 205 | + |
0 commit comments