Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

69 lignes
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. package testutil
  15. import (
  16. "log"
  17. "time"
  18. "go.opencensus.io/plugin/ocgrpc"
  19. "go.opencensus.io/stats/view"
  20. "go.opencensus.io/trace"
  21. )
  22. // TestExporter is a test utility exporter. It should be created with NewtestExporter.
  23. type TestExporter struct {
  24. Spans []*trace.SpanData
  25. Stats chan *view.Data
  26. }
  27. // NewTestExporter creates a TestExporter and registers it with OpenCensus.
  28. func NewTestExporter() *TestExporter {
  29. te := &TestExporter{Stats: make(chan *view.Data)}
  30. view.RegisterExporter(te)
  31. view.SetReportingPeriod(time.Millisecond)
  32. if err := view.Register(ocgrpc.DefaultClientViews...); err != nil {
  33. log.Fatal(err)
  34. }
  35. trace.RegisterExporter(te)
  36. trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
  37. return te
  38. }
  39. // ExportSpan exports a span.
  40. func (te *TestExporter) ExportSpan(s *trace.SpanData) {
  41. te.Spans = append(te.Spans, s)
  42. }
  43. // ExportView exports a view.
  44. func (te *TestExporter) ExportView(vd *view.Data) {
  45. if len(vd.Rows) > 0 {
  46. select {
  47. case te.Stats <- vd:
  48. default:
  49. }
  50. }
  51. }
  52. // Unregister unregisters the exporter from OpenCensus.
  53. func (te *TestExporter) Unregister() {
  54. view.UnregisterExporter(te)
  55. trace.UnregisterExporter(te)
  56. view.SetReportingPeriod(0) // reset to default value
  57. }