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.
 
 
 

82 lines
2.4 KiB

  1. // Copyright 2017 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 spanner
  15. import (
  16. "context"
  17. "fmt"
  18. "go.opencensus.io/stats"
  19. "go.opencensus.io/stats/view"
  20. "go.opencensus.io/trace"
  21. )
  22. func startSpan(ctx context.Context, name string) context.Context {
  23. ctx, _ = trace.StartSpan(ctx, name)
  24. return ctx
  25. }
  26. func endSpan(ctx context.Context, err error) {
  27. span := trace.FromContext(ctx)
  28. if err != nil {
  29. // TODO(jba): Add error code to the status.
  30. span.SetStatus(trace.Status{Message: err.Error()})
  31. }
  32. span.End()
  33. }
  34. func statsPrintf(ctx context.Context, attrMap map[string]interface{}, format string, args ...interface{}) {
  35. var attrs []trace.Attribute
  36. for k, v := range attrMap {
  37. var a trace.Attribute
  38. switch v := v.(type) {
  39. case string:
  40. a = trace.StringAttribute(k, v)
  41. case bool:
  42. a = trace.BoolAttribute(k, v)
  43. case int:
  44. a = trace.Int64Attribute(k, int64(v))
  45. case int64:
  46. a = trace.Int64Attribute(k, v)
  47. default:
  48. a = trace.StringAttribute(k, fmt.Sprintf("%#v", v))
  49. }
  50. attrs = append(attrs, a)
  51. }
  52. trace.FromContext(ctx).Annotatef(attrs, format, args...)
  53. }
  54. const statsPrefix = "cloud.google.com/go/spanner/"
  55. func recordStat(ctx context.Context, m *stats.Int64Measure, n int64) {
  56. stats.Record(ctx, m.M(n))
  57. }
  58. var (
  59. // OpenSessionCount is a measure of the number of sessions currently opened.
  60. // It is EXPERIMENTAL and subject to change or removal without notice.
  61. OpenSessionCount = stats.Int64(statsPrefix+"open_session_count", "Number of sessions currently opened",
  62. stats.UnitDimensionless)
  63. // OpenSessionCountView is a view of the last value of OpenSessionCount.
  64. // It is EXPERIMENTAL and subject to change or removal without notice.
  65. OpenSessionCountView = &view.View{
  66. Name: OpenSessionCount.Name(),
  67. Description: OpenSessionCount.Description(),
  68. Measure: OpenSessionCount,
  69. Aggregation: view.LastValue(),
  70. }
  71. )