记一次GO语言调取字节云监控SDK

记一次GO语言调取字节云监控SDK

安装SDK

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

在go.mod中设置服务依赖。

require github.com/volcengine/volcengine-go-sdk v1.0.48

代码示例

package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"github.com/sirupsen/logrus"
	"github.com/volcengine/volcengine-go-sdk/volcengine"
	"github.com/volcengine/volcengine-go-sdk/volcengine/credentials"
	"github.com/volcengine/volcengine-go-sdk/volcengine/session"
	"gopkg.in/yaml.v2"
	"io"
	"monitor/service/volcobserve"
	"os"
	"strings"
)

func main() {
	var (
		endTime      int64
		startTime    int64
		dimensions   DimensionSlice
		metricName   string
		period       string
		namespace    string
		subnamespace string
	)

	log := logrus.New()
	file, err := os.OpenFile("logfile.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err == nil {
		log.SetOutput(io.MultiWriter(os.Stdout, file))
	} else {
		log.Info("Failed to open log file, using default stderr")
	}
	defer file.Close()

	flag.Int64Var(&endTime, "endTime", 0, "End time")
	flag.Int64Var(&startTime, "startTime", 0, "Start time")
	flag.Var(&dimensions, "dimensions", "Dimensions (name=value pairs, separated by comma)")
	flag.StringVar(&metricName, "metricName", "", "metricName")

	flag.StringVar(&period, "period", "", "Period")
	flag.StringVar(&namespace, "namespace", "", "Namespace")
	flag.StringVar(&subnamespace, "subnamespace", "", "subnamespace")

	flag.Parse()

	bagConfigPath := "metric.yaml"
	bagConfigFile, err := os.ReadFile(bagConfigPath)

	var bConfig config
	err = yaml.Unmarshal(bagConfigFile, &bConfig)

	config := volcengine.NewConfig().
		WithRegion(bConfig.Region).
		WithCredentials(credentials.NewStaticCredentials(bConfig.AccessKey, bConfig.SecretKey, "")).
		WithEndpoint(bConfig.EndPoint)
	sess, err := session.NewSession(config)
	if err != nil {
		log.Error(err)
	}
	svc := volcobserve.New(sess, bConfig.Service, bConfig.Service, bConfig.Version)
	in := &volcobserve.GetMetricDataInput{
		EndTime:   volcengine.Int64(endTime),
		StartTime: volcengine.Int64(startTime),
		Instances: []*volcobserve.InstanceForGetMetricDataInput{
			{
				Dimensions: dimensions,
			},
		},
		MetricName:   volcengine.String(metricName),
		Period:       volcengine.String(period),
		Namespace:    volcengine.String(namespace),
		SubNamespace: volcengine.String(subnamespace),
	}

	resp, err := svc.GetMetricData(in)
	if err != nil {
		log.Error(err)
	}
	b, _ := json.Marshal(resp)
	log.Println(string(b))
}

type DimensionSlice []*volcobserve.DimensionForGetMetricDataInput

func (s *DimensionSlice) String() string {
	result := make([]string, len(*s))
	for i, dim := range *s {
		result[i] = fmt.Sprintf("%s=%s", *dim.Name, *dim.Value)
	}
	return strings.Join(result, ",")
}

func (s *DimensionSlice) Set(value string) error {
	pairs := strings.Split(value, ",")
	for _, pair := range pairs {
		kv := strings.SplitN(pair, "=", 2)
		if len(kv) != 2 {
			return fmt.Errorf("invalid dimension: %s", pair)
		}

		name := strings.TrimSpace(kv[0])
		val := strings.TrimSpace(kv[1])

		dim := &volcobserve.DimensionForGetMetricDataInput{
			Name:  volcengine.String(name),
			Value: volcengine.String(val),
		}
		*s = append(*s, dim)
	}
	return nil
}

type config struct {
	AccessKey string `yaml:"accessKey"`
	SecretKey string `yaml:"secretKey"`
	EndPoint  string `yaml:"endPoint"`
	Scheme    string `yaml:"scheme"`
	Region    string `yaml:"region"`
	Service   string `yaml:"service"`
	ServiceID string `yaml:"serviceID"`
	Version   string `yaml:"version"`
}

新建一个metric.yaml

