Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the error handling for Logs Query and Logs Query Batch #17965

Merged
merged 25 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
702a5ec
update the error handling for Query Logs
KarishmaGhiya Sep 30, 2021
39b0865
lint error and ref-docs
KarishmaGhiya Sep 30, 2021
b4ab85e
remove the throwOnAnyError flag
KarishmaGhiya Sep 30, 2021
54404d0
update user agent to be similar for both clients
KarishmaGhiya Sep 30, 2021
8b4c4d1
update sdk_Version
KarishmaGhiya Sep 30, 2021
1397692
update readme
KarishmaGhiya Sep 30, 2021
84ca11e
Update sdk/monitor/monitor-query/README.md
KarishmaGhiya Oct 1, 2021
de48db1
Update sdk/monitor/monitor-query/README.md
KarishmaGhiya Oct 1, 2021
d13224a
Update sdk/monitor/monitor-query/README.md
KarishmaGhiya Oct 1, 2021
3ff87e9
Update sdk/monitor/monitor-query/README.md
KarishmaGhiya Oct 1, 2021
d1596af
Update sdk/monitor/monitor-query/README.md
KarishmaGhiya Oct 1, 2021
61245b5
Update sdk/monitor/monitor-query/README.md
KarishmaGhiya Oct 1, 2021
5d9bb38
Update sdk/monitor/monitor-query/README.md
KarishmaGhiya Oct 1, 2021
ea2e59e
Update sdk/monitor/monitor-query/README.md
KarishmaGhiya Oct 1, 2021
9764d68
Update sdk/monitor/monitor-query/README.md
KarishmaGhiya Oct 1, 2021
beba760
Update sdk/monitor/monitor-query/README.md
KarishmaGhiya Oct 1, 2021
b533deb
Update sdk/monitor/monitor-query/README.md
KarishmaGhiya Oct 1, 2021
cf0657a
Update sdk/monitor/monitor-query/samples-dev/logsQueryBatch.ts
KarishmaGhiya Oct 1, 2021
24cff5c
Update sdk/monitor/monitor-query/samples-dev/logsQueryBatch.ts
KarishmaGhiya Oct 1, 2021
cf2565d
Update sdk/monitor/monitor-query/samples/v1/javascript/logsQueryBatch.js
KarishmaGhiya Oct 1, 2021
36110fa
Update sdk/monitor/monitor-query/src/models/publicLogsModels.ts
KarishmaGhiya Oct 1, 2021
2bf6741
Update sdk/monitor/monitor-query/samples/v1/javascript/logsQueryBatch.js
KarishmaGhiya Oct 1, 2021
5451138
Update sdk/monitor/monitor-query/samples/v1/typescript/src/logsQueryB…
KarishmaGhiya Oct 1, 2021
551fd29
Update sdk/monitor/monitor-query/samples/v1/typescript/src/logsQueryB…
KarishmaGhiya Oct 1, 2021
57d45f4
updated changelog
KarishmaGhiya Oct 1, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 149 additions & 78 deletions sdk/monitor/monitor-query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,26 @@ async function run() {
const result = await logsQueryClient.queryWorkspace(azureLogAnalyticsWorkspaceId, kustoQuery, {
duration: Durations.twentyFourHours
});
const tablesFromResult = result.tables;

if (tablesFromResult == null) {
console.log(`No results for query '${kustoQuery}'`);
return;
}
if (result.status === LogsQueryResultStatus.Success) {
const tablesFromResult: LogsTable[] = result.tables;

console.log(`Results for query '${kustoQuery}'`);
if (tablesFromResult.length === 0) {
console.log(`No results for query '${kustoQuery}'`);
return;
}
console.log(`This query has returned table(s) - `);
processTables(tablesFromResult);
} else {
console.log(`Error processing the query '${kustoQuery}' - ${result.partialError}`);
if (result.partialTables.length > 0) {
console.log(`This query has also returned partial data in the following table(s) - `);
processTables(result.partialTables);
}
}
}

async function processTables(tablesFromResult: LogsTable[]) {
for (const table of tablesFromResult) {
const columnHeaderString = table.columnDescriptors
.map((column) => `${column.name}(${column.type}) `)
Expand All @@ -123,41 +134,57 @@ async function run() {
}
}
}

run().catch((err) => console.log("ERROR:", err));
```

#### Handle logs query response

The `queryWorkspace` function of `LogsQueryClient` returns the `LogsQueryResult`. Here's a hierarchy of the response:
The `queryWorkspace` function of `LogsQueryClient` returns a `LogsQueryResult` object. The object type can be `LogsQuerySuccessfulResult` or `LogsQueryPartialResult`. Here's a hierarchy of the response:

```
LogsQueryResult
LogsQuerySuccessfulResult
|---statistics
|---visualization
|---error
|---status ("PartialFailure" | "Success" | "Failure")
|---status ("Success")
|---tables (list of `LogsTable` objects)
|---name
|---rows
|---columnDescriptors (list of `LogsColumn` objects)
|---name
|---type

KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved
LogsQueryPartialResult
|---statistics
|---visualization
|---status ("PartialFailure")
|---partialError
|--name
|--code
|--message
|--stack
|---partialTables (list of `LogsTable` objects)
|---name
|---rows
|---columnDescriptors (list of `LogsColumn` objects)
|---name
|---type
```

For example, to handle a response with tables:

```ts
const tablesFromResult = result.tables;

for (const table of tablesFromResult) {
const columnHeaderString = table.columnDescriptors
.map((column) => `${column.name}(${column.type}) `)
.join("| ");
console.log("| " + columnHeaderString);
async function processTables(tablesFromResult: LogsTable[]) {
for (const table of tablesFromResult) {
const columnHeaderString = table.columnDescriptors
.map((column) => `${column.name}(${column.type}) `)
.join("| ");
console.log("| " + columnHeaderString);

for (const row of table.rows) {
const columnValuesString = row.map((columnValue) => `'${columnValue}' `).join("| ");
console.log("| " + columnValuesString);
for (const row of table.rows) {
const columnValuesString = row.map((columnValue) => `'${columnValue}' `).join("| ");
console.log("| " + columnValuesString);
}
}
}
```
Expand Down Expand Up @@ -210,89 +237,133 @@ export async function main() {
}

let i = 0;
for (const response of result.results) {
for (const response of result) {
console.log(`Results for query with query: ${queriesBatch[i]}`);

if (response.error) {
console.log(` Query had errors:`, response.error);
if (response.status === LogsQueryResultStatus.Success) {
console.log(
`Printing results from query '${queriesBatch[i].query}' for '${queriesBatch[i].timespan}'`
);
processTables(response.tables);
} else if (response.status === LogsQueryResultStatus.PartialFailure) {
console.log(
`Printing partial results from query '${queriesBatch[i].query}' for '${queriesBatch[i].timespan}'`
);
processTables(response.partialTables);
console.log(
` Query had errors:${response.partialError.message} with code ${response.partialError.code}`
);
} else {
if (response.tables == null) {
console.log(`No results for query`);
} else {
console.log(
`Printing results from query '${queriesBatch[i].query}' for '${queriesBatch[i].timespan}'`
);

for (const table of response.tables) {
const columnHeaderString = table.columnDescriptors
.map((column) => `${column.name}(${column.type}) `)
.join("| ");
console.log(columnHeaderString);

for (const row of table.rows) {
const columnValuesString = row.map((columnValue) => `'${columnValue}' `).join("| ");
console.log(columnValuesString);
}
}
}
console.log(`Printing errors from query '${queriesBatch[i].query}'`);
console.log(` Query had errors:${response.message} with code ${response.code}`);
}
// next query
i++;
}
}

async function processTables(tablesFromResult: LogsTable[]) {
for (const table of tablesFromResult) {
const columnHeaderString = table.columnDescriptors
.map((column) => `${column.name}(${column.type}) `)
.join("| ");
console.log("| " + columnHeaderString);

for (const row of table.rows) {
const columnValuesString = row.map((columnValue) => `'${columnValue}' `).join("| ");
console.log("| " + columnValuesString);
}
}
}
```

#### Handle logs batch query response

The `queryBatch` function of `LogsQueryClient` returns a `LogsQueryBatchResult` object. Here's a hierarchy of the response:
The `queryBatch` function of `LogsQueryClient` returns a `LogsQueryBatchResult` object. `LogsQueryBatchResult` contains a list of objects with the following possible types:

- `LogsQueryPartialResult`
- `LogsQuerySuccessfulResult`
- `LogsQueryError`

Here's a hierarchy of the response:

```
LogsQueryBatchResult
|---results (list of following objects)
|---statistics
|---visualization
|---error
|---status ("PartialFailure" | "Success" | "Failure")
|---tables (list of `LogsTable` objects)

LogsQuerySuccessfulResult
|---statistics
|---visualization
|---status ("Success")
|---tables (list of `LogsTable` objects)
|---name
|---rows
|---columnDescriptors (list of `LogsColumn` objects)
|---name
|---rows
|---columnDescriptors (list of `LogsColumn` objects)
|---name
|---type
|---type

KarishmaGhiya marked this conversation as resolved.
Show resolved Hide resolved
LogsQueryPartialResult
|---statistics
|---visualization
|---status ("PartialFailure")
|---partialError
|--name
|--code
|--message
|--stack
|---partialTables (list of `LogsTable` objects)
|---name
|---rows
|---columnDescriptors (list of `LogsColumn` objects)
|---name
|---type

LogsQueryError
|--name
|--code
|--message
|--stack
|--status ("Failure")
```

For example, to handle a batch logs query response:
For example, the following code handles a batch logs query response:

```ts
let i = 0;
for (const response of result.results) {
console.log(`Results for query with query: ${queriesBatch[i]}`);

if (response.error) {
console.log(` Query had errors:`, response.error);
} else {
if (response.tables == null) {
console.log(`No results for query`);
} else {
async function processBatchResult(result: LogsQueryBatchResult) {
let i = 0;
for (const response of result) {
console.log(`Results for query with query: ${queriesBatch[i]}`);
if (response.status === LogsQueryResultStatus.Success) {
console.log(
`Printing results from query '${queriesBatch[i].query}' for '${queriesBatch[i].timespan}'`
);
processTables(response.tables);
} else if (response.status === LogsQueryResultStatus.PartialFailure) {
console.log(
`Printing partial results from query '${queriesBatch[i].query}' for '${queriesBatch[i].timespan}'`
);
processTables(response.partialTables);
console.log(
` Query had errors:${response.partialError.message} with code ${response.partialError.code}`
);
} else {
console.log(`Printing errors from query '${queriesBatch[i].query}'`);
console.log(` Query had errors:${response.message} with code ${response.code}`);
}
// next query
i++;
}
}

for (const table of response.tables) {
const columnHeaderString = table.columnDescriptors
.map((column) => `${column.name}(${column.type}) `)
.join("| ");
console.log(columnHeaderString);
async function processTables(tablesFromResult: LogsTable[]) {
for (const table of tablesFromResult) {
const columnHeaderString = table.columnDescriptors
.map((column) => `${column.name}(${column.type}) `)
.join("| ");
console.log("| " + columnHeaderString);

for (const row of table.rows) {
const columnValuesString = row.map((columnValue) => `'${columnValue}' `).join("| ");
console.log(columnValuesString);
}
}
for (const row of table.rows) {
const columnValuesString = row.map((columnValue) => `'${columnValue}' `).join("| ");
console.log("| " + columnValuesString);
}
}
// next query
i++;
}
```

Expand Down
55 changes: 27 additions & 28 deletions sdk/monitor/monitor-query/review/monitor-query.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,9 @@ import { OperationOptions } from '@azure/core-client';
import { PagedAsyncIterableIterator } from '@azure/core-paging';
import { TokenCredential } from '@azure/core-auth';

// @public
export class AggregateBatchError extends Error {
constructor(errors: LogsErrorInfo[]);
errors: BatchError[];
}

// @public
export type AggregationType = "None" | "Average" | "Count" | "Minimum" | "Maximum" | "Total";

// @public
export class BatchError extends Error implements LogsErrorInfo {
constructor(errorInfo: LogsErrorInfo);
code: string;
}

// @public
export const Durations: {
readonly sevenDays: "P7D";
Expand Down Expand Up @@ -64,19 +52,10 @@ export interface LogsErrorInfo extends Error {

// @public
export interface LogsQueryBatchOptions extends OperationOptions {
throwOnAnyFailure?: boolean;
}

// @public
export interface LogsQueryBatchResult {
results: {
tables?: LogsTable[];
error?: LogsErrorInfo;
status?: LogsQueryResultStatus;
statistics?: Record<string, unknown>;
visualization?: Record<string, unknown>;
}[];
}
export type LogsQueryBatchResult = Array<LogsQueryPartialResult | LogsQuerySuccessfulResult | LogsQueryError>;

// @public
export class LogsQueryClient {
Expand All @@ -91,26 +70,46 @@ export interface LogsQueryClientOptions extends CommonClientOptions {
endpoint?: string;
}

// @public
export interface LogsQueryError extends Error {
code: string;
status: LogsQueryResultStatus.Failure;
}

// @public
export interface LogsQueryOptions extends OperationOptions {
additionalWorkspaces?: string[];
includeQueryStatistics?: boolean;
includeVisualization?: boolean;
serverTimeoutInSeconds?: number;
throwOnAnyFailure?: boolean;
}

// @public
export interface LogsQueryResult {
error?: LogsErrorInfo;
export interface LogsQueryPartialResult {
partialError: LogsErrorInfo;
partialTables: LogsTable[];
statistics?: Record<string, unknown>;
status: LogsQueryResultStatus;
tables: LogsTable[];
status: LogsQueryResultStatus.PartialFailure;
visualization?: Record<string, unknown>;
}

// @public
export type LogsQueryResultStatus = "PartialFailure" | "Success" | "Failure";
export type LogsQueryResult = LogsQuerySuccessfulResult | LogsQueryPartialResult;

// @public
export enum LogsQueryResultStatus {
Failure = "Failure",
PartialFailure = "PartialFailure",
Success = "Success"
}

// @public
export interface LogsQuerySuccessfulResult {
statistics?: Record<string, unknown>;
status: LogsQueryResultStatus.Success;
tables: LogsTable[];
visualization?: Record<string, unknown>;
}

// @public
export interface LogsTable {
Expand Down
Loading