Skip to content

Commit bdf740c

Browse files
[Monitor Ingestion] Beta 1 library for monitor logs ingestion (Azure#22394)
1 parent 562bd45 commit bdf740c

File tree

58 files changed

+4021
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+4021
-0
lines changed

common/config/rush/pnpm-lock.yaml

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rush.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@
511511
"projectFolder": "sdk/monitor/monitor-query",
512512
"versionPolicyName": "client"
513513
},
514+
{
515+
"packageName": "@azure/monitor-ingestion",
516+
"projectFolder": "sdk/monitor/monitor-ingestion",
517+
"versionPolicyName": "client"
518+
},
514519
{
515520
"packageName": "@azure/dev-tool",
516521
"projectFolder": "common/tools/dev-tool",

sdk/monitor/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ extends:
3939
safeName: azuremonitoropentelemetryexporter
4040
- name: azure-monitor-query
4141
safeName: azuremonitorquery
42+
- name: azure-monitor-ingestion
43+
safeName: azuremonitoringestion

sdk/monitor/monitor-ingestion/.nycrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"include": [
3+
"dist-esm/src/**/*.js"
4+
],
5+
"exclude": [
6+
"**/*.d.ts",
7+
"dist-esm/src/generated/*"
8+
],
9+
"reporter": [
10+
"text-summary",
11+
"html",
12+
"cobertura"
13+
],
14+
"exclude-after-remap": false,
15+
"sourceMap": true,
16+
"produce-source-map": true,
17+
"instrument": true,
18+
"all": true
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Release History
2+
3+
## 1.0.0-beta.1 (2022-07-07)
4+
5+
### Features Added
6+
7+
- First beta release for the @azure/monitor-ingestion library.

sdk/monitor/monitor-ingestion/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022 Microsoft
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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+
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js/sdk/monitor/monitor-ingestion/README.png)

0 commit comments

Comments
 (0)