Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic version with example #1

Merged
merged 11 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Cargo.lock
example/target/
*.so
*.a
src/gen.rs
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "telegraf-plugin-go-glue"]
path = telegraf-plugin-go-glue
url = https://github.com/StarryInternet/telegraf-plugin-go-glue
20 changes: 20 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# line length
error_on_line_overflow = true
max_width = 100

# comments
comment_width = 100
wrap_comments = false # rustfmt is broken currently and doesn't softwrap
normalize_comments = true

# commas
trailing_comma = "Never"
match_block_trailing_comma = true

# code
use_try_shorthand = true
binop_separator = "Back"
format_strings = false # rustfmt string handling is horribly busted

# imports
reorder_imports = true
20 changes: 20 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "telegraf-plugin"
version = "0.1.0"
authors = ["Timidger <pcarpenter@starry.com>"]
license = "MIT"
edition = "2018"
build = "build.rs"

[lib]
proc-macro = true

[build-dependencies]
bindgen = "0.49.*"

[dependencies]
chrono = "0.4"
libc = "0.2"
syn = { version = "0.15", features = ["full", "fold"] }
quote = "0.6"
proc-macro2 = "0.4"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 Starry

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 39 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2019. Starry, Inc. All Rights Reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// Software written by Preston Carpenter <pcarpenter@starry.com>
fn main() {
let builder = bindgen::builder()
.derive_debug(true)
.derive_default(true)
.derive_copy(true)
// NOTE This is due to implementing `Drop` in the macro.
.no_copy("go_value")
.generate_comments(true)
.blacklist_function("sample_config")
.blacklist_function("description")
.blacklist_function("gather")
.header("telegraf-plugin-go-glue/c_api.h")
.ctypes_prefix("libc");
let generated = builder.generate().unwrap();
generated.write_to_file("src/gen.rs").unwrap();
}
12 changes: 12 additions & 0 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "telegraf-plugin-example"
version = "0.1.0"
authors = ["Timidger <pcarpenter@starry.com>"]
edition = "2018"

[lib]
crate_type = ["staticlib"]

[dependencies]
libc = "0.2"
telegraf-plugin = { path = "../" }
44 changes: 44 additions & 0 deletions example/makefile-template.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[tasks.rust_build]
command = "cargo"
args = ["build"]

[tasks.copy_go_files]
script = [
'''
cp target/debug/libtelegraf_plugin_example.a ./plugin.a
cp telegraf-plugin-go-glue/lib.go ./
cp telegraf-plugin-go-glue/c_api.h ./
if [ -f plugin.so ]; then
rm plugin.so
fi
'''
]

[tasks.go_build]
command = "go"
args = ["build", "-buildmode=plugin", "-o=plugin.so"]
dependencies = ["rust_build", "copy_go_files"]

[tasks.cleanup]
script = [
'''
rm plugin.a
rm lib.go
rm c_api.h
if [ -f plugin.so ]; then
chmod a+x plugin.so
fi
'''
]

# This must run after the Rust build,
# so that it can link the Rust artifact to it.
[tasks.build]
dependencies = [
"rust_build",
"go_build",
"cleanup"
]

[config]
on_error_task = "cleanup"
54 changes: 54 additions & 0 deletions example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2019. Starry, Inc. All Rights Reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// Software written by Preston Carpenter <pcarpenter@starry.com>
use telegraf_plugin::link_to_go;

macro_rules! map(
{ $($key:expr => $value:expr),* } => {
{
#[allow(unused_mut)]
let mut m = ::std::collections::HashMap::new();
$(
m.insert($key, $value);
)*
m
}
};
);

#[link_to_go("Description of the plugin", "<sample_config>")]
fn collect_metric() {
let fields = map! {"rust_key".into() => "rust_value".into()};
let tags = map! {
"int".into() => 100i8.into(),
"uint".into() => 255u8.into(),
"double".into() => 3.1459.into(),
"bool".into() => (!false).into(),
"string".into() => "hello world".into()
};
record_field("rust-field", &fields, &tags, None);
record_gauge("rust-gauge", &fields, &tags, None);
record_counter("rust-counter", &fields, &tags, None);
record_summary("rust-summary", &fields, &tags, None);
record_histogram("rust-histogram", &fields, &tags, None);
}
1 change: 1 addition & 0 deletions example/telegraf-plugin-go-glue
Loading