-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from solarwindscloud/NH-58499
NH-58499: poc for backward compatibility for solarwinds_apm
- Loading branch information
Showing
4 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
# Copyright (c) 2016 SolarWinds, LLC. | ||
# Copyright (c) 2023 SolarWinds, LLC. | ||
# All rights reserved. | ||
|
||
require_relative './api/transaction_name' | ||
require_relative './api/current_trace_info' | ||
require_relative './api/tracing' | ||
require_relative './api/opentelemetry' | ||
require_relative './api/custom_metrics' | ||
|
||
module SolarWindsAPM | ||
module API | ||
extend SolarWindsAPM::API::TransactionName | ||
extend SolarWindsAPM::API::CurrentTraceInfo | ||
extend SolarWindsAPM::API::Tracing | ||
extend SolarWindsAPM::API::OpenTelemetry | ||
extend SolarWindsAPM::API::CustomMetrics | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
module SolarWindsAPM | ||
module API | ||
module CustomMetrics | ||
# Send counts | ||
# | ||
# Use this method to report the number of times an action occurs. The metric counts reported are summed and flushed every 60 seconds. | ||
# | ||
# === Arguments: | ||
# | ||
# * +name+ (String) Name to be used for the metric. Must be 255 or fewer characters and consist only of A-Za-z0-9.:-* | ||
# * +count+ (Integer, optional, default = 1): Count of actions being reported | ||
# * +with_hostname+ (Boolean, optional, default = false): Indicates if the host name should be included as a tag for the metric | ||
# * +tags_kvs+ (Hash, optional): List of key/value pairs to describe the metric. The key must be <= 64 characters, the value must be <= 255 characters, allowed characters: A-Za-z0-9.:-_ | ||
# | ||
# === Example: | ||
# | ||
# class WorkTracker | ||
# def counting(name, tags = {}) | ||
# yield # yield to where work is done | ||
# SolarWindsAPM::API.increment_metric(name, 1, false, tags) | ||
# end | ||
# end | ||
# | ||
# === Returns: | ||
# * 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) == 0 | ||
end | ||
|
||
# Send values with counts | ||
# | ||
# Use this method to report a value for each or multiple counts. The metric values reported are aggregated and flushed every 60 seconds. The dashboard displays the average value per count. | ||
# | ||
# === Arguments: | ||
# | ||
# * +name+ (String) Name to be used for the metric. Must be 255 or fewer characters and consist only of A-Za-z0-9.:-* | ||
# * +value+ (Numeric) Value to be added to the current sum | ||
# * +count+ (Integer, optional, default = 1): Count of actions being reported | ||
# * +with_hostname+ (Boolean, optional, default = false): Indicates if the host name should be included as a tag for the metric | ||
# * +tags_kvs+ (Hash, optional): List of key/value pairs to describe the metric. The key must be <= 64 characters, the value must be <= 255 characters, allowed characters: A-Za-z0-9.:-_ | ||
# | ||
# === Example: | ||
# | ||
# class WorkTracker | ||
# def timing(name, tags = {}) | ||
# start = Time.now | ||
# yield # yield to where work is done | ||
# duration = Time.now - start | ||
# SolarWindsAPM::API.summary_metric(name, duration, 1, false, tags) | ||
# end | ||
# end | ||
# | ||
# === Returns: | ||
# * 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) == 0 | ||
end | ||
|
||
private | ||
|
||
def make_tags(tags_kvs) | ||
unless tags_kvs.is_a?(Hash) | ||
SolarWindsAPM.logger.warn("[solarwinds_apm/metrics] CustomMetrics received tags_kvs that are not a Hash (found #{tags_kvs.class}), setting tags_kvs = {}") | ||
tags_kvs = {} | ||
end | ||
count = tags_kvs.size | ||
tags = SolarWindsAPM::MetricTags.new(count) | ||
|
||
tags_kvs.each_with_index do |(k, v), i| | ||
tags.add(i, k.to_s, v.to_s) | ||
end | ||
|
||
[tags, count] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters