本文为您介绍云监控Go SDK的下载地址、安装方式以及代码示例,帮助您快速了解如何使用SDK调用OpenAPI。
go version
可以检查当前Go的版本信息。Golang SDK下载地址:volcengine-go-sdk 。
新建一个Go项目,使用go mod作为软件依赖工具。
在go.mod
中设置服务依赖。
require github.com/volcengine/volcengine-go-sdk v1.0.148
如需下载最新版本,请参考Github下载地址中的release tag,替换命令中v1.0.148为最新版本。
新建GetMetricData.go
文件,添加如下代码,查询指定指标在指定时间选段内聚合的时序数据。
package main import ( "fmt" "github.com/volcengine/volcengine-go-sdk/service/volcobserve" "github.com/volcengine/volcengine-go-sdk/volcengine" "github.com/volcengine/volcengine-go-sdk/volcengine/session" ) func main() { // 设置您的ak ak := "Your AK" // 设置您的sk sk := "Your SK" // 设置您要访问的地域 var region = "cn-beijing" //if use env Credentials //please //export VOLCSTACK_ACCESS_KEY=AK //export VOLCSTACK_SECRET_KEY=SK // and WithCredentials(credentials.NewEnvCredentials()) config := volcengine.NewConfig(). WithRegion(region). WithAkSk(ak, sk). //WithCredentials(credentials.NewEnvCredentials()). WithDisableSSL(true) //WithLogLevel(volcengine.LogDebugWithHTTPBody). sess, _ := session.NewSession(config) svc := volcobserve.New(sess) in := &volcobserve.GetMetricDataInput{ EndTime: volcengine.Int64(1673272219), StartTime: volcengine.Int64(1673185819), Instances: []*volcobserve.InstanceForGetMetricDataInput{ { Dimensions: []*volcobserve.DimensionForGetMetricDataInput{ { Name: volcengine.String("****"), Value: volcengine.String("eip-rrtfrj****"), }, }, }, }, MetricName: volcengine.String("***"), Period: volcengine.String("**s"), Namespace: volcengine.String("***"), SubNamespace: volcengine.String("***"), } resp, err := svc.GetMetricData(in) if err != nil { panic(err) } fmt.Printf("Response is %v", resp) }