All files / src stats.coffee

14% Statements 14/100
0% Branches 0/8
0% Functions 0/8
14.89% Lines 14/94
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 1671x 1x 1x 1x 1x 1x 1x 1x 1x   1x   1x                                                                                                                     1x                                                         1x                                                                                             1x                                      
config = require './config/config'
SDC = require "statsd-client"
statsdServer = config.get 'statsd'
application = config.get 'application'
logger = require "winston"
os = require 'os'
timer = new Date()
domain = os.hostname() + '.' + application.name
util = require 'util'
 
sdc = new SDC statsdServer
 
exports.incrementTransactionCount = (ctx, done) ->
  logger.info 'sending counts to statsd for ' + domain + '.' + ctx.authorisedChannel._id
  transactionStatus = ctx.transactionStatus
  try
    sdc.increment domain + '.channels' # Overall Counter
    sdc.increment domain + '.channels.' + transactionStatus #Overall Transaction Status
    sdc.increment domain + '.channels.' + ctx.authorisedChannel._id # Per channel
    sdc.increment domain + '.channels.' + ctx.authorisedChannel._id + '.statuses.' + transactionStatus # Per Channel Status
 
    if ctx.mediatorResponse?
      # Check for custom mediator metrics
      if ctx.mediatorResponse.metrics?
 
 
        for metric in ctx.mediatorResponse.metrics
 
          if metric.type == 'counter'
            logger.info 'incrementing mediator counter ' + metric.name
            sdc.increment domain + '.channels.' + ctx.authorisedChannel._id + '.' + ctx.mediatorResponse.properties.name + '.mediator_metrics.' + metric.name
 
          if metric.type == 'timer'
            logger.info 'incrementing mediator timer ' + metric.name
            sdc.timing domain + '.channels.' + ctx.authorisedChannel._id + '.' + ctx.mediatorResponse.properties.name + '.mediator_metrics.' + metric.name, metric.value
 
          if metric.type == 'gauge'
            logger.info 'incrementing mediator gauge ' + metric.name
            sdc.gauge domain + '.channels.' + ctx.authorisedChannel._id + '.' + ctx.mediatorResponse.properties.name + '.mediator_metrics.' + metric.name, metric.value
 
 
      if ctx.mediatorResponse.orchestrations?
        for orchestration in ctx.mediatorResponse.orchestrations
          do (orchestration) ->
            orchestrationStatus = orchestration.response.status
            orchestrationName = orchestration.name
            if orchestration.group
              orchestrationName = "#{orchestration.group}.#{orchestration.name}" #Namespace it by group
            sdc.increment domain + '.channels.' + ctx.authorisedChannel._id + '.orchestrations.' + orchestrationName
            sdc.increment domain + '.channels.' + ctx.authorisedChannel._id + '.orchestrations.' + orchestrationName + '.statusCodes.' + orchestrationStatus
            sdc.increment domain + '.channels.' + ctx.authorisedChannel._id + '.statuses.' + transactionStatus + '.orchestrations.' + orchestrationName
            sdc.increment domain + '.channels.' + ctx.authorisedChannel._id + '.statuses.' + transactionStatus + '.orchestrations.' + orchestrationName + '.statusCodes.' + orchestrationStatus
 
            if orchestration.metrics?
              for metric in orchestration.metrics
                if metric.type == 'counter'
                  logger.info 'incrementing orchestration counter ' + metric.name
                  sdc.increment   domain + '.channels.' + ctx.authorisedChannel._id + '.' + ctx.mediatorResponse.properties.name + '.orchestrations.' + orchestrationName + '.' + metric.name, metric.value
 
                if metric.type == 'timer'
                  logger.info 'incrementing orchestration timer ' + metric.name
                  sdc.timing      domain + '.channels.' + ctx.authorisedChannel._id + '.' + ctx.mediatorResponse.properties.name + '.orchestrations.' + orchestrationName + '.' + metric.name, metric.value
 
                if metric.type == 'gauge'
                  logger.info 'incrementing orchestration gauge ' + metric.name
                  sdc.gauge       domain + '.channels.' + ctx.authorisedChannel._id + '.' + ctx.mediatorResponse.properties.name + '.orchestrations.' + orchestrationName  + '.' + metric.name, metric.value
 
  catch error
    logger.error error, done
  done()
 
