main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2017, 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. // Command helloworld is an example program that collects data for
  15. // video size.
  16. package main
  17. import (
  18. "context"
  19. "fmt"
  20. "log"
  21. "math/rand"
  22. "time"
  23. "go.opencensus.io/examples/exporter"
  24. "go.opencensus.io/stats"
  25. "go.opencensus.io/stats/view"
  26. "go.opencensus.io/tag"
  27. "go.opencensus.io/trace"
  28. )
  29. var (
  30. // frontendKey allows us to breakdown the recorded data
  31. // by the frontend used when uploading the video.
  32. frontendKey tag.Key
  33. // videoSize will measure the size of processed videos.
  34. videoSize *stats.Int64Measure
  35. )
  36. func main() {
  37. ctx := context.Background()
  38. // Register an exporter to be able to retrieve
  39. // the data from the subscribed views.
  40. e, err := exporter.NewLogExporter(exporter.Options{ReportingInterval: time.Duration(time.Second)})
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. e.Start()
  45. defer e.Stop()
  46. defer e.Close()
  47. trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
  48. frontendKey, err = tag.NewKey("example.com/keys/frontend")
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. videoSize = stats.Int64("example.com/measure/video_size", "size of processed videos", stats.UnitBytes)
  53. view.SetReportingPeriod(2 * time.Second)
  54. // Create view to see the processed video size
  55. // distribution broken down by frontend.
  56. // Register will allow view data to be exported.
  57. if err := view.Register(&view.View{
  58. Name: "example.com/views/video_size",
  59. Description: "processed video size over time",
  60. TagKeys: []tag.Key{frontendKey},
  61. Measure: videoSize,
  62. Aggregation: view.Distribution(1<<16, 1<<32),
  63. }); err != nil {
  64. log.Fatalf("Cannot register view: %v", err)
  65. }
  66. // Process the video.
  67. process(ctx)
  68. // Wait for a duration longer than reporting duration to ensure the stats
  69. // library reports the collected data.
  70. fmt.Println("Wait longer than the reporting duration...")
  71. time.Sleep(4 * time.Second)
  72. }
  73. // process processes the video and instruments the processing
  74. // by creating a span and collecting metrics about the operation.
  75. func process(ctx context.Context) {
  76. ctx, err := tag.New(ctx,
  77. tag.Insert(frontendKey, "mobile-ios9.3.5"),
  78. )
  79. if err != nil {
  80. log.Fatal(err)
  81. }
  82. ctx, span := trace.StartSpan(ctx, "example.com/ProcessVideo")
  83. defer span.End()
  84. // Process video.
  85. // Record the processed video size.
  86. // Sleep for [1,10] milliseconds to fake work.
  87. time.Sleep(time.Duration(rand.Intn(10)+1) * time.Millisecond)
  88. stats.Record(ctx, videoSize.M(25648))
  89. }