We’re excited to announce the stable release of Azure Monitor Query for Go. Monitor Query allows users to retrieve data from Azure Monitor‘s Logs and Metrics data platforms. With this release, Go joins the following languages offering a stable client library:
Considerable effort has gone into delivering an engaging developer experience in Monitor Query while remaining idiomatic to Go. Since the module’s first beta in September, we’ve taken learnings from early adopters and from moderated UX participants to iterate on the design and bring the module to its current state.
Thank you to the Azure SDK for Go engineering team and the wonderful Product Managers that made this module possible. Additionally, thank you to all the developers who tested and provided feedback. We hope you continue to use Monitor Query to build powerful, data-driven applications.
Install the package
To install the latest version of Monitor Query, use the go get
command. The Azure Identity module is required to authenticate your client.
go get github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
You need an Azure subscription and a working development environment for Go version 1.18 or above.
For logs, you need an Azure Log Analytics workspace. The workspace contains the logs data, and its workspace ID is used for all log queries.
For metrics, you need the resource ID/URI of any Azure resource. That ID is used for all metric queries for that resource.
Create a client
Monitor Query contains two separate clients for querying data: one to connect to logs and the other to metrics. They follow a similar pattern for creation and both pass in a TokenCredential
as a parameter. This blog post uses the token credential DefaultAzureCredential
to authenticate.
Logs client
package main
import (
"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 {
//TODO: handle error
}
client, err := azquery.NewLogsClient(cred, nil)
if err != nil {
//TODO: handle error
}
_ = client
}
Metrics client
package main
import (
"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 {
//TODO: handle error
}
client, err := azquery.NewMetricsClient(cred, nil)
if err != nil {
//TODO: handle error
}
_ = client
}
Execute a query
Once a client is created, you can execute queries with it.
Query logs
To specify the logs query, the service uses the Kusto Query Language.
For a basic logs query, use the QueryWorkspace
method. The method takes in a context, primary workspace ID, a Body
struct, and options. The Body
contains the Kusto query and a timespan over which to execute the query. It’s recommended to always include a timespan to avoid long-running queries.
The below example demonstrates a basic logs query using LogsClient.QueryWorkspace
. It executes the Kusto query AzureActivity | top 10 by TimeGenerated
from December 25, 2022 to December 26, 2022 UTC on the example workspace ID. The code then checks for errors, and if successful, prints the results of the query.
package main
import (
"context"
"fmt"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery"
)
// Basic QueryWorkspace example
func main() {
workspaceID := "g4d1e129-fb1e-4b0a-b234-250abc987ea65" // example Azure Log Analytics Workspace ID
// See the "Create a client" section above how to create a LogsClient
res, err := logsClient.QueryWorkspace(
context.TODO(),
workspaceID,
azquery.Body{
Query: to.Ptr("AzureActivity | top 10 by TimeGenerated"), // example Kusto query
Timespan: to.Ptr(azquery.NewTimeInterval(time.Date(2022, 12, 25, 0, 0, 0, 0, time.UTC), time.Date(2022, 12, 25, 12, 0, 0, 0, time.UTC))),
},
nil)
if err != nil {
//TODO: handle error
}
if res.Error != nil {
//TODO: handle partial error
}
// Print Rows
for _, table := range res.Tables {
for _, row := range table.Rows {
fmt.Println(row)
}
}
}
Query metrics
In contrast to log queries, metric queries don’t use the Kusto Query Language.
Use the MetricsClient.QueryResource
method to retrieve metrics data. The method takes in a context, resource ID, and options. The following example shows the code querying the specified blob resource.
package main
import (
"context"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery"
)
// Basic QueryResource example
func main() {
// See the "Create a client" section above how to create a MetricsClient
res, err := metricsClient.QueryResource(context.TODO(), resourceURI,
&azquery.MetricsClientQueryResourceOptions{
Timespan: to.Ptr(azquery.NewTimeInterval(time.Date(2022, 12, 25, 0, 0, 0, 0, time.UTC), time.Date(2022, 12, 25, 12, 0, 0, 0, time.UTC))),
Interval: to.Ptr("PT1M"),
MetricNames: nil,
Aggregation: to.SliceOfPtrs(azquery.AggregationTypeAverage, azquery.AggregationTypeCount),
Top: to.Ptr[int32](3),
OrderBy: to.Ptr("Average asc"),
Filter: to.Ptr("BlobType eq '*'"),
ResultType: nil,
MetricNamespace: to.Ptr("Microsoft.Storage/storageAccounts/blobServices"),
})
if err != nil {
//TODO: handle error
}
// Print out metric name and the time stamps of each metric data point
for _, metric := range res.Value {
fmt.Println(*metric.Name.Value)
for _, timeSeriesElement := range metric.TimeSeries {
for _, metricValue := range timeSeriesElement.Data {
fmt.Println(metricValue.TimeStamp)
}
}
}
}
Summary
The new Azure Monitor Query for Go module allows users to retrieve Logs and Metrics data from Azure Monitor. To learn more, please see our documentation. You can also find more examples either on pkg.go.dev or in our GitHub repository.
We always love hearing your feedback. To report an issue or submit suggestions, visit our GitHub repository.
0 comments