You need to enable JavaScript to run this app.
导航
Go使用示例
最近更新时间:2024.07.17 17:12:17首次发布时间:2023.01.12 10:09:55

本文为您介绍云监控Go SDK的下载地址、安装方式以及代码示例,帮助您快速了解如何使用SDK调用OpenAPI。

前提条件

  • 您已注册火山引擎账号并完成实名认证。具体步骤,请参见账号注册实名认证
  • 已获取账号的Access Key。具体步骤,请参见获取Access Key
  • 已安装Go 1.12~1.18版本,执行go version可以检查当前Go的版本信息。

SDK下载地址

Golang SDK下载地址:volcengine-go-sdk

安装SDK

  1. 新建一个Go项目,使用go mod作为软件依赖工具。

  2. 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)
}