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.
 
 
 

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