You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

69 lines
1.8 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 bigtable
  16. import (
  17. "fmt"
  18. "go.opencensus.io/plugin/ocgrpc"
  19. "go.opencensus.io/trace"
  20. "golang.org/x/net/context"
  21. "google.golang.org/api/option"
  22. "google.golang.org/grpc"
  23. )
  24. func openCensusOptions() []option.ClientOption {
  25. return []option.ClientOption{
  26. option.WithGRPCDialOption(grpc.WithStatsHandler(&ocgrpc.ClientHandler{})),
  27. }
  28. }
  29. func traceStartSpan(ctx context.Context, name string) context.Context {
  30. ctx, _ = trace.StartSpan(ctx, name)
  31. return ctx
  32. }
  33. func traceEndSpan(ctx context.Context, err error) {
  34. span := trace.FromContext(ctx)
  35. if err != nil {
  36. span.SetStatus(trace.Status{Message: err.Error()})
  37. }
  38. span.End()
  39. }
  40. func tracePrintf(ctx context.Context, attrMap map[string]interface{}, format string, args ...interface{}) {
  41. var attrs []trace.Attribute
  42. for k, v := range attrMap {
  43. var a trace.Attribute
  44. switch v := v.(type) {
  45. case string:
  46. a = trace.StringAttribute(k, v)
  47. case bool:
  48. a = trace.BoolAttribute(k, v)
  49. case int:
  50. a = trace.Int64Attribute(k, int64(v))
  51. case int64:
  52. a = trace.Int64Attribute(k, v)
  53. default:
  54. a = trace.StringAttribute(k, fmt.Sprintf("%#v", v))
  55. }
  56. attrs = append(attrs, a)
  57. }
  58. trace.FromContext(ctx).Annotatef(attrs, format, args...)
  59. }