|
| 1 | +import { flags } from '@salesforce/command'; |
| 2 | +import SfpowerscriptsCommand from '../../SfpowerscriptsCommand'; |
| 3 | +import SFPStatsSender from '@dxatscale/sfpowerscripts.core/lib/stats/SFPStatsSender'; |
| 4 | +import SFPLogger, { LoggerLevel, COLOR_KEY_MESSAGE } from '@dxatscale/sfp-logger'; |
| 5 | +import { Messages } from '@salesforce/core'; |
| 6 | + |
| 7 | +// Initialize Messages with the current plugin directory |
| 8 | +Messages.importMessagesDirectory(__dirname); |
| 9 | + |
| 10 | +// Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core, |
| 11 | +// or any library that is using the messages framework can also be loaded this way. |
| 12 | +const messages = Messages.loadMessages('@dxatscale/sfpowerscripts', 'metrics_report'); |
| 13 | + |
| 14 | +export default class Report extends SfpowerscriptsCommand { |
| 15 | + public static description = messages.getMessage('commandDescription'); |
| 16 | + |
| 17 | + protected static requiresDevhubUsername = false; |
| 18 | + protected static requiresProject = false; |
| 19 | + |
| 20 | + public static examples = ['$ sfpowerscripts metrics:report -m <metric> -t <type> -v <value>']; |
| 21 | + |
| 22 | + protected static flagsConfig = { |
| 23 | + metric: flags.string({ |
| 24 | + description: 'metrics to publish', |
| 25 | + required: true, |
| 26 | + char: 'm', |
| 27 | + }), |
| 28 | + type: flags.enum({ |
| 29 | + options: [ |
| 30 | + 'gauge', |
| 31 | + 'counter', |
| 32 | + 'timer', |
| 33 | + ], |
| 34 | + description: 'type of metric', |
| 35 | + required: true, |
| 36 | + char: 't', |
| 37 | + }), |
| 38 | + value: flags.string({ |
| 39 | + description: 'value of metric', |
| 40 | + required: true, |
| 41 | + char: 'v', |
| 42 | + }), |
| 43 | + tags: flags.string({ |
| 44 | + description: 'tags for metric', |
| 45 | + required: false, |
| 46 | + char: 'g', |
| 47 | + }), |
| 48 | + loglevel: flags.enum({ |
| 49 | + description: 'logging level for this command invocation', |
| 50 | + default: 'info', |
| 51 | + required: false, |
| 52 | + options: [ |
| 53 | + 'trace', |
| 54 | + 'debug', |
| 55 | + 'info', |
| 56 | + 'warn', |
| 57 | + 'error', |
| 58 | + 'fatal', |
| 59 | + 'TRACE', |
| 60 | + 'DEBUG', |
| 61 | + 'INFO', |
| 62 | + 'WARN', |
| 63 | + 'ERROR', |
| 64 | + 'FATAL', |
| 65 | + ], |
| 66 | + }), |
| 67 | + }; |
| 68 | + |
| 69 | + public async execute(): Promise<void> { |
| 70 | + this.validateEnvVars(); |
| 71 | + |
| 72 | + switch (this.flags.type) { |
| 73 | + case 'gauge': |
| 74 | + SFPLogger.log(COLOR_KEY_MESSAGE(`Publishing Gauge Metric ${this.flags.metric} with value ${this.flags.value}`)); |
| 75 | + SFPStatsSender.logGauge(this.flags.metric, this.flags.value, JSON.parse(this.flags.tags)); |
| 76 | + break; |
| 77 | + case 'counter': |
| 78 | + SFPLogger.log(COLOR_KEY_MESSAGE(`Publishing Count Metric ${this.flags.metric} with value ${this.flags.value}`)); |
| 79 | + SFPStatsSender.logCount(this.flags.metric, JSON.parse(this.flags.tags)); |
| 80 | + break; |
| 81 | + case 'timer': |
| 82 | + SFPLogger.log(COLOR_KEY_MESSAGE(`Publishing Elapsed Metric ${this.flags.metric} with value ${this.flags.value}`)); |
| 83 | + SFPStatsSender.logElapsedTime(this.flags.metric, Number.parseInt(this.flags.value), JSON.parse(this.flags.tags)); |
| 84 | + break; |
| 85 | + default: |
| 86 | + throw new Error('Invalid Metric Type'); |
| 87 | + }; |
| 88 | + |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + private validateEnvVars() { |
| 94 | + if ( |
| 95 | + !( |
| 96 | + process.env.SFPOWERSCRIPTS_STATSD || |
| 97 | + process.env.SFPOWERSCRIPTS_DATADOG || |
| 98 | + process.env.SFPOWERSCRIPTS_NEWRELIC || |
| 99 | + process.env.SFPOWERSCRIPTS_SPLUNK |
| 100 | + ) |
| 101 | + ) { |
| 102 | + throw new Error('Environment variable not set for metrics. No metrics will be published.'); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
0 commit comments