Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from Overmuse/SR/initial_development
Browse files Browse the repository at this point in the history
initial development
  • Loading branch information
SebRollen authored Jun 17, 2021
2 parents ab5cd6f + 630f997 commit c3b77e7
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 0 deletions.
128 changes: 128 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
on:
push:
branches:
- main
paths:
- 'Cargo.toml'
- 'Cargo.lock'
- 'Dockerfile'
- 'src/**'
pull_request:
paths:
- 'Cargo.toml'
- 'Cargo.lock'
- 'Dockerfile'
- 'src/**'

name: Continuous integration

jobs:
cancel-previous:
name: Cancel Previous Runs
runs-on: ubuntu-latest
steps:
- name: Cancel actions
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Cache
uses: actions/cache@v2
id: cache
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Setup git credentials
uses: fusion-engineering/setup-git-credentials@v2
with:
credentials: ${{secrets.GIT_USER_CREDENTIALS}}
- name: Setup toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Install rustfmt
run: rustup component add rustfmt
- name: Run check
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
needs: [fmt]
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Cache
uses: actions/cache@v2
id: cache
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Setup git credentials
uses: fusion-engineering/setup-git-credentials@v2
with:
credentials: ${{secrets.GIT_USER_CREDENTIALS}}
- name: Setup toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Install clippy
run: rustup component add clippy
- name: Run clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings

coverage:
name: Test & Coverage
runs-on: ubuntu-latest
needs: [fmt, clippy]
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Cache
uses: actions/cache@v2
id: cache
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Setup git credentials
uses: fusion-engineering/setup-git-credentials@v2
with:
credentials: ${{secrets.GIT_USER_CREDENTIALS}}
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Install tarpaulin
run: cargo install cargo-tarpaulin
- name: Generate coverage
run: cargo tarpaulin --out Xml
- name: Upload to codecov
uses: codecov/codecov-action@v1
with:
token: ${{secrets.CODECOV_TOKEN}}
fail_ci_if_error: true
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release
on:
push:
branches:
- main

jobs:
deploy:
name: Tag if new release
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Read version number
id: read_toml
uses: outcome-co/[email protected]
with:
path: Cargo.toml
key: package.version
- name: Set tag env variable
run: echo IMAGE_TAG=v${{steps.read_toml.outputs.package_version}} >> $GITHUB_ENV
- uses: ncipollo/release-action@v1
continue-on-error: true
with:
allowUpdates: false
tag: ${{ env.IMAGE_TAG }}
token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "dogstatsd"
version = "0.1.0"
authors = ["Sebastian Rollen <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bytes = "1.0.1"
tokio = { version = "1.7.0", default-features = false, features = ["net"] }

[dev-dependencies]
tokio = { version = "1.7.0", default-features = false, features = ["macros", "rt"] }
50 changes: 50 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::metric::Metric;
use std::io::Error;
use tokio::net::{ToSocketAddrs, UdpSocket};

pub struct Client {
socket: UdpSocket,
}

impl Client {
pub async fn new<T: ToSocketAddrs>(
host_address: T,
target_address: &str,
) -> Result<Client, Error> {
let socket = UdpSocket::bind(host_address).await?;
socket.connect(target_address).await?;
Ok(Self { socket })
}

pub async fn send<I, T>(&self, metric: Metric<'_, I, T>) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: AsRef<str>,
{
let bytes = metric.into_bytes();
self.socket.send(&bytes).await?;
Ok(())
}
}

#[cfg(test)]
mod test {
use super::*;

#[tokio::test]
async fn test_client() {
let udp_receiver = UdpSocket::bind("127.0.0.1:8125").await.unwrap();
let v: &[&str] = &[];
let client = Client::new("127.0.0.1:1234", "127.0.0.1:8125")
.await
.unwrap();
client.send(Metric::increase("test", v)).await.unwrap();
udp_receiver.connect("127.0.0.1:1234").await.unwrap();
let mut bytes_received: usize = 0;
let mut buf = [0; 8];
while bytes_received < 8 {
bytes_received += udp_receiver.recv_from(&mut buf).await.unwrap().0;
}
assert_eq!(&buf, b"test:1|c");
}
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod client;
mod metric;
pub use client::Client;
pub use metric::Metric;
Loading

0 comments on commit c3b77e7

Please sign in to comment.