accessKey : ""
secretKey : "=="
endPoint : ""
region : ""
version : ""
service : ""
serviceID : ""

代码完成

测试

go run main.go --endTime=1691579850 --startTime=1691493450
–subnamespace= --dimensions=“Namespace=test” --metricName=“Container_CPUUsed” --period=“1m” --namespace=“VCM_VKE”

编写Dockerfile

# builder image
FROM golang:1.20.7 as builder

RUN apt-get update && apt-get install -y vim

WORKDIR /standard-component

ADD . .

#编译linux环境
ENV GOOS=linux GOARCH=amd64
#golang 代理仓库
#ENV GOPROXY=https://goproxy.cn,https://goproxy.io,direct
ENV GOPROXY=https://proxy.golang.com.cn,direct

RUN go mod tidy
RUN go build -o component main.go
RUN chmod +x component

#runtime image
FROM golang:1.20.7

RUN apt-get update && apt-get install -y vim

#
WORKDIR /standard-component
COPY --from=builder /standard-component/component .
COPY --from=builder /standard-component/metric.yaml ./metric.yaml

打包镜像
docker build . -aa-test:1.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,实现安卓app调取萤石摄像头需要用到萤石SDK。下面是一个简单的示例: 1. 在萤石开发者中心申请AppKey和AppSecret,以获取SDK的使用权限。 2. 将萤石SDK添加到您的项目中。您可以通过Gradle添加以下依赖: ```groovy implementation 'com.videogo:ezopenSDK:5.0.0' ``` 3. 在您的代码中调用SDK接口,实现调取摄像头的功能。以下是一个示例代码: ```java import com.videogo.openapi.EZOpenSDK; import com.videogo.openapi.bean.EZAccessToken; import com.videogo.openapi.bean.EZCameraInfo; import com.videogo.openapi.callback.OnEZAccessTokenCallback; import com.videogo.util.LogUtil; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private static final String APP_KEY = "您的AppKey"; private static final String APP_SECRET = "您的AppSecret"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化EZOpenSDK EZOpenSDK.showSDKLog(true); EZOpenSDK.enableP2P(true); EZOpenSDK.initLib(this, APP_KEY); // 获取AccessToken EZOpenSDK.getInstance().openLoginPage(new OnEZAccessTokenCallback() { @Override public void onAccessTokenResult(final EZAccessToken ezAccessToken) { // 获取AccessToken成功 LogUtil.i(TAG, "onAccessTokenResult: " + ezAccessToken.getAccessToken()); // 获取摄像头信息 EZOpenSDK.getInstance().getCameraInfo("您的摄像头序列号", new OnGetCameraInfoListener() { @Override public void onGetCameraInfoSuccess(EZCameraInfo ezCameraInfo) { // 获取摄像头信息成功 LogUtil.i(TAG, "onGetCameraInfoSuccess: " + ezCameraInfo.getCameraName()); // 打开实时预览页面 EZOpenSDK.getInstance().openCameraLivePlayPage(ezCameraInfo.getCameraId(), 1); } @Override public void onGetCameraInfoError(ErrorInfo errorInfo) { // 获取摄像头信息失败 LogUtil.e(TAG, "onGetCameraInfoError: " + errorInfo.getErrorCode()); } }); } @Override public void onAccessTokenError(ErrorInfo errorInfo) { // 获取AccessToken失败 LogUtil.e(TAG, "onAccessTokenError: " + errorInfo.getErrorCode()); } }, APP_KEY, APP_SECRET); } @Override protected void onDestroy() { super.onDestroy(); // 释放EZOpenSDK资源 EZOpenSDK.getInstance().releaseLib(); } } ``` 这段代码的作用是获取萤石的AccessToken,然后获取指定摄像头的信息,最后打开实时预览页面。请将代码中的“您的AppKey”、“您的AppSecret”和“您的摄像头序列号”替换为实际的值。 需要注意的是,打开实时预览页面需要相应的权限,例如网络访问和摄像头访问权限。您需要在AndroidManifest.xml文件中添加以下权限声明: ```xml <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CAMERA" /> ``` 这样,就可以实现安卓app调取萤石摄像头的功能了。如果您需要添加更多的功能,例如录制视频或拍照等,可以参考萤石SDK相关文档。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值