Skip to content

Commit eb44449

Browse files
[monitor-query] Addressing last bits of feedback for 1.0.0-beta.1 (Azure#15578)
Addressing more of the API review and feature crew feedback: - Rename CommonDurations to Durations, Logs and Metrics clients are now LogsQueryClient and MetricsQueryClient, respectively. - Make timespan a required parameter (request from the service team) - Fixing some cosmetic naming in the samples. We have three parameters now and they're all strings... - Deserialize `dynamic` and `Date` properties into their native types, when possible. Also, - fixed type definition for 'Table.rows' so it accomodates the wider variety of primitives it deserializes (Date, number (int, long, real), boolean and dynamic). - LocalizableStrings are now just normal `string`'s. - Rename the return objects from the operations to be `*Result` rather than `*Response`. And some other general improvements: - Add in a reliable test for server timeout using a query that Pavel cooked up. - Fixed any outstanding lint issues and made lint a required build step so they don't sneak back in. And making some things a bit less specified: - Replace statistics with 'any' until we figure out how we want to map the schema. - Leaving MetadataValue in place until I find the right replacement for it. Fixing most of Azure#15296
1 parent 64bfeb5 commit eb44449

24 files changed

+978
-525
lines changed

sdk/monitor/monitor-query/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Instructions:
3737

3838
### Authenticate the client
3939

40-
[LogsClient]<!--[msdocs_logs_client]--> and [MetricsClient]<!--[msdocs_metrics_client]--> authenticate using a [service principal](#authenticating-with-a-service-principal).
40+
[LogsQueryClient]<!--[msdocs_logs_client]--> and [MetricsQueryClient]<!--[msdocs_metrics_client]--> authenticate using a [service principal](#authenticating-with-a-service-principal).
4141

4242
#### Authenticating with a service principal
4343

@@ -51,40 +51,40 @@ Using [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-js/blob/ma
5151

5252
```javascript
5353
const { DefaultAzureCredential } = require("@azure/identity");
54-
const { LogsClient, MetricsClient } = require("@azure/monitor-query");
54+
const { LogsQueryClient, MetricsQueryClient } = require("@azure/monitor-query");
5555

5656
const credential = new DefaultAzureCredential();
5757

58-
const logsClient = new LogsClient(credential);
58+
const logsQueryClient = new LogsQueryClient(credential);
5959
// or
60-
const metricsClient = new MetricsClient(credential);
60+
const metricsQueryClient = new MetricsQueryClient(credential);
6161
```
6262

6363
More information about `@azure/identity` can be found [here](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/identity/identity/README.md)
6464

6565
## Key concepts
6666

67-
The [`LogsClient`]<!--[msdocs_logs_client]--> allows you to query logs, using the [Kusto query language][kusto_query_language]. This data can be queried in the
67+
The [`LogsQueryClient`]<!--[msdocs_logs_client]--> allows you to query logs, using the [Kusto query language][kusto_query_language]. This data can be queried in the
6868
portal using tables like `AppEvents`, `AppDependencies` and others.
6969

70-
The [`MetricsClient`]<!--[msdocs_metrics_client]--> allows you to query metrics.
70+
The [`MetricsQueryClient`]<!--[msdocs_metrics_client]--> allows you to query metrics.
7171

7272
## Examples
7373

7474
#### Querying logs
7575

76-
The LogsClient can be used to query a Monitor workspace using the Kusto Query language.
76+
The `LogsQueryClient` can be used to query a Monitor workspace using the Kusto Query language.
7777

7878
```javascript
79-
const { LogsClient } = require("@azure/monitor-query");
79+
const { LogsQueryClient } = require("@azure/monitor-query");
8080
const { DefaultAzureCredential } = require("@azure/identity");
8181

8282
const azureLogAnalyticsWorkspaceId = "<the Workspace Id for your Azure Log Analytics resource>";
83-
const logsClient = new LogsClient(new DefaultAzureCredential());
83+
const logsQueryClient = new LogsQueryClient(new DefaultAzureCredential());
8484

8585
async function run() {
8686
const kustoQuery = "AppEvents | limit 1";
87-
const result = logsClient.queryLogs(azureLogAnalyticsWorkspaceId, kustoQuery);
87+
const result = await logsQueryClient.queryLogs(azureLogAnalyticsWorkspaceId, kustoQuery);
8888
const tablesFromResult = result.tables;
8989

9090
if (tablesFromResult == null) {
@@ -166,8 +166,8 @@ folder for more details.
166166
[azure_monitor_create_using_cli]: https://docs.microsoft.com/azure/azure-monitor/logs/quick-create-workspace-cli
167167
[azure_monitor_roles]: https://docs.microsoft.com/azure/azure-monitor/roles-permissions-security
168168
[kusto_query_language]: https://docs.microsoft.com/azure/data-explorer/kusto/query/
169-
[msdocs_metrics_client]: https://docs.microsoft.com/javascript/api/@azure/monitor-query/metricsclient
170-
[msdocs_logs_client]: https://docs.microsoft.com/javascript/api/@azure/monitor-query/logsclient
169+
[msdocs_metrics_client]: https://docs.microsoft.com/javascript/api/@azure/monitor-query/metricsqueryclient
170+
[msdocs_logs_client]: https://docs.microsoft.com/javascript/api/@azure/monitor-query/logsqueryclient
171171
[samples]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/monitor/monitor-query/samples
172172
[samples_logsquery_ts]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/monitor/monitor-query/samples/v1/typescript/src/logQuery.ts
173173
[samples_logquerybatch_ts]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/monitor/monitor-query/samples/v1/typescript/src/logQueryBatch.ts

sdk/monitor/monitor-query/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"integration-test:node": "nyc mocha -r esm --require source-map-support/register --slow 5000 --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 5000000 --full-trace \"dist-esm/test/{,!(browser)/**/}/*.spec.js\"",
6363
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
6464
"lint:fix": "eslint package.json api-extractor.json src test --ext .ts --fix --fix-type [problem,suggestion]",
65-
"lint": "eslint package.json api-extractor.json src test --format unix --ext .ts",
65+
"lint": "eslint package.json api-extractor.json src test --format unix --ext .ts --max-warnings 0",
6666
"pack": "npm pack 2>&1",
6767
"prebuild": "npm run clean",
6868
"test:browser": "npm run build:test && npm run integration-test:browser",

sdk/monitor/monitor-query/review/monitor-query.api.md

Lines changed: 44 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface BatchQuery {
1818
qualifiedNames?: string[];
1919
query: string;
2020
serverTimeoutInSeconds?: number;
21-
timespan?: string;
21+
timespan: string;
2222
workspace: string;
2323
workspaceIds?: string[];
2424
workspaces?: string[];
@@ -28,7 +28,7 @@ export interface BatchQuery {
2828
export type ColumnDataType = string;
2929

3030
// @public
31-
export const CommonDurations: {
31+
export const Durations: {
3232
readonly last7Days: "P7D";
3333
readonly last3Days: "P3D";
3434
readonly last2Days: "P2D";
@@ -66,7 +66,7 @@ export interface GetMetricDefinitionsOptions extends OperationOptions {
6666
}
6767

6868
// @public
69-
export interface GetMetricDefinitionsResponse {
69+
export interface GetMetricDefinitionsResult {
7070
definitions: MetricDefinition[];
7171
}
7272

@@ -76,33 +76,38 @@ export interface GetMetricNamespacesOptions {
7676
}
7777

7878
// @public
79-
export interface GetMetricNamespacesResponse {
79+
export interface GetMetricNamespacesResult {
8080
namespaces: MetricNamespace[];
8181
}
8282

8383
// @public
84-
export interface LocalizableString {
85-
localizedValue?: string;
86-
value: string;
84+
export interface LogsColumn {
85+
name?: string;
86+
type?: ColumnDataType;
8787
}
8888

8989
// @public
90-
export class LogsClient {
91-
constructor(tokenCredential: TokenCredential, options?: LogsClientOptions);
92-
// (undocumented)
93-
queryLogs(workspaceId: string, query: string, options?: QueryLogsOptions): Promise<QueryLogsResult>;
94-
// (undocumented)
95-
queryLogsBatch(batch: QueryLogsBatch, options?: QueryLogsBatchOptions): Promise<QueryLogsBatchResponse>;
90+
export class LogsQueryClient {
91+
constructor(tokenCredential: TokenCredential, options?: LogsQueryClientOptions);
92+
queryLogs(workspaceId: string, query: string, timespan: string, options?: QueryLogsOptions): Promise<QueryLogsResult>;
93+
queryLogsBatch(batch: QueryLogsBatch, options?: QueryLogsBatchOptions): Promise<QueryLogsBatchResult>;
9694
}
9795

98-
// @public (undocumented)
99-
export interface LogsClientOptions extends PipelineOptions {
96+
// @public
97+
export interface LogsQueryClientOptions extends PipelineOptions {
10098
endpoint?: string;
10199
}
102100

101+
// @public
102+
export interface LogsTable {
103+
columns: LogsColumn[];
104+
name: string;
105+
rows: (Date | string | number | Record<string, unknown> | boolean)[][];
106+
}
107+
103108
// @public
104109
export interface MetadataValue {
105-
name?: LocalizableString;
110+
name?: string;
106111
value?: string;
107112
}
108113

@@ -111,7 +116,7 @@ export interface Metric {
111116
displayDescription: string;
112117
errorCode?: string;
113118
id: string;
114-
name: LocalizableString;
119+
name: string;
115120
timeseries: TimeSeriesElement[];
116121
type: string;
117122
unit: MetricUnit;
@@ -123,26 +128,18 @@ export interface MetricAvailability {
123128
timeGrain?: string;
124129
}
125130

126-
// @public
127-
export interface MetricColumn {
128-
name?: string;
129-
type?: ColumnDataType;
130-
}
131-
132131
// @public
133132
export interface MetricDefinition {
134133
category?: string;
135-
dimensions?: LocalizableString_2[];
134+
dimensions?: string[];
136135
displayDescription?: string;
137136
id?: string;
138137
isDimensionRequired?: boolean;
139138
metricAvailabilities?: MetricAvailability[];
140-
// Warning: (ae-forgotten-export) The symbol "LocalizableString" needs to be exported by the entry point index.d.ts
141-
name?: LocalizableString_2;
139+
name?: string;
142140
primaryAggregationType?: AggregationType;
143141
resourceId?: string;
144-
// Warning: (ae-forgotten-export) The symbol "MetricUnit" needs to be exported by the entry point index.d.ts
145-
unit?: MetricUnit_2;
142+
unit?: MetricUnit;
146143
}
147144

148145
// @public
@@ -159,22 +156,19 @@ export interface MetricNamespaceName {
159156
metricNamespaceName?: string;
160157
}
161158

162-
// @public (undocumented)
163-
export class MetricsClient {
164-
constructor(tokenCredential: TokenCredential, options?: MetricsClientOptions);
165-
// (undocumented)
166-
getMetricDefinitions(resourceUri: string, options?: GetMetricDefinitionsOptions): Promise<GetMetricDefinitionsResponse>;
167-
// (undocumented)
168-
getMetricNamespaces(resourceUri: string, options?: GetMetricNamespacesOptions): Promise<GetMetricNamespacesResponse>;
169-
// (undocumented)
170-
queryMetrics(resourceUri: string, options?: QueryMetricsOptions): Promise<QueryMetricsResponse>;
171-
}
172-
173-
// @public (undocumented)
159+
// @public
174160
export interface MetricsClientOptions extends PipelineOptions {
175161
endpoint?: string;
176162
}
177163

164+
// @public
165+
export class MetricsQueryClient {
166+
constructor(tokenCredential: TokenCredential, options?: MetricsClientOptions);
167+
getMetricDefinitions(resourceUri: string, options?: GetMetricDefinitionsOptions): Promise<GetMetricDefinitionsResult>;
168+
getMetricNamespaces(resourceUri: string, options?: GetMetricNamespacesOptions): Promise<GetMetricNamespacesResult>;
169+
queryMetrics(resourceUri: string, timespan: string, options?: QueryMetricsOptions): Promise<QueryMetricsResult>;
170+
}
171+
178172
// @public
179173
export type MetricUnit = string;
180174

@@ -196,30 +190,30 @@ export interface QueryLogsBatch {
196190
queries: BatchQuery[];
197191
}
198192

199-
// @public (undocumented)
193+
// @public
200194
export type QueryLogsBatchOptions = OperationOptions;
201195

202196
// @public
203-
export interface QueryLogsBatchResponse {
197+
export interface QueryLogsBatchResult {
204198
results?: {
205199
id?: string;
206200
status?: number;
207-
tables?: Table[];
201+
tables?: LogsTable[];
208202
error?: ErrorInfo;
209203
}[];
210204
}
211205

212-
// @public (undocumented)
206+
// @public
213207
export interface QueryLogsOptions extends OperationOptions {
214208
includeQueryStatistics?: boolean;
215209
serverTimeoutInSeconds?: number;
216-
timespan?: string;
217210
}
218211

219-
// @public (undocumented)
220-
export type QueryLogsResult = QueryResults & {
221-
statistics?: QueryStatistics;
222-
};
212+
// @public
213+
export interface QueryLogsResult {
214+
statistics?: any;
215+
tables: LogsTable[];
216+
}
223217

224218
// @public
225219
export interface QueryMetricsOptions extends OperationOptions {
@@ -230,12 +224,11 @@ export interface QueryMetricsOptions extends OperationOptions {
230224
metricNamespace?: string;
231225
orderBy?: string;
232226
resultType?: ResultType;
233-
timespan?: string;
234227
top?: number;
235228
}
236229

237230
// @public
238-
export interface QueryMetricsResponse {
231+
export interface QueryMetricsResult {
239232
cost?: number;
240233
interval?: string;
241234
metrics: Metric[];
@@ -244,33 +237,9 @@ export interface QueryMetricsResponse {
244237
timespan: string;
245238
}
246239

247-
// @public
248-
export interface QueryResults {
249-
statistics?: any;
250-
tables: Table[];
251-
}
252-
253-
// @public (undocumented)
254-
export interface QueryStatistics {
255-
// (undocumented)
256-
[key: string]: unknown;
257-
// (undocumented)
258-
query?: {
259-
executionTime?: number;
260-
[key: string]: unknown;
261-
};
262-
}
263-
264240
// @public
265241
export type ResultType = "Data" | "Metadata";
266242

267-
// @public
268-
export interface Table {
269-
columns: MetricColumn[];
270-
name: string;
271-
rows: string[][];
272-
}
273-
274243
// @public
275244
export interface TimeSeriesElement {
276245
data?: MetricValue[];

sdk/monitor/monitor-query/samples-dev/logsQuery.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,50 @@
66
*/
77

88
import { DefaultAzureCredential } from "@azure/identity";
9-
import { CommonDurations, LogsClient, Table } from "@azure/monitor-query";
9+
import { Durations, LogsQueryClient, LogsTable } from "@azure/monitor-query";
1010
import * as dotenv from "dotenv";
1111
dotenv.config();
1212

1313
const monitorWorkspaceId = process.env.MONITOR_WORKSPACE_ID;
1414

1515
export async function main() {
1616
const tokenCredential = new DefaultAzureCredential();
17-
const logsClient = new LogsClient(tokenCredential);
17+
const logsQueryClient = new LogsQueryClient(tokenCredential);
1818

1919
if (!monitorWorkspaceId) {
2020
throw new Error("MONITOR_WORKSPACE_ID must be set in the environment for this sample");
2121
}
2222

23-
const kqlQuery =
23+
const kustoQuery =
2424
"AppEvents | project TimeGenerated, Name, AppRoleInstance | order by TimeGenerated asc | limit 10";
2525

26-
console.log(`Running '${kqlQuery}' for the last 5 minutes`);
27-
const result = await logsClient.queryLogs(monitorWorkspaceId, kqlQuery, {
26+
console.log(`Running '${kustoQuery}' over the last 5 minutes`);
27+
const result = await logsQueryClient.queryLogs(
28+
monitorWorkspaceId,
29+
kustoQuery,
2830
// The timespan is an ISO8601 formatted time (or interval). Some common aliases
2931
// are available (like lastDay, lastHour, last48Hours, etc..) but any properly formatted ISO8601
3032
// value is valid.
31-
timespan: CommonDurations.last5Minutes,
33+
Durations.last5Minutes,
34+
{
35+
// optionally enable returning additional statistics about the query's execution.
36+
// (by default this is off)
37+
includeQueryStatistics: true,
3238

33-
// optionally enable returning additional statistics about the query's execution.
34-
// (by default this is off)
35-
includeQueryStatistics: true,
36-
37-
// explicitly control the amount of time the server can spend processing the query.
38-
serverTimeoutInSeconds: 60
39-
});
39+
// explicitly control the amount of time the server can spend processing the query.
40+
serverTimeoutInSeconds: 60
41+
}
42+
);
4043

41-
const tablesFromResult: Table[] | undefined = result.tables;
44+
const tablesFromResult: LogsTable[] | undefined = result.tables;
4245

4346
if (tablesFromResult == null) {
44-
console.log(`No results for query '${kqlQuery}'`);
47+
console.log(`No results for query '${kustoQuery}'`);
4548
return;
4649
}
4750

4851
console.log(
49-
`Results for query '${kqlQuery}', execution time: ${result.statistics?.query?.executionTime}`
52+
`Results for query '${kustoQuery}', execution time: ${result.statistics?.query?.executionTime}`
5053
);
5154

5255
for (const table of tablesFromResult) {

0 commit comments

Comments
 (0)