追加新的跟踪失败

huangapple go评论83阅读模式
英文:

Appending a new trace fails

问题

我正在尝试使用patchTraces方法将跟踪信息提交到Google CloudTrace。我发送的补丁请求返回了一个不透明的错误消息。你知道我做错了什么吗?

以下是你的Go代码:

package main

import (
	"io/ioutil"
	"log"
	"os"
	"time"

	"github.com/twinj/uuid"
	"golang.org/x/oauth2"
	goog "golang.org/x/oauth2/google"
	cloudtrace "google.golang.org/api/cloudtrace/v1"
)

func run() error {
	blob, err := ioutil.ReadFile("jwt.json")
	if err != nil {
		return err
	}
	conf, err := goog.JWTConfigFromJSON(blob, cloudtrace.CloudPlatformScope)
	if err != nil {
		return err
	}

	client := conf.Client(oauth2.NoContext)

	srv, err := cloudtrace.New(client)
	if err != nil {
		return err
	}

	tracer := cloudtrace.NewProjectsService(srv)
	now := time.Now()
	format := "2006-01-02T15:04:05.999999999Z"

	call := tracer.PatchTraces("foo-1262", &cloudtrace.Traces{
		Traces: []*cloudtrace.Trace{
			{
				TraceId:   uuid.NewV4().String(),
				ProjectId: "foo-1262",
				Spans: []*cloudtrace.TraceSpan{
					{
						StartTime: now.Format(format),
						EndTime:   now.Add(5 * time.Second).Format(format),
						Kind:      "RPC_SERVER",
						Name:      "bar",
						SpanId:    100,
					},
				},
			},
		},
	})

	_, err = call.Do()
	return err
}

func main() {
	if err := run(); err != nil {
		log.Fatal(err)
	}
}

这是发送的请求:

https://cloudtrace.googleapis.com/v1/projects/foo-1262/traces?alt=json
{
"traces": [{
"projectId": "foo-1262",
"spans": [{
"endTime": "2016-03-26T17:23:19.705253417Z",
"kind": "RPC_SERVER",
"name": "foo",
"spanId": "100",
"startTime": "2016-03-26T17:23:14.705253417Z"
}],
"traceId": "4d86ec85-419d-40cf-ae95-d49c2d066cd6"
}]
}

响应如下:

{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"errors": [{
"message": "Request contains an invalid argument.",
"domain": "global",
"reason": "badRequest"
}],
"status": "INVALID_ARGUMENT"
}
}

一切看起来都正确,但我收到了一个无用的错误消息。请帮忙!

英文:

I'm attempting to use the patchTraces method to submit tracing information to Google CloudTrace. The patch request I'm making returns a opaque error message. Any idea what I'm doing wrong?

Go code

package main
import (
"io/ioutil"
"log"
"os"
"time"
"github.com/twinj/uuid"
"golang.org/x/oauth2"
goog "golang.org/x/oauth2/google"
cloudtrace "google.golang.org/api/cloudtrace/v1"
)
func run() error {
blob, err := ioutil.ReadFile("jwt.json")
if err != nil {
return err
}
conf, err := goog.JWTConfigFromJSON(blob, cloudtrace.CloudPlatformScope)
if err != nil {
return err
}
client := conf.Client(oauth2.NoContext)
srv, err := cloudtrace.New(client)
if err != nil {
return err
}
tracer := cloudtrace.NewProjectsService(srv)
now := time.Now()
format := "2006-01-02T15:04:05.999999999Z"
call := tracer.PatchTraces("foo-1262", &cloudtrace.Traces{
Traces: []*cloudtrace.Trace{
{
TraceId:   uuid.NewV4().String(),
ProjectId: "foo-1262",
Spans: []*cloudtrace.TraceSpan{
{
StartTime: now.Format(format),
EndTime:   now.Add(5 * time.Second).Format(format),
Kind:      "RPC_SERVER",
Name:      "bar",
SpanId:    100,
},
},
},
},
})
_, err = call.Do()
return err
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}

Here's the request that gets sent

https://cloudtrace.googleapis.com/v1/projects/foo-1262/traces?alt=json
{
"traces": [{
"projectId": "foo-1262",
"spans": [{
"endTime": "2016-03-26T17:23:19.705253417Z",
"kind": "RPC_SERVER",
"name": "foo",
"spanId": "100",
"startTime": "2016-03-26T17:23:14.705253417Z"
}],
"traceId": "4d86ec85-419d-40cf-ae95-d49c2d066cd6"
}]
}

Response

{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"errors": [{
"message": "Request contains an invalid argument.",
"domain": "global",
"reason": "badRequest"
}],
"status": "INVALID_ARGUMENT"
}
}

Everything looks correct, but I'm getting back a useless error message. Please help!

答案1

得分: 0

我的追踪ID生成有误。

package main

import (
    "io/ioutil"
    "log"
    "os"
    "time"
    "encoding/hex"
    "rand"

    "golang.org/x/oauth2"
    goog "golang.org/x/oauth2/google"
    cloudtrace "google.golang.org/api/cloudtrace/v1"
)

func run() error {
    blob, err := ioutil.ReadFile("jwt.json")
    if err != nil {
        return err
    }
    conf, err := goog.JWTConfigFromJSON(blob, cloudtrace.CloudPlatformScope)
    if err != nil {
        return err
    }

    client := conf.Client(oauth2.NoContext)

    srv, err := cloudtrace.New(client)
    if err != nil {
        return err
    }

    tracer := cloudtrace.NewProjectsService(srv)
    now := time.Now()
    format := "2006-01-02T15:04:05.999999999Z"

    p := make([]byte, 16)
    rand.Read(p)

    call := tracer.PatchTraces("foo-1262", &cloudtrace.Traces{
        Traces: []*cloudtrace.Trace{
            {
                TraceId:   hex.EncodeToString(p),
                ProjectId: "foo-1262",
                Spans: []*cloudtrace.TraceSpan{
                    {
                        StartTime: now.Format(format),
                        EndTime:   now.Add(5 * time.Second).Format(format),
                        Kind:      "RPC_SERVER",
                        Name:      "bar",
                        SpanId:    100,
                    },
                },
            },
        },
    })

    _, err = call.Do()
    return err
}

func main() {
    if err := run(); err != nil {
        log.Fatal(err)
    }
}
英文:

My trace ID generation was incorrect.

package main
import (
"io/ioutil"
"log"
"os"
"time"
"encoding/hex"
"rand"
"golang.org/x/oauth2"
goog "golang.org/x/oauth2/google"
cloudtrace "google.golang.org/api/cloudtrace/v1"
)
func run() error {
blob, err := ioutil.ReadFile("jwt.json")
if err != nil {
return err
}
conf, err := goog.JWTConfigFromJSON(blob, cloudtrace.CloudPlatformScope)
if err != nil {
return err
}
client := conf.Client(oauth2.NoContext)
srv, err := cloudtrace.New(client)
if err != nil {
return err
}
tracer := cloudtrace.NewProjectsService(srv)
now := time.Now()
format := "2006-01-02T15:04:05.999999999Z"
p := make([]byte, 16)
rand.Read(p)
call := tracer.PatchTraces("foo-1262", &cloudtrace.Traces{
Traces: []*cloudtrace.Trace{
{
TraceId:   hex.EncodeToString(p),
ProjectId: "foo-1262",
Spans: []*cloudtrace.TraceSpan{
{
StartTime: now.Format(format),
EndTime:   now.Add(5 * time.Second).Format(format),
Kind:      "RPC_SERVER",
Name:      "bar",
SpanId:    100,
},
},
},
},
})
_, err = call.Do()
return err
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}

huangapple
  • 本文由 发表于 2016年3月27日 08:39:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/36242589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定