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