gpt4 book ai didi

go - 生产就绪的 Google Cloud Function 是什么样的?

转载 作者:IT老高 更新时间:2023-10-28 13:08:50 25 4
gpt4 key购买 nike

我刚刚开始使用 Google Cloud Functions 进行整个无服务器操作,所有示例基本上都是“Helloworld”。

package function

import (
"net/http"
)

func F(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!\n"))
}

生产就绪功能是什么样的?

最佳答案

鉴于提供商会为您处理诸如扩展之类的事情,我要做的第一件事就是关注可观察性。你不知道你不知道什么。我喜欢添加自己的跟踪和结构化日志记录;记录到标准输出在很多方面都存在不足。

package function

import (
"context"
"fmt"
"net/http"
"os"
"sync"

"cloud.google.com/go/logging"
"contrib.go.opencensus.io/exporter/stackdriver"
"contrib.go.opencensus.io/exporter/stackdriver/propagation"
"go.opencensus.io/trace"
"google.golang.org/genproto/googleapis/api/monitoredres"
)

var (
logger *logging.Logger
once sync.Once
)

// configFunc sets the global configuration; it's overridden in tests.
var configFunc = defaultConfigFunc

func F(w http.ResponseWriter, r *http.Request) {
once.Do(func() {
if err := configFunc(); err != nil {
panic(err)
}
})

defer logger.Flush()

ctx := r.Context()
var span *trace.Span

httpFormat := &propagation.HTTPFormat{}
sc, ok := httpFormat.SpanContextFromRequest(r)
if ok {
ctx, span = trace.StartSpanWithRemoteParent(ctx, "helloworld", sc,
trace.WithSampler(trace.AlwaysSample()),
trace.WithSpanKind(trace.SpanKindServer),
)
defer span.End()
}

logger.Log(logging.Entry{
Payload: "Handling new HTTP request",
Severity: logging.Info,
})

w.Write([]byte("Hello, World!\n"))
}

func defaultConfigFunc() error {
var err error

projectId := os.Getenv("GCP_PROJECT")
if projectId == "" {
return fmt.Errorf("GCP_PROJECT environment variable unset or missing")
}

functionName := os.Getenv("FUNCTION_NAME")
if functionName == "" {
return fmt.Errorf("FUNCTION_NAME environment variable unset or missing")
}

region := os.Getenv("FUNCTION_REGION")
if region == "" {
return fmt.Errorf("FUNCTION_REGION environment variable unset or missing")
}

stackdriverExporter, err := stackdriver.NewExporter(stackdriver.Options{ProjectID: projectId})
if err != nil {
return err
}

trace.RegisterExporter(stackdriverExporter)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})

client, err := logging.NewClient(context.Background(), projectId)
if err != nil {
return err
}

monitoredResource := monitoredres.MonitoredResource{
Type: "cloud_function",
Labels: map[string]string{
"function_name": functionName,
"region": region,
},
}

commonResource := logging.CommonResource(&monitoredResource)
logger = client.Logger(functionName, commonResource)

return nil
}

此外,请确保您提供了所有依赖项,您确实希望您的构建是可重现的。这些天我正在使用 Go 1.11,以下对我有用:

$ mkdir helloworld
$ cd helloworld
$ vim function.go
$ echo "module github.com/kelseyhightower/helloworld" > go.mod
$ go mod vendor

关于go - 生产就绪的 Google Cloud Function 是什么样的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52075778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com