trace.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package trace
  15. import (
  16. "context"
  17. "fmt"
  18. "go.opencensus.io/trace"
  19. "google.golang.org/api/googleapi"
  20. "google.golang.org/genproto/googleapis/rpc/code"
  21. "google.golang.org/grpc/status"
  22. )
  23. // StartSpan adds a span to the trace with the given name.
  24. func StartSpan(ctx context.Context, name string) context.Context {
  25. ctx, _ = trace.StartSpan(ctx, name)
  26. return ctx
  27. }
  28. // EndSpan ends a span with the given error.
  29. func EndSpan(ctx context.Context, err error) {
  30. span := trace.FromContext(ctx)
  31. if err != nil {
  32. span.SetStatus(toStatus(err))
  33. }
  34. span.End()
  35. }
  36. // toStatus interrogates an error and converts it to an appropriate
  37. // OpenCensus status.
  38. func toStatus(err error) trace.Status {
  39. if err2, ok := err.(*googleapi.Error); ok {
  40. return trace.Status{Code: httpStatusCodeToOCCode(err2.Code), Message: err2.Message}
  41. } else if s, ok := status.FromError(err); ok {
  42. return trace.Status{Code: int32(s.Code()), Message: s.Message()}
  43. } else {
  44. return trace.Status{Code: int32(code.Code_UNKNOWN), Message: err.Error()}
  45. }
  46. }
  47. // TODO (deklerk): switch to using OpenCensus function when it becomes available.
  48. // Reference: https://github.com/googleapis/googleapis/blob/26b634d2724ac5dd30ae0b0cbfb01f07f2e4050e/google/rpc/code.proto
  49. func httpStatusCodeToOCCode(httpStatusCode int) int32 {
  50. switch httpStatusCode {
  51. case 200:
  52. return int32(code.Code_OK)
  53. case 499:
  54. return int32(code.Code_CANCELLED)
  55. case 500:
  56. return int32(code.Code_UNKNOWN) // Could also be Code_INTERNAL, Code_DATA_LOSS
  57. case 400:
  58. return int32(code.Code_INVALID_ARGUMENT) // Could also be Code_OUT_OF_RANGE
  59. case 504:
  60. return int32(code.Code_DEADLINE_EXCEEDED)
  61. case 404:
  62. return int32(code.Code_NOT_FOUND)
  63. case 409:
  64. return int32(code.Code_ALREADY_EXISTS) // Could also be Code_ABORTED
  65. case 403:
  66. return int32(code.Code_PERMISSION_DENIED)
  67. case 401:
  68. return int32(code.Code_UNAUTHENTICATED)
  69. case 429:
  70. return int32(code.Code_RESOURCE_EXHAUSTED)
  71. case 501:
  72. return int32(code.Code_UNIMPLEMENTED)
  73. case 503:
  74. return int32(code.Code_UNAVAILABLE)
  75. default:
  76. return int32(code.Code_UNKNOWN)
  77. }
  78. }
  79. // TODO: (odeke-em): perhaps just pass around spans due to the cost
  80. // incurred from using trace.FromContext(ctx) yet we could avoid
  81. // throwing away the work done by ctx, span := trace.StartSpan.
  82. func TracePrintf(ctx context.Context, attrMap map[string]interface{}, format string, args ...interface{}) {
  83. var attrs []trace.Attribute
  84. for k, v := range attrMap {
  85. var a trace.Attribute
  86. switch v := v.(type) {
  87. case string:
  88. a = trace.StringAttribute(k, v)
  89. case bool:
  90. a = trace.BoolAttribute(k, v)
  91. case int:
  92. a = trace.Int64Attribute(k, int64(v))
  93. case int64:
  94. a = trace.Int64Attribute(k, v)
  95. default:
  96. a = trace.StringAttribute(k, fmt.Sprintf("%#v", v))
  97. }
  98. attrs = append(attrs, a)
  99. }
  100. trace.FromContext(ctx).Annotatef(attrs, format, args...)
  101. }