exports.measureTransactionDuration = (ctx, done) ->
  logger.info 'sending durations to statsd for ' + domain + '.' + ctx.authorisedChannel._id
  transactionStatus = ctx.transactionStatus
 
  try
    sdc.timing domain + '.channels'  , ctx.timer # Overall Timer
    sdc.timing domain + '.channels.' + transactionStatus, ctx.timer # Overall Transaction Status
    sdc.timing domain + '.channels.' + ctx.authorisedChannel._id, ctx.timer # Per Channel
    sdc.timing domain + '.channels.' + ctx.authorisedChannel._id + '.statuses.' + transactionStatus, ctx.timer # Per Channel Status
 
 
    if ctx.mediatorResponse?
      if ctx.mediatorResponse.orchestrations?
        for orchestration in ctx.mediatorResponse.orchestrations
          do (orchestration) ->
            orchestratrionDuration = orchestration.response.timestamp - orchestration.request.timestamp
            orchestrationStatus = orchestration.response.status
            orchestrationName = orchestration.name
            if orchestration.group
              orchestrationName = "#{orchestration.group}.#{orchestration.name}" #Namespace it by group
 
            sdc.timing domain + '.channels.' + ctx.authorisedChannel._id + '.orchestrations.' + orchestrationName, orchestratrionDuration
            sdc.timing domain + '.channels.' + ctx.authorisedChannel._id + '.orchestrations.' + orchestrationName + '.statusCodes.' + orchestrationStatus , orchestratrionDuration
 
 
  catch error
    logger.error error, done
  done()
 
exports.nonPrimaryRouteRequestCount = (ctx, route, done) ->
 
  sdc.increment domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name # Per non-primary route
  sdc.increment domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.statusCodes.' + route.response.status # Per route response status
 
  if route.metrics?
    for metric in route.metrics
      if metric.type == 'counter'
        logger.info 'incrementing mediator counter ' + metric.name
        sdc.increment domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.mediator_metrics.' + metric.name
 
      if metric.type == 'timer'
        logger.info 'incrementing mediator timer ' + metric.name
        sdc.timing domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.mediator_metrics.' + metric.name, metric.value
 
      if metric.type == 'gauge'
        logger.info 'incrementing mediator gauge ' + metric.name
        sdc.gauge domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.mediator_metrics.' + metric.name, metric.value
 
  if route.orchestrations?
    for orchestration in route.orchestrations
      do (orchestration) ->
        orchestrationStatus = orchestration.response.status
        orchestrationName = orchestration.name
        if orchestration.group
          orchestrationName = "#{orchestration.group}.#{orchestration.name}" #Namespace it by group
        sdc.increment domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.orchestrations.' + orchestrationName
        sdc.increment domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.orchestrations.' + orchestrationName + '.statusCodes.' + orchestrationStatus
        sdc.increment domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.statusCodes.' + route.response.status + '.orchestrations.' + orchestrationName
        sdc.increment domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.statusCodes.' + route.response.status + '.orchestrations.' + orchestrationName + '.statusCodes.' + orchestrationStatus
 
        # Log custom orchestration metrics
        if orchestration.metrics?
          for metric in orchestration.metrics
            if metric.type == 'counter'
              logger.info 'incrementing '+ route.name + ' orchestration counter ' + metric.name
              sdc.increment   domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.orchestrations.' + orchestrationName + '.' + metric.name, metric.value
 
            if metric.type == 'timer'
              logger.info 'incrementing '+ route.name + 'orchestration timer ' + metric.name
              sdc.timing      domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.orchestrations.' + orchestrationName + '.' + metric.name, metric.value
 
            if metric.type == 'gauge'
              logger.info 'incrementing '+ route.name + 'orchestration gauge ' + metric.name
              sdc.gauge       domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.orchestrations.' + orchestrationName  + '.' + metric.name, metric.value
  done()
 
exports.nonPrimaryRouteDurations = (ctx, route, done) ->
 
  sdc.timing domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name, ctx.timer
  sdc.timing domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.statusCodes.' + route.response.status, ctx.timer
 
  if route.orchestrations?
    for orchestration in route.orchestrations
      do (orchestration) ->
        orchestratrionDuration = orchestration.response.timestamp - orchestration.request.timestamp
        orchestrationStatus = orchestration.response.status
        orchestrationName = orchestration.name
        if orchestration.group
          orchestrationName = "#{orchestration.group}.#{orchestration.name}" #Namespace it by group
        sdc.timing domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.orchestrations.' + orchestrationName, orchestratrionDuration
        sdc.timing domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.orchestrations.' + orchestrationName + '.statusCodes.' + orchestrationStatus , orchestratrionDuration
        sdc.timing domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.statusCodes.' + route.response.status + '.orchestrations.' + orchestrationName, orchestratrionDuration
        sdc.timing domain + '.channels.' + ctx.authorisedChannel._id + '.nonPrimaryRoutes.' + route.name + '.statusCodes.' + route.response.status + '.orchestrations.' + orchestrationName + '.statusCodes.' + orchestrationStatus , orchestratrionDuration
 
  done()