追加新的跟踪失败

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

Appending a new trace fails

问题

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

以下是你的Go代码:

  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "os"
  6. "time"
  7. "github.com/twinj/uuid"
  8. "golang.org/x/oauth2"
  9. goog "golang.org/x/oauth2/google"
  10. cloudtrace "google.golang.org/api/cloudtrace/v1"
  11. )
  12. func run() error {
  13. blob, err := ioutil.ReadFile("jwt.json")
  14. if err != nil {
  15. return err
  16. }
  17. conf, err := goog.JWTConfigFromJSON(blob, cloudtrace.CloudPlatformScope)
  18. if err != nil {
  19. return err
  20. }
  21. client := conf.Client(oauth2.NoContext)
  22. srv, err := cloudtrace.New(client)
  23. if err != nil {
  24. return err
  25. }
  26. tracer := cloudtrace.NewProjectsService(srv)
  27. now := time.Now()
  28. format := "2006-01-02T15:04:05.999999999Z"
  29. call := tracer.PatchTraces("foo-1262", &cloudtrace.Traces{
  30. Traces: []*cloudtrace.Trace{
  31. {
  32. TraceId: uuid.NewV4().String(),
  33. ProjectId: "foo-1262",
  34. Spans: []*cloudtrace.TraceSpan{
  35. {
  36. StartTime: now.Format(format),
  37. EndTime: now.Add(5 * time.Second).Format(format),
  38. Kind: "RPC_SERVER",
  39. Name: "bar",
  40. SpanId: 100,
  41. },
  42. },
  43. },
  44. },
  45. })
  46. _, err = call.Do()
  47. return err
  48. }
  49. func main() {
  50. if err := run(); err != nil {
  51. log.Fatal(err)
  52. }
  53. }

这是发送的请求:

  1. https://cloudtrace.googleapis.com/v1/projects/foo-1262/traces?alt=json
  2. {
  3. "traces": [{
  4. "projectId": "foo-1262",
  5. "spans": [{
  6. "endTime": "2016-03-26T17:23:19.705253417Z",
  7. "kind": "RPC_SERVER",
  8. "name": "foo",
  9. "spanId": "100",
  10. "startTime": "2016-03-26T17:23:14.705253417Z"
  11. }],
  12. "traceId": "4d86ec85-419d-40cf-ae95-d49c2d066cd6"
  13. }]
  14. }

响应如下:

  1. {
  2. "error": {
  3. "code": 400,
  4. "message": "Request contains an invalid argument.",
  5. "errors": [{
  6. "message": "Request contains an invalid argument.",
  7. "domain": "global",
  8. "reason": "badRequest"
  9. }],
  10. "status": "INVALID_ARGUMENT"
  11. }
  12. }

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

英文:

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

  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "os"
  6. "time"
  7. "github.com/twinj/uuid"
  8. "golang.org/x/oauth2"
  9. goog "golang.org/x/oauth2/google"
  10. cloudtrace "google.golang.org/api/cloudtrace/v1"
  11. )
  12. func run() error {
  13. blob, err := ioutil.ReadFile("jwt.json")
  14. if err != nil {
  15. return err
  16. }
  17. conf, err := goog.JWTConfigFromJSON(blob, cloudtrace.CloudPlatformScope)
  18. if err != nil {
  19. return err
  20. }
  21. client := conf.Client(oauth2.NoContext)
  22. srv, err := cloudtrace.New(client)
  23. if err != nil {
  24. return err
  25. }
  26. tracer := cloudtrace.NewProjectsService(srv)
  27. now := time.Now()
  28. format := "2006-01-02T15:04:05.999999999Z"
  29. call := tracer.PatchTraces("foo-1262", &cloudtrace.Traces{
  30. Traces: []*cloudtrace.Trace{
  31. {
  32. TraceId: uuid.NewV4().String(),
  33. ProjectId: "foo-1262",
  34. Spans: []*cloudtrace.TraceSpan{
  35. {
  36. StartTime: now.Format(format),
  37. EndTime: now.Add(5 * time.Second).Format(format),
  38. Kind: "RPC_SERVER",
  39. Name: "bar",
  40. SpanId: 100,
  41. },
  42. },
  43. },
  44. },
  45. })
  46. _, err = call.Do()
  47. return err
  48. }
  49. func main() {
  50. if err := run(); err != nil {
  51. log.Fatal(err)
  52. }
  53. }

