|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +require_relative '../reporter/client/proto' |
| 17 | + |
| 18 | +module Skywalking |
| 19 | + module Meter |
| 20 | + # Base class for all data sources |
| 21 | + class DataSource |
| 22 | + # Automatically register all generator methods as gauges |
| 23 | + # @param meter_service [MeterService] the service to register gauges with |
| 24 | + def register(meter_service) |
| 25 | + methods.grep(/_generator$/).each do |method_name| |
| 26 | + metric_name = "instance_ruby_#{method_name.to_s.sub('_generator', '')}" |
| 27 | + # Create a lambda that calls the generator method |
| 28 | + getter = lambda { send(method_name) } |
| 29 | + gauge = Gauge.new(metric_name, getter) |
| 30 | + meter_service.register(gauge) |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + # Represents a gauge metric that reports instantaneous values |
| 36 | + class Gauge |
| 37 | + attr_reader :name |
| 38 | + |
| 39 | + # @param name [String] metric name |
| 40 | + # @param getter [Proc] a callable that returns the current value |
| 41 | + def initialize(name, getter) |
| 42 | + @name = name |
| 43 | + @getter = getter |
| 44 | + @labels = [] |
| 45 | + end |
| 46 | + |
| 47 | + # Add a label to this gauge |
| 48 | + # @param key [String] label key |
| 49 | + # @param value [String] label value |
| 50 | + # @return [self] |
| 51 | + def add_label(key, value) |
| 52 | + @labels << Label.new(name: key, value: value) |
| 53 | + self |
| 54 | + end |
| 55 | + |
| 56 | + # Collect current metric value |
| 57 | + # @return [MeterData] meter data |
| 58 | + def collect |
| 59 | + value = @getter.call |
| 60 | + MeterData.new( |
| 61 | + singleValue: MeterSingleValue.new( |
| 62 | + name: @name, |
| 63 | + value: value.to_f, |
| 64 | + labels: @labels |
| 65 | + ) |
| 66 | + ) |
| 67 | + rescue |
| 68 | + # Return zero value if getter fails |
| 69 | + MeterData.new( |
| 70 | + singleValue: MeterSingleValue.new( |
| 71 | + name: @name, |
| 72 | + value: 0.0, |
| 73 | + labels: @labels |
| 74 | + ) |
| 75 | + ) |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | +end |
0 commit comments