Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

84 рядки
2.5 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 prometheus is an example program that collects data for
  15. // video size. Collected data is exported to Prometheus.
  16. package main
  17. import (
  18. "context"
  19. "log"
  20. "math/rand"
  21. "net/http"
  22. "time"
  23. "go.opencensus.io/exporter/prometheus"
  24. "go.opencensus.io/stats"
  25. "go.opencensus.io/stats/view"
  26. )
  27. // Create measures. The program will record measures for the size of
  28. // processed videos and the number of videos marked as spam.
  29. var (
  30. videoCount = stats.Int64("example.com/measures/video_count", "number of processed videos", stats.UnitDimensionless)
  31. videoSize = stats.Int64("example.com/measures/video_size", "size of processed video", stats.UnitBytes)
  32. )
  33. func main() {
  34. ctx := context.Background()
  35. exporter, err := prometheus.NewExporter(prometheus.Options{})
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. view.RegisterExporter(exporter)
  40. // Create view to see the number of processed videos cumulatively.
  41. // Create view to see the amount of video processed
  42. // Subscribe will allow view data to be exported.
  43. // Once no longer needed, you can unsubscribe from the view.
  44. if err = view.Register(
  45. &view.View{
  46. Name: "video_count",
  47. Description: "number of videos processed over time",
  48. Measure: videoCount,
  49. Aggregation: view.Count(),
  50. },
  51. &view.View{
  52. Name: "video_size",
  53. Description: "processed video size over time",
  54. Measure: videoSize,
  55. Aggregation: view.Distribution(0, 1<<16, 1<<32),
  56. },
  57. ); err != nil {
  58. log.Fatalf("Cannot register the view: %v", err)
  59. }
  60. // Set reporting period to report data at every second.
  61. view.SetReportingPeriod(1 * time.Second)
  62. // Record some data points...
  63. go func() {
  64. for {
  65. stats.Record(ctx, videoCount.M(1), videoSize.M(rand.Int63()))
  66. <-time.After(time.Millisecond * time.Duration(1+rand.Intn(400)))
  67. }
  68. }()
  69. addr := ":9999"
  70. log.Printf("Serving at %s", addr)
  71. http.Handle("/metrics", exporter)
  72. log.Fatal(http.ListenAndServe(addr, nil))
  73. }