|
| 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 | + |
| 15 | +#include <time.h> |
| 16 | +#include <cstdlib> |
| 17 | +#include <iostream> |
| 18 | +#include <memory> |
| 19 | +#include <string> |
| 20 | + |
| 21 | +#include "absl/strings/string_view.h" |
| 22 | +#include "absl/time/clock.h" |
| 23 | +#include "absl/time/time.h" |
| 24 | +#include "opencensus/exporters/stats/stdout/stdout_exporter.h" |
| 25 | +#include "opencensus/exporters/trace/stdout/stdout_exporter.h" |
| 26 | +#include "opencensus/stats/stats.h" |
| 27 | +#include "opencensus/trace/sampler.h" |
| 28 | +#include "opencensus/trace/span.h" |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +ABSL_CONST_INIT const absl::string_view kFrontendKey = "my.org/keys/frontend"; |
| 33 | +ABSL_CONST_INIT const absl::string_view kVideoSizeViewName = |
| 34 | + "my.org/views/video_size"; |
| 35 | +ABSL_CONST_INIT const absl::string_view kVideoSizeMeasureName = |
| 36 | + "my.org/measure/video_size"; |
| 37 | + |
| 38 | +// The resource owner defines and registers a measure. A function exposing a |
| 39 | +// function-local static is the recommended style, ensuring that the measure is |
| 40 | +// only registered once. |
| 41 | +opencensus::stats::MeasureInt VideoSizeMeasure() { |
| 42 | + static const opencensus::stats::MeasureInt video_size = |
| 43 | + opencensus::stats::MeasureRegistry::RegisterInt( |
| 44 | + kVideoSizeMeasureName, "MBy", "size of processed videos"); |
| 45 | + return video_size; |
| 46 | +} |
| 47 | + |
| 48 | +} // namespace |
| 49 | + |
| 50 | +// Simple program that collects data for video size. |
| 51 | +int main(int argc, char **argv) { |
| 52 | + if (argc != 1) { |
| 53 | + std::cerr << "Usage: " << argv[0] << "\n"; |
| 54 | + return 1; |
| 55 | + } |
| 56 | + srand(time(NULL)); |
| 57 | + |
| 58 | + // Register stdout exporters. |
| 59 | + opencensus::exporters::stats::StdoutExporter::Register(); |
| 60 | + opencensus::exporters::trace::StdoutExporter::Register(); |
| 61 | + |
| 62 | + // Call measure so that it is initialized. |
| 63 | + VideoSizeMeasure(); |
| 64 | + |
| 65 | + // Create view to see the processed video size distribution broken down by |
| 66 | + // frontend. The view has bucket boundaries (0, 256, 65536) that will group |
| 67 | + // measure values into histogram buckets. |
| 68 | + const opencensus::stats::ViewDescriptor video_size_view = |
| 69 | + opencensus::stats::ViewDescriptor() |
| 70 | + .set_name(kVideoSizeViewName) |
| 71 | + .set_description("processed video size over time") |
| 72 | + .set_measure(kVideoSizeMeasureName) |
| 73 | + .set_aggregation(opencensus::stats::Aggregation::Distribution( |
| 74 | + opencensus::stats::BucketBoundaries::Exponential(8, 1 << 8, 2))) |
| 75 | + .add_column(kFrontendKey); |
| 76 | + opencensus::stats::View view(video_size_view); |
| 77 | + video_size_view.RegisterForExport(); |
| 78 | + |
| 79 | + opencensus::trace::AlwaysSampler sampler; |
| 80 | + auto span = opencensus::trace::Span::StartSpan("my.org/ProcessVideo", nullptr, |
| 81 | + {&sampler}); |
| 82 | + |
| 83 | + // Process video. |
| 84 | + // Record the processed video size. |
| 85 | + span.AddAnnotation("Start processing video."); |
| 86 | + // Sleep for [1,10] milliseconds to fake work. |
| 87 | + absl::SleepFor(absl::Milliseconds(rand() % 10 + 1)); |
| 88 | + opencensus::stats::Record({{VideoSizeMeasure(), 25648}}, |
| 89 | + {{kFrontendKey, "video size"}}); |
| 90 | + span.AddAnnotation("Finished processing video."); |
| 91 | + span.End(); |
| 92 | + |
| 93 | + // Sleep for ~5 seconds to ensure that exporters will process the span. |
| 94 | + std::cout << "Wait longer than the reporting duration...\n"; |
| 95 | + absl::SleepFor(absl::Milliseconds(5100)); |
| 96 | + |
| 97 | + // Sleep while exporters run in the background. |
| 98 | + std::cout << "Views:\n" << video_size_view.DebugString() << "\n"; |
| 99 | + const auto data = view.GetData(); |
| 100 | + if (data.type() == opencensus::stats::ViewData::Type::kDistribution) { |
| 101 | + for (auto &it : data.distribution_data()) { |
| 102 | + for (auto &name : it.first) std::cout << name << " : "; |
| 103 | + std::cout << it.second.DebugString() << "\n"; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments