Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

100 Zeilen
2.8 KiB

  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 := &exporter.PrintExporter{}
  41. view.RegisterExporter(e)
  42. trace.RegisterExporter(e)
  43. var err error
  44. frontendKey, err = tag.NewKey("example.com/keys/frontend")
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. videoSize = stats.Int64("example.com/measure/video_size", "size of processed videos", stats.UnitBytes)
  49. view.SetReportingPeriod(2 * time.Second)
  50. // Create view to see the processed video size
  51. // distribution broken down by frontend.
  52. // Register will allow view data to be exported.
  53. if err := view.Register(&view.View{
  54. Name: "example.com/views/video_size",
  55. Description: "processed video size over time",
  56. TagKeys: []tag.Key{frontendKey},
  57. Measure: videoSize,
  58. Aggregation: view.Distribution(1<<16, 1<<32),
  59. }); err != nil {
  60. log.Fatalf("Cannot register view: %v", err)
  61. }
  62. // Process the video.
  63. process(ctx)
  64. // Wait for a duration longer than reporting duration to ensure the stats
  65. // library reports the collected data.
  66. fmt.Println("Wait longer than the reporting duration...")
  67. time.Sleep(2 * time.Second)
  68. }
  69. // process processes the video and instruments the processing
  70. // by creating a span and collecting metrics about the operation.
  71. func process(ctx context.Context) {
  72. ctx, err := tag.New(ctx,
  73. tag.Insert(frontendKey, "mobile-ios9.3.5"),
  74. )
  75. if err != nil {
  76. log.Fatal(err)
  77. }
  78. ctx, span := trace.StartSpan(ctx, "example.com/ProcessVideo")
  79. defer span.End()
  80. // Process video.
  81. // Record the processed video size.
  82. // Sleep for [1,10] milliseconds to fake work.
  83. time.Sleep(time.Duration(rand.Intn(10)+1) * time.Millisecond)
  84. stats.Record(ctx, videoSize.M(25648))
  85. }