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

expand examples #19208

Merged
merged 10 commits into from
Oct 4, 2022
Merged
Changes from 6 commits
Commits
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
111 changes: 78 additions & 33 deletions sdk/monitor/azquery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func main() {
panic(err)
}

client := azkeys.NewLogsClient(cred, nil)
client := azquery.NewLogsClient(cred, nil)
}
```

Expand All @@ -63,7 +63,7 @@ func main() {
panic(err)
}

client := azkeys.NewMetricsClient(cred, nil)
client := azquery.NewMetricsClient(cred, nil)
}
```

Expand Down Expand Up @@ -102,6 +102,9 @@ The timespan can be the following string formats:
<duration> such as "P1Y2M10DT2H30M" // 1 year, 2 months, 10 days, 2 hours, 20 minutes
```

### Logs Workspace IDs
gracewilcox marked this conversation as resolved.
Show resolved Hide resolved


## Examples

- [Logs query](#logs-query)
Expand All @@ -117,66 +120,104 @@ The timespan can be the following string formats:
- [Metrics result structure](#metrics-result-structure)

### Logs query
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.

```go
client := azquery.NewLogsClient(cred, nil)
timespan := "2022-08-30/2022-08-31"
import (
gracewilcox marked this conversation as resolved.
Show resolved Hide resolved
"context"

res, err := client.QueryWorkspace(context.TODO(), workspaceID, azquery.Body{Query: to.Ptr(query), Timespan: to.Ptr(timespan)}, nil)
if err != nil {
panic(err)
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery"
)

func main() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
panic(err)
}
client := azquery.NewLogsClient(cred, nil)
workspaceID := "g4d1e129-fb1e-4b0a-b234-250abc987ea65" // example Azure Log Analytics Workspace ID
query := "AzureActivity | top 10 by TimeGenerated" // Kusto query
timespan := "2022-08-30/2022-08-31" // ISO8601 Standard timespan

res, err := client.QueryWorkspace(context.TODO(), workspaceID, azquery.Body{Query: to.Ptr(query), Timespan: to.Ptr(timespan)}, nil)
if err != nil {
panic(err)
}
if res.Results.Error != nil {
// handle partial error
}

table := res.Results.Tables[0]
fmt.Println("Response rows:")
for _, row := range table.Rows {
fmt.Println(row)
}
}
_ = res
```

#### Logs query body structure
```
Body
|---Query *string // Kusto Query
|---Timespan *string // ISO8601 Standard Timespan
|---Timespan *string // ISO8601 Standard Timespan- refer to timespan section for more info
|---Workspaces []*string //Optional- additional workspaces to query
```

#### Logs query result structure
```
LogsResponse
Results
|---Tables []*Table
|---Columns []*Column
|---Name *string
|---Type *LogsColumnType
|---Name *string
|---Rows [][]interface{}
|---Error *ErrorInfo
|---Code *string
|---Message *string
|---AdditionalProperties interface{}
|---Details []*ErrorDetail
|---Code *string
|---Message *string
|---AdditionalProperties interface{}
|---Resources []*string
|---Target *string
|---Value *string
|---Innererror *ErrorInfo
|---Render interface{}
|---Statistics interface{}
```

### Batch query
`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.
```go
client := azquery.NewLogsClient(cred, nil)
timespan := "2022-08-30/2022-08-31"
import (
"context"

batchRequest := azquery.BatchRequest{[]*azquery.BatchQueryRequest{
{Body: &azquery.Body{Query: to.Ptr(kustoQuery1), Timespan: to.Ptr(timespan)}, ID: to.Ptr("1"), Workspace: to.Ptr(workspaceID)},
{Body: &azquery.Body{Query: to.Ptr(kustoQuery2), Timespan: to.Ptr(timespan)}, ID: to.Ptr("2"), Workspace: to.Ptr(workspaceID)},
{Body: &azquery.Body{Query: to.Ptr(kustoQuery3), Timespan: to.Ptr(timespan)}, ID: to.Ptr("3"), Workspace: to.Ptr(workspaceID)},
}}
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery"
)

res, err := client.Batch(context.TODO(), batchRequest, nil)
if err != nil {
panic(err)
func main() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
panic(err)
}
client := azquery.NewLogsClient(cred, nil)
workspaceID := "g4d1e129-fb1e-4b0a-b234-250abc987ea65" // example Azure Log Analytics Workspace ID
timespan := "2022-08-30/2022-08-31" // ISO8601 Standard Timespan

batchRequest := azquery.BatchRequest{[]*azquery.BatchQueryRequest{
{Body: &azquery.Body{Query: to.Ptr(kustoQuery1), Timespan: to.Ptr(timespan)}, ID: to.Ptr("1"), Workspace: to.Ptr(workspaceID)},
{Body: &azquery.Body{Query: to.Ptr(kustoQuery2), Timespan: to.Ptr(timespan)}, ID: to.Ptr("2"), Workspace: to.Ptr(workspaceID)},
{Body: &azquery.Body{Query: to.Ptr(kustoQuery3), Timespan: to.Ptr(timespan)}, ID: to.Ptr("3"), Workspace: to.Ptr(workspaceID)},
}}

res, err := client.Batch(context.TODO(), batchRequest, nil)
if err != nil {
panic(err)
}

responses := res.BatchResponse.Responses
fmt.Println("ID's of successful responses:")
for _, response := range responses {
if response.Body.Error == nil {
fmt.Println(*response.ID)
}
}
}
_ = res
```

#### Batch query request structure
Expand Down Expand Up @@ -279,7 +320,7 @@ _ = res

#### Metrics result structure
```
MetricsResults
Response
|---Timespan *string
|---Value []*Metric
|---ID *string
Expand Down Expand Up @@ -346,6 +387,10 @@ comments.
[azure_sub]: https://azure.microsoft.com/free/
[azure_monitor_create_using_portal]: https://docs.microsoft.com/azure/azure-monitor/logs/quick-create-workspace
[azure_monitor_overview]: https://docs.microsoft.com/azure/azure-monitor/overview
[context]: https://pkg.go.dev/context
[kusto_query_language]: https://learn.microsoft.com/azure/data-explorer/kusto/query/
[log_analytics_workspace]: https://learn.microsoft.com/azure/azure-monitor/logs/log-analytics-workspace-overview
[time_go]: https://pkg.go.dev/time
[time_intervals]: https://en.wikipedia.org/wiki/ISO_8601#Time_intervals

[cla]: https://cla.microsoft.com
Expand Down