Skip to content

Commit 33dfd6c

Browse files
authored
expand examples (Azure#19208)
* start docs * save change * update readme samples * fix typo * fix broken link * fix second link * example file * examples links in readme * panic to todo error
1 parent c4b71e1 commit 33dfd6c

File tree

2 files changed

+66
-40
lines changed

2 files changed

+66
-40
lines changed

sdk/monitor/azquery/README.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ import (
4242
func main() {
4343
cred, err := azidentity.NewDefaultAzureCredential(nil)
4444
if err != nil {
45-
panic(err)
45+
//TODO: handle error
4646
}
4747

48-
client := azkeys.NewLogsClient(cred, nil)
48+
client := azquery.NewLogsClient(cred, nil)
4949
}
5050
```
5151

@@ -60,16 +60,16 @@ import (
6060
func main() {
6161
cred, err := azidentity.NewDefaultAzureCredential(nil)
6262
if err != nil {
63-
panic(err)
63+
//TODO: handle error
6464
}
6565

66-
client := azkeys.NewMetricsClient(cred, nil)
66+
client := azquery.NewMetricsClient(cred, nil)
6767
}
6868
```
6969

7070
### Execute the query
7171

72-
For examples of Logs and Metrics queries, see the [Examples](#examples) section.
72+
For examples of Logs and Metrics queries, see the [Examples](#examples) section of this readme or in the example_test.go file of our GitHub repo for [azquery](https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/monitor/azquery).
7373

7474
## Key concepts
7575

@@ -117,55 +117,47 @@ The timespan can be the following string formats:
117117
- [Metrics result structure](#metrics-result-structure)
118118

119119
### Logs query
120+
The example below shows a basic logs query using the `QueryWorkspace` method. `QueryWorkspace` takes in a [context][context], a [Log Analytics Workspace][log_analytics_workspace] ID string, a [Body](#logs-query-body-structure) struct, and a [LogsClientQueryWorkspaceOptions](#increase-wait-time-include-statistics-include-render-visualization) struct and returns a [Results](#logs-query-result-structure) struct.
121+
120122
```go
121-
client := azquery.NewLogsClient(cred, nil)
122-
timespan := "2022-08-30/2022-08-31"
123+
workspaceID := "g4d1e129-fb1e-4b0a-b234-250abc987ea65" // example Azure Log Analytics Workspace ID
124+
query := "AzureActivity | top 10 by TimeGenerated" // Kusto query
125+
timespan := "2022-08-30/2022-08-31" // ISO8601 Standard timespan
123126

124127
res, err := client.QueryWorkspace(context.TODO(), workspaceID, azquery.Body{Query: to.Ptr(query), Timespan: to.Ptr(timespan)}, nil)
125128
if err != nil {
126-
panic(err)
129+
//TODO: handle error
127130
}
128131
_ = res
129132
```
133+
full example: [link][example_query_workspace]
130134

131135
#### Logs query body structure
132136
```
133137
Body
134138
|---Query *string // Kusto Query
135-
|---Timespan *string // ISO8601 Standard Timespan
139+
|---Timespan *string // ISO8601 Standard Timespan- refer to timespan section for more info
136140
|---Workspaces []*string //Optional- additional workspaces to query
137141
```
138142

139143
#### Logs query result structure
140144
```
141-
LogsResponse
145+
Results
142146
|---Tables []*Table
143147
|---Columns []*Column
144148
|---Name *string
145149
|---Type *LogsColumnType
146150
|---Name *string
147151
|---Rows [][]interface{}
148152
|---Error *ErrorInfo
149-
|---Code *string
150-
|---Message *string
151-
|---AdditionalProperties interface{}
152-
|---Details []*ErrorDetail
153-
|---Code *string
154-
|---Message *string
155-
|---AdditionalProperties interface{}
156-
|---Resources []*string
157-
|---Target *string
158-
|---Value *string
159-
|---Innererror *ErrorInfo
160153
|---Render interface{}
161154
|---Statistics interface{}
162155
```
163156

164157
### Batch query
158+
`Batch` is an advanced method allowing users to execute multiple logs queries in a single request. It takes in a [BatchRequest](#batch-query-request-structure) and returns a [BatchResponse](#batch-query-result-structure). `Batch` can return results in any order (usually in order of completion/success). Please use the `ID` attribute to identify the correct response.
165159
```go
166-
client := azquery.NewLogsClient(cred, nil)
167-
timespan := "2022-08-30/2022-08-31"
168-
160+
timespan := "2022-08-30/2022-08-31" // ISO8601 Standard Timespan
169161
batchRequest := azquery.BatchRequest{[]*azquery.BatchQueryRequest{
170162
{Body: &azquery.Body{Query: to.Ptr(kustoQuery1), Timespan: to.Ptr(timespan)}, ID: to.Ptr("1"), Workspace: to.Ptr(workspaceID)},
171163
{Body: &azquery.Body{Query: to.Ptr(kustoQuery2), Timespan: to.Ptr(timespan)}, ID: to.Ptr("2"), Workspace: to.Ptr(workspaceID)},
@@ -174,10 +166,11 @@ batchRequest := azquery.BatchRequest{[]*azquery.BatchQueryRequest{
174166

175167
res, err := client.Batch(context.TODO(), batchRequest, nil)
176168
if err != nil {
177-
panic(err)
169+
//TODO: handle error
178170
}
179171
_ = res
180172
```
173+
full example: [link][example_batch]
181174

182175
#### Batch query request structure
183176

@@ -229,7 +222,7 @@ additionalWorkspaces := []*string{&workspaceID2, &workspaceID3}
229222

230223
res, err := client.QueryWorkspace(context.TODO(), workspaceID, azquery.Body{Query: to.Ptr(query), Timespan: to.Ptr(timespan), Workspaces: additionalWorkspaces}, nil)
231224
if err != nil {
232-
panic(err)
225+
//TODO: handle error
233226
}
234227
_ = res
235228
```
@@ -251,7 +244,7 @@ options := &azquery.LogsClientQueryWorkspaceOptions{Prefer: &prefer}
251244
res, err := client.QueryWorkspace(context.TODO(), workspaceID,
252245
azquery.Body{Query: to.Ptr(query), Timespan: to.Ptr(timespan)}, options)
253246
if err != nil {
254-
panic(err)
247+
//TODO: handle error
255248
}
256249
_ = res
257250
```
@@ -272,14 +265,14 @@ res, err := client.QueryResource(context.Background(), resourceURI,
272265
Metricnamespace: to.Ptr("Microsoft.Storage/storageAccounts/blobServices"),
273266
})
274267
if err != nil {
275-
panic(err)
268+
//TODO: handle error
276269
}
277270
_ = res
278271
```
279272

280273
#### Metrics result structure
281274
```
282-
MetricsResults
275+
Response
283276
|---Timespan *string
284277
|---Value []*Metric
285278
|---ID *string
@@ -342,10 +335,17 @@ comments.
342335

343336
<!-- LINKS -->
344337
[managed_identity]: https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/overview
338+
[azquery]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/monitor/azquery
345339
[azure_identity]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity
346340
[azure_sub]: https://azure.microsoft.com/free/
347341
[azure_monitor_create_using_portal]: https://docs.microsoft.com/azure/azure-monitor/logs/quick-create-workspace
348342
[azure_monitor_overview]: https://docs.microsoft.com/azure/azure-monitor/overview
343+
[context]: https://pkg.go.dev/context
344+
[example_batch]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery#example-LogsClient.Batch
345+
[example_query_workspace]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery#example-LogsClient.QueryWorkspace
346+
[kusto_query_language]: https://learn.microsoft.com/azure/data-explorer/kusto/query/
347+
[log_analytics_workspace]: https://learn.microsoft.com/azure/azure-monitor/logs/log-analytics-workspace-overview
348+
[time_go]: https://pkg.go.dev/time
349349
[time_intervals]: https://en.wikipedia.org/wiki/ISO_8601#Time_intervals
350350

351351
[cla]: https://cla.microsoft.com

sdk/monitor/azquery/example_test.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package azquery_test
88

99
import (
1010
"context"
11+
"fmt"
1112

1213
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
1314
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
@@ -22,7 +23,7 @@ var kustoQuery3 string
2223
func ExampleNewLogsClient() {
2324
cred, err := azidentity.NewDefaultAzureCredential(nil)
2425
if err != nil {
25-
panic(err)
26+
//TODO: handle error
2627
}
2728

2829
client := azquery.NewLogsClient(cred, nil)
@@ -32,28 +33,46 @@ func ExampleNewLogsClient() {
3233
func ExampleNewMetricsClient() {
3334
cred, err := azidentity.NewDefaultAzureCredential(nil)
3435
if err != nil {
35-
panic(err)
36+
//TODO: handle error
3637
}
3738

3839
client := azquery.NewMetricsClient(cred, nil)
3940
_ = client
4041
}
4142

4243
func ExampleLogsClient_QueryWorkspace() {
44+
cred, err := azidentity.NewDefaultAzureCredential(nil)
45+
if err != nil {
46+
//TODO: handle error
47+
}
4348
client := azquery.NewLogsClient(cred, nil)
44-
timespan := "2022-08-30/2022-08-31"
49+
workspaceID := "g4d1e129-fb1e-4b0a-b234-250abc987ea65" // example Azure Log Analytics Workspace ID
50+
query := "AzureActivity | top 10 by TimeGenerated" // Kusto query
51+
timespan := "2022-08-30/2022-08-31" // ISO8601 Standard timespan
4552

46-
res, err := client.QueryWorkspace(context.TODO(), workspaceID,
47-
azquery.Body{Query: to.Ptr(query), Timespan: to.Ptr(timespan)}, nil)
53+
res, err := client.QueryWorkspace(context.TODO(), workspaceID, azquery.Body{Query: to.Ptr(query), Timespan: to.Ptr(timespan)}, nil)
4854
if err != nil {
49-
panic(err)
55+
//TODO: handle error
56+
}
57+
if res.Results.Error != nil {
58+
//TODO: handle partial error
59+
}
60+
61+
table := res.Results.Tables[0]
62+
fmt.Println("Response rows:")
63+
for _, row := range table.Rows {
64+
fmt.Println(row)
5065
}
51-
_ = res
5266
}
5367

5468
func ExampleLogsClient_Batch() {
69+
cred, err := azidentity.NewDefaultAzureCredential(nil)
70+
if err != nil {
71+
//TODO: handle error
72+
}
5573
client := azquery.NewLogsClient(cred, nil)
56-
timespan := "2022-08-30/2022-08-31"
74+
workspaceID := "g4d1e129-fb1e-4b0a-b234-250abc987ea65" // example Azure Log Analytics Workspace ID
75+
timespan := "2022-08-30/2022-08-31" // ISO8601 Standard Timespan
5776

5877
batchRequest := azquery.BatchRequest{[]*azquery.BatchQueryRequest{
5978
{Body: &azquery.Body{Query: to.Ptr(kustoQuery1), Timespan: to.Ptr(timespan)}, ID: to.Ptr("1"), Workspace: to.Ptr(workspaceID)},
@@ -63,9 +82,16 @@ func ExampleLogsClient_Batch() {
6382

6483
res, err := client.Batch(context.TODO(), batchRequest, nil)
6584
if err != nil {
66-
panic(err)
85+
//TODO: handle error
86+
}
87+
88+
responses := res.BatchResponse.Responses
89+
fmt.Println("ID's of successful responses:")
90+
for _, response := range responses {
91+
if response.Body.Error == nil {
92+
fmt.Println(*response.ID)
93+
}
6794
}
68-
_ = res
6995
}
7096

7197
func ExampleMetricsClient_QueryResource() {
@@ -82,7 +108,7 @@ func ExampleMetricsClient_QueryResource() {
82108
Metricnamespace: to.Ptr("Microsoft.Storage/storageAccounts/blobServices"),
83109
})
84110
if err != nil {
85-
panic(err)
111+
//TODO: handle error
86112
}
87113
_ = res
88114
}

0 commit comments

Comments
 (0)