From 2c2f95508f2d5f3a463cae8ffa43ac9fea1030d0 Mon Sep 17 00:00:00 2001
From: John Harris <john.harris@nexusmods.com>
Date: Thu, 1 Sep 2022 13:25:50 +0100
Subject: [PATCH] feat: document singleton usage and simplify client metrics
 usage

---
 README.md                                      | 12 ++++++++++++
 lib/nexus_semantic_logger.rb                   |  5 +++++
 lib/nexus_semantic_logger/datadog_singleton.rb |  3 ++-
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 8a4ecb0..21c4ff6 100644
--- a/README.md
+++ b/README.md
@@ -11,6 +11,18 @@ As well as providing a semantic logger, this gem handles datadog telemetry assoc
 * metrics
    * statsd is automatically attached to datadog runtime metrics and may also be used for custom metrics.
 
+### Sending metrics
+
+Ensure the metric name is in the format: `nexus.{component}.{major}.{minor}`
+
+Where _major and minor_ are specific to the component logic e.g. `nexus.uploads.clamscan.pass`
+
+For example, to increment a count:
+
+```
+NexusSemanticLogger.metrics.increment('nexus.users.registration.complete')
+```
+
 # Local gem development
 
 Steps to run this gem from local sources in one the nexus 'staged build' rails components:
diff --git a/lib/nexus_semantic_logger.rb b/lib/nexus_semantic_logger.rb
index 6867bf4..99e6494 100644
--- a/lib/nexus_semantic_logger.rb
+++ b/lib/nexus_semantic_logger.rb
@@ -6,4 +6,9 @@ require 'nexus_semantic_logger/datadog_singleton'
 require 'nexus_semantic_logger/datadog_tracer'
 
 module NexusSemanticLogger
+
+  # Get application wide object for sending metrics.
+  def metrics
+    DatadogSingleton.instance
+  end
 end
diff --git a/lib/nexus_semantic_logger/datadog_singleton.rb b/lib/nexus_semantic_logger/datadog_singleton.rb
index c270974..927b26a 100644
--- a/lib/nexus_semantic_logger/datadog_singleton.rb
+++ b/lib/nexus_semantic_logger/datadog_singleton.rb
@@ -3,7 +3,8 @@ require 'singleton'
 
 module NexusSemanticLogger
   # Application wide location to get datadog objects.
-  # Can be moved to its own gem in future, and there is scope to make the usage code even leaner.
+  # dogstatsd-ruby maintains its own queue and thread for flushing, so the client code should never create its
+  # own statsd instance.
   class DatadogSingleton
     include Singleton
     attr_accessor :statsd, :tags
-- 
GitLab