選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

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