Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

85 rader
2.6 KiB

  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. "go.opencensus.io/trace"
  18. "google.golang.org/api/googleapi"
  19. "google.golang.org/genproto/googleapis/rpc/code"
  20. "google.golang.org/grpc/status"
  21. )
  22. // StartSpan adds a span to the trace with the given name.
  23. func StartSpan(ctx context.Context, name string) context.Context {
  24. ctx, _ = trace.StartSpan(ctx, name)
  25. return ctx
  26. }
  27. // EndSpan ends a span with the given error.
  28. func EndSpan(ctx context.Context, err error) {
  29. span := trace.FromContext(ctx)
  30. if err != nil {
  31. span.SetStatus(toStatus(err))
  32. }
  33. span.End()
  34. }
  35. // ToStatus interrogates an error and converts it to an appropriate
  36. // OpenCensus status.
  37. func toStatus(err error) trace.Status {
  38. if err2, ok := err.(*googleapi.Error); ok {
  39. return trace.Status{Code: httpStatusCodeToOCCode(err2.Code), Message: err2.Message}
  40. } else if s, ok := status.FromError(err); ok {
  41. return trace.Status{Code: int32(s.Code()), Message: s.Message()}
  42. } else {
  43. return trace.Status{Code: int32(code.Code_UNKNOWN), Message: err.Error()}
  44. }
  45. }
  46. // TODO (deklerk): switch to using OpenCensus function when it becomes available.
  47. // Reference: https://github.com/googleapis/googleapis/blob/26b634d2724ac5dd30ae0b0cbfb01f07f2e4050e/google/rpc/code.proto
  48. func httpStatusCodeToOCCode(httpStatusCode int) int32 {
  49. switch httpStatusCode {
  50. case 200:
  51. return int32(code.Code_OK)
  52. case 499:
  53. return int32(code.Code_CANCELLED)
  54. case 500:
  55. return int32(code.Code_UNKNOWN) // Could also be Code_INTERNAL, Code_DATA_LOSS
  56. case 400:
  57. return int32(code.Code_INVALID_ARGUMENT) // Could also be Code_OUT_OF_RANGE
  58. case 504:
  59. return int32(code.Code_DEADLINE_EXCEEDED)
  60. case 404:
  61. return int32(code.Code_NOT_FOUND)
  62. case 409:
  63. return int32(code.Code_ALREADY_EXISTS) // Could also be Code_ABORTED
  64. case 403:
  65. return int32(code.Code_PERMISSION_DENIED)
  66. case 401:
  67. return int32(code.Code_UNAUTHENTICATED)
  68. case 429:
  69. return int32(code.Code_RESOURCE_EXHAUSTED)
  70. case 501:
  71. return int32(code.Code_UNIMPLEMENTED)
  72. case 503:
  73. return int32(code.Code_UNAVAILABLE)
  74. default:
  75. return int32(code.Code_UNKNOWN)
  76. }
  77. }