Skip to content

Commit

Permalink
Dockerfile (#2)
Browse files Browse the repository at this point in the history
* Ease debugging

* Ease consumption via a Dockerfile and Docker Hub container
  • Loading branch information
paulo-ferraz-oliveira authored Dec 30, 2022
1 parent 705b930 commit 0df5940
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 490 deletions.
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.eslintrc.yml
.git
.gitignore
.prettierrc
README.md
node_modules
package-lock.json
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM graphiteapp/graphite-statsd:1.1.10-4

ARG VSN=1.0.0

WORKDIR /etc/service/dogstatsd-2-statsd

# Read /entrypoint to understand why we use run and /etc/service
RUN set -ex \
&& sed -i 's/8125/8135/g' /opt/statsd/config/udp.js \
&& apk add npm git \
&& git clone --depth 1 --branch $VSN https://github.com/paulo-ferraz-oliveira/dogstatsd-2-statsd.git . \
&& npm install --omit=dev \
&& echo -e "#!/bin/bash\n\nexec node index.js" > run \
&& chmod +x run

ENTRYPOINT /entrypoint
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@ We implement a very basic translation layer for when you're using
Datadog as a remote sink for your metrics but, e.g. locally you're using
Graphite.

## Reference
## References

* Datadog: Datagram Format and Shell Usage:
* <https://docs.datadoghq.com/developers/dogstatsd/datagram_shell?tab=metrics>

* Stats: StatsD Metric Types:
* <https://github.com/statsd/statsd/blob/master/docs/metric_types.md>

## Docker

Find a Docker container, created from our [Dockerfile](Dockerfile), at
[Docker Hub: pauloferrazoliveira/dogstatsd-2-statsd](https://hub.docker.com/repository/docker/pauloferrazoliveira/dogstatsd-2-statsd).

## Usage

To use for the first time:

```shell
npm install
npm install --omit=dev
```

Make sure your 8125 is not bound and then:

```shell
node index.js | ./node_modules/bunyan/bin/bunyan
node index.js
```

Now start sending your metrics and watch the logs.
Expand All @@ -33,6 +38,18 @@ Now start sending your metrics and watch the logs.
* local port 8125 is not bound (this is our input)
* local port 8135 is bound to `statsd` input

## Contributing

First, make sure you have `yarn` installed locally.

Then run

```shell
npm install
```

to install dev dependencies.

## Linting

To lint
Expand Down
15 changes: 7 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import dgram from 'node:dgram'
import bunyan from 'bunyan'

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

function trans(msg) {
log.trace(`server got: ${msg}`)
console.log(`SRC: ${msg}`)

// Very dumb translation, but hopefully serves _all_ our purposes.
// Assumes input is correctly formatted as Datadog.
Expand Down Expand Up @@ -40,21 +37,23 @@ function trans(msg) {
if (sampleRate) {
transMsg += `|@${sampleRate}`
}

log.trace(`transformed it as ${transMsg}`)
console.log(`DST: ${transMsg}`)

return transMsg
}

const udpServer = dgram.createSocket('udp4')
const udpClient = dgram.createSocket('udp4')
const srcPort = 8125
const dstPort = 8135

udpServer
.on('message', (udp) => {
const msgs = udp.toString().split('\n')
msgs.forEach((msg) => {
const transMsg = trans(msg)
udpClient.send(transMsg, 8135, 'localhost')
udpClient.send(transMsg, dstPort, 'localhost')
})
})
.bind(8125)
.bind(srcPort)
console.log(`Relaying UDP ${srcPort} to ${dstPort}...`)
Loading

0 comments on commit 0df5940

Please sign in to comment.