Skip to content

Commit

Permalink
Ease initial operation even further
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ferraz-oliveira committed Oct 31, 2022
1 parent 84cdcd2 commit 705b930
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ node index.js | ./node_modules/bunyan/bin/bunyan

Now start sending your metrics and watch the logs.

### Assumptions

* local port 8125 is not bound (this is our input)
* local port 8135 is bound to `statsd` input

## Linting

To lint
Expand Down
19 changes: 11 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import dgram from 'node:dgram'
import bunyan from 'bunyan'

const log = bunyan.createLogger({ name: 'dogstatsd-2-statsd', level: 'trace' })
const server = dgram.createSocket('udp4')

function trans(msg) {
log.trace(`server got: ${msg}`)
Expand Down Expand Up @@ -47,11 +46,15 @@ function trans(msg) {
return transMsg
}

server.on('message', (udp) => {
const msgs = udp.toString().split('\n')
msgs.forEach((msg) => {
trans(msg)
})
})
const udpServer = dgram.createSocket('udp4')
const udpClient = dgram.createSocket('udp4')

server.bind(8125)
udpServer
.on('message', (udp) => {
const msgs = udp.toString().split('\n')
msgs.forEach((msg) => {
const transMsg = trans(msg)
udpClient.send(transMsg, 8135, 'localhost')
})
})
.bind(8125)

0 comments on commit 705b930

Please sign in to comment.