From 6bd9542f86351c7a25a2cf817d0ce5dda776a608 Mon Sep 17 00:00:00 2001 From: xuan-cao-swi Date: Wed, 20 Sep 2023 11:09:44 -0400 Subject: [PATCH] NH-58499: move custom_metrics to api and add test --- lib/solarwinds_apm.rb | 1 - lib/solarwinds_apm/api.rb | 2 + .../{sdk => api}/custom_metrics.rb | 14 ++--- lib/solarwinds_apm/sdk.rb | 10 --- test/api/custom_metrics_test.rb | 62 +++++++++++++++++++ test/minitest_helper.rb | 19 ++++++ 6 files changed, 90 insertions(+), 18 deletions(-) rename lib/solarwinds_apm/{sdk => api}/custom_metrics.rb (91%) delete mode 100644 lib/solarwinds_apm/sdk.rb create mode 100644 test/api/custom_metrics_test.rb diff --git a/lib/solarwinds_apm.rb b/lib/solarwinds_apm.rb index 4f32667a..025b0ad0 100644 --- a/lib/solarwinds_apm.rb +++ b/lib/solarwinds_apm.rb @@ -12,7 +12,6 @@ require 'solarwinds_apm/support_report' require 'solarwinds_apm/constants' require 'solarwinds_apm/api' - require 'solarwinds_apm/sdk' require 'solarwinds_apm/base' require 'solarwinds_apm/logger' require 'solarwinds_apm/config' diff --git a/lib/solarwinds_apm/api.rb b/lib/solarwinds_apm/api.rb index e2fb1852..e471e9d8 100644 --- a/lib/solarwinds_apm/api.rb +++ b/lib/solarwinds_apm/api.rb @@ -5,6 +5,7 @@ require_relative './api/current_trace_info' require_relative './api/tracing' require_relative './api/opentelemetry' +require_relative './api/custom_metrics' module SolarWindsAPM module API @@ -12,5 +13,6 @@ module API extend SolarWindsAPM::API::CurrentTraceInfo extend SolarWindsAPM::API::Tracing extend SolarWindsAPM::API::OpenTelemetry + extend SolarWindsAPM::API::CustomMetrics end end \ No newline at end of file diff --git a/lib/solarwinds_apm/sdk/custom_metrics.rb b/lib/solarwinds_apm/api/custom_metrics.rb similarity index 91% rename from lib/solarwinds_apm/sdk/custom_metrics.rb rename to lib/solarwinds_apm/api/custom_metrics.rb index d25426e4..49b8db12 100644 --- a/lib/solarwinds_apm/sdk/custom_metrics.rb +++ b/lib/solarwinds_apm/api/custom_metrics.rb @@ -1,5 +1,5 @@ module SolarWindsAPM - module SDK + module API module CustomMetrics # Send counts # @@ -17,19 +17,19 @@ module CustomMetrics # class WorkTracker # def counting(name, tags = {}) # yield # yield to where work is done - # SolarWindsAPM::SDK.increment_metric(name, 1, false, tags) + # SolarWindsAPM::API.increment_metric(name, 1, false, tags) # end # end # # === Returns: - # * 0 on success, error code on failure + # * Boolean # def increment_metric(name, count=1, with_hostname=false, tags_kvs={}) # rubocop:disable Style/OptionalBooleanParameter return true unless SolarWindsAPM.loaded with_hostname = with_hostname ? 1 : 0 tags, tags_count = make_tags(tags_kvs) - SolarWindsAPM::CustomMetrics.increment(name.to_s, count, with_hostname, nil, tags, tags_count) == 1 + SolarWindsAPM::CustomMetrics.increment(name.to_s, count, with_hostname, nil, tags, tags_count) == 0 end # Send values with counts @@ -51,19 +51,19 @@ def increment_metric(name, count=1, with_hostname=false, tags_kvs={}) # rubocop: # start = Time.now # yield # yield to where work is done # duration = Time.now - start - # SolarWindsAPM::SDK.summary_metric(name, duration, 1, false, tags) + # SolarWindsAPM::API.summary_metric(name, duration, 1, false, tags) # end # end # # === Returns: - # * 0 on success, error code on failure + # * Boolean # def summary_metric(name, value, count=1, with_hostname=false, tags_kvs={}) # rubocop:disable Style/OptionalBooleanParameter return true unless SolarWindsAPM.loaded with_hostname = with_hostname ? 1 : 0 tags, tags_count = make_tags(tags_kvs) - SolarWindsAPM::CustomMetrics.summary(name.to_s, value, count, with_hostname, nil, tags, tags_count) == 1 + SolarWindsAPM::CustomMetrics.summary(name.to_s, value, count, with_hostname, nil, tags, tags_count) == 0 end private diff --git a/lib/solarwinds_apm/sdk.rb b/lib/solarwinds_apm/sdk.rb deleted file mode 100644 index 35bb197b..00000000 --- a/lib/solarwinds_apm/sdk.rb +++ /dev/null @@ -1,10 +0,0 @@ -# Copyright (c) 2023 SolarWinds, LLC. -# All rights reserved. - -require_relative './sdk/custom_metrics' - -module SolarWindsAPM - module SDK - extend SolarWindsAPM::SDK::CustomMetrics - end -end \ No newline at end of file diff --git a/test/api/custom_metrics_test.rb b/test/api/custom_metrics_test.rb new file mode 100644 index 00000000..7cb3ec51 --- /dev/null +++ b/test/api/custom_metrics_test.rb @@ -0,0 +1,62 @@ +# Copyright (c) 2023 SolarWinds, LLC. +# All rights reserved. + +require 'minitest_helper' +require 'minitest/mock' +require './lib/solarwinds_apm/api' + +describe 'SolarWinds Custom Metrics Test' do + it 'test_increment_metric_with_single_variable_with_successful_liboboe_metric_call' do + SolarWindsAPM::CustomMetrics.stub(:increment, 0) do + SolarWindsAPM::MetricTags.stub(:new, {}) do + result = SolarWindsAPM::API.increment_metric('abc') + _(result).must_equal true + end + end + end + + it 'test_increment_metric_with_single_variable_with_failed_liboboe_metric_call' do + SolarWindsAPM::CustomMetrics.stub(:increment, 1) do + SolarWindsAPM::MetricTags.stub(:new, {}) do + result = SolarWindsAPM::API.increment_metric('abc') + _(result).must_equal false + end + end + end + + it 'test_increment_metric_with_single_variable_with_success_variables' do + SolarWindsAPM::CustomMetrics.stub(:increment, 0) do + SolarWindsAPM::MetricTags.stub(:new, {}) do + result = SolarWindsAPM::API.increment_metric('abc', 1, true, {}) + _(result).must_equal true + end + end + end + + it 'test_summary_metric_with_single_variable_with_success' do + SolarWindsAPM::CustomMetrics.stub(:summary, 0) do + SolarWindsAPM::MetricTags.stub(:new, {}) do + result = SolarWindsAPM::API.summary_metric('abc', 1, true, {}) + _(result).must_equal true + end + end + end + + it 'test_summary_metric_with_single_variable_with_failure' do + SolarWindsAPM::CustomMetrics.stub(:summary, 1) do + SolarWindsAPM::MetricTags.stub(:new, {}) do + result = SolarWindsAPM::API.summary_metric('abc', 1, true, {}) + _(result).must_equal false + end + end + end + + it 'test_summary_metric_with_single_variable_with_failure' do + SolarWindsAPM::CustomMetrics.stub(:summary, 0) do + SolarWindsAPM::MetricTags.stub(:new, {}) do + result = SolarWindsAPM::API.summary_metric('abc', 7.7, 1, true, {} ) + _(result).must_equal true + end + end + end +end diff --git a/test/minitest_helper.rb b/test/minitest_helper.rb index 931997bb..5d4587d8 100644 --- a/test/minitest_helper.rb +++ b/test/minitest_helper.rb @@ -153,6 +153,25 @@ class << self end end +# for custom_metrics_test.rb +module SolarWindsAPM + module CustomMetrics + def self.increment + end + def self.summary + end + end +end + +module SolarWindsAPM + module MetricTags + def self.new + end + def self.add + end + end +end + ## # create_span_data #