Here's the request that gets sent

  1. https://cloudtrace.googleapis.com/v1/projects/foo-1262/traces?alt=json
  2. {
  3. "traces": [{
  4. "projectId": "foo-1262",
  5. "spans": [{
  6. "endTime": "2016-03-26T17:23:19.705253417Z",
  7. "kind": "RPC_SERVER",
  8. "name": "foo",
  9. "spanId": "100",
  10. "startTime": "2016-03-26T17:23:14.705253417Z"
  11. }],
  12. "traceId": "4d86ec85-419d-40cf-ae95-d49c2d066cd6"
  13. }]
  14. }

Response

  1. {
  2. "error": {
  3. "code": 400,
  4. "message": "Request contains an invalid argument.",
  5. "errors": [{
  6. "message": "Request contains an invalid argument.",
  7. "domain": "global",
  8. "reason": "badRequest"
  9. }],
  10. "status": "INVALID_ARGUMENT"
  11. }
  12. }

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

答案1

得分: 0

我的追踪ID生成有误。

  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "os"
  6. "time"
  7. "encoding/hex"
  8. "rand"
  9. "golang.org/x/oauth2"
  10. goog "golang.org/x/oauth2/google"
  11. cloudtrace "google.golang.org/api/cloudtrace/v1"
  12. )
  13. func run() error {
  14. blob, err := ioutil.ReadFile("jwt.json")
  15. if err != nil {
  16. return err
  17. }
  18. conf, err := goog.JWTConfigFromJSON(blob, cloudtrace.CloudPlatformScope)
  19. if err != nil {
  20. return err
  21. }
  22. client := conf.Client(oauth2.NoContext)
  23. srv, err := cloudtrace.New(client)
  24. if err != nil {
  25. return err
  26. }
  27. tracer := cloudtrace.NewProjectsService(srv)
  28. now := time.Now()
  29. format := "2006-01-02T15:04:05.999999999Z"
  30. p := make([]byte, 16)
  31. rand.Read(p)
  32. call := tracer.PatchTraces("foo-1262", &cloudtrace.Traces{
  33. Traces: []*cloudtrace.Trace{
  34. {
  35. TraceId: hex.EncodeToString(p),
  36. ProjectId: "foo-1262",
  37. Spans: []*cloudtrace.TraceSpan{
  38. {
  39. StartTime: now.Format(format),
  40. EndTime: now.Add(5 * time.Second).Format(format),
  41. Kind: "RPC_SERVER",
  42. Name: "bar",
  43. SpanId: 100,
  44. },
  45. },
  46. },
  47. },
  48. })
  49. _, err = call.Do()
  50. return err
  51. }
  52. func main() {
  53. if err := run(); err != nil {
  54. log.Fatal(err)
  55. }
  56. }
英文:

My trace ID generation was incorrect.

  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "os"
  6. "time"
  7. "encoding/hex"
  8. "rand"
  9. "golang.org/x/oauth2"
  10. goog "golang.org/x/oauth2/google"
  11. cloudtrace "google.golang.org/api/cloudtrace/v1"
  12. )
  13. func run() error {
  14. blob, err := ioutil.ReadFile("jwt.json")
  15. if err != nil {
  16. return err
  17. }
  18. conf, err := goog.JWTConfigFromJSON(blob, cloudtrace.CloudPlatformScope)
  19. if err != nil {
  20. return err
  21. }
  22. client := conf.Client(oauth2.NoContext)
  23. srv, err := cloudtrace.New(client)
  24. if err != nil {
  25. return err
  26. }
  27. tracer := cloudtrace.NewProjectsService(srv)
  28. now := time.Now()
  29. format := "2006-01-02T15:04:05.999999999Z"
  30. p := make([]byte, 16)
  31. rand.Read(p)
  32. call := tracer.PatchTraces("foo-1262", &cloudtrace.Traces{
  33. Traces: []*cloudtrace.Trace{
  34. {
  35. TraceId: hex.EncodeToString(p),
  36. ProjectId: "foo-1262",
  37. Spans: []*cloudtrace.TraceSpan{
  38. {
  39. StartTime: now.Format(format),
  40. EndTime: now.Add(5 * time.Second).Format(format),
  41. Kind: "RPC_SERVER",
  42. Name: "bar",
  43. SpanId: 100,
  44. },
  45. },
  46. },
  47. },
  48. })
  49. _, err = call.Do()
  50. return err
  51. }
  52. func main() {
  53. if err := run(); err != nil {
  54. log.Fatal(err)
  55. }
  56. }

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:

确定