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.
 
 
 

101 line
2.9 KiB

  1. // Copyright 2018, OpenCensus Authors
  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 trace_test
  15. import (
  16. "context"
  17. "testing"
  18. "go.opencensus.io/stats"
  19. "go.opencensus.io/stats/view"
  20. "go.opencensus.io/trace"
  21. )
  22. func TestTraceExemplar(t *testing.T) {
  23. m := stats.Float64("measure."+t.Name(), "", stats.UnitDimensionless)
  24. v := &view.View{
  25. Measure: m,
  26. Aggregation: view.Distribution(1, 2, 3),
  27. }
  28. view.Register(v)
  29. ctx := context.Background()
  30. ctx, span := trace.StartSpan(ctx, t.Name(), trace.WithSampler(trace.AlwaysSample()))
  31. stats.Record(ctx, m.M(1.5))
  32. span.End()
  33. rows, err := view.RetrieveData(v.Name)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. if len(rows) == 0 {
  38. t.Fatal("len(rows) = 0; want > 0")
  39. }
  40. dd := rows[0].Data.(*view.DistributionData)
  41. if got := len(dd.ExemplarsPerBucket); got < 3 {
  42. t.Fatalf("len(dd.ExemplarsPerBucket) = %d; want >= 2", got)
  43. }
  44. exemplar := dd.ExemplarsPerBucket[1]
  45. if exemplar == nil {
  46. t.Fatal("Expected exemplar")
  47. }
  48. if got, want := exemplar.Value, 1.5; got != want {
  49. t.Fatalf("exemplar.Value = %v; got %v", got, want)
  50. }
  51. if _, ok := exemplar.Attachments["trace_id"]; !ok {
  52. t.Fatalf("exemplar.Attachments = %v; want trace_id key", exemplar.Attachments)
  53. }
  54. if _, ok := exemplar.Attachments["span_id"]; !ok {
  55. t.Fatalf("exemplar.Attachments = %v; want span_id key", exemplar.Attachments)
  56. }
  57. }
  58. func TestTraceExemplar_notSampled(t *testing.T) {
  59. m := stats.Float64("measure."+t.Name(), "", stats.UnitDimensionless)
  60. v := &view.View{
  61. Measure: m,
  62. Aggregation: view.Distribution(1, 2, 3),
  63. }
  64. view.Register(v)
  65. ctx := context.Background()
  66. ctx, span := trace.StartSpan(ctx, t.Name(), trace.WithSampler(trace.NeverSample()))
  67. stats.Record(ctx, m.M(1.5))
  68. span.End()
  69. rows, err := view.RetrieveData(v.Name)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. if len(rows) == 0 {
  74. t.Fatal("len(rows) = 0; want > 0")
  75. }
  76. dd := rows[0].Data.(*view.DistributionData)
  77. if got := len(dd.ExemplarsPerBucket); got < 3 {
  78. t.Fatalf("len(buckets) = %d; want >= 2", got)
  79. }
  80. exemplar := dd.ExemplarsPerBucket[1]
  81. if exemplar == nil {
  82. t.Fatal("Expected exemplar")
  83. }
  84. if got, want := exemplar.Value, 1.5; got != want {
  85. t.Fatalf("exemplar.Value = %v; got %v", got, want)
  86. }
  87. if _, ok := exemplar.Attachments["trace_id"]; ok {
  88. t.Fatalf("exemplar.Attachments = %v; want no trace_id", exemplar.Attachments)
  89. }
  90. if _, ok := exemplar.Attachments["span_id"]; ok {
  91. t.Fatalf("exemplar.Attachments = %v; want span_id key", exemplar.Attachments)
  92. }
  93. }