Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
David Marcovitch authored and David Marcovitch committed Jun 10, 2024
1 parent 9cb8163 commit 393861e
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 64 deletions.
27 changes: 12 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Package implements configuration for plugins
// Package implements loading of fluent-bit configuration file. Allows plugin to access values
// defined in configuration file.

package config

Expand All @@ -11,28 +12,25 @@ import (
"github.com/fluent/fluent-bit-go/output"
)

// Holds settings for s3 clp plugin
// from user defined fluent-bit configuration file
// Holds settings for s3 clp plugin from user defined fluent-bit configuration file.
type S3Config struct {
Id string
Path string
File string
}

// Generates configuration struct
// Generates configuration struct containing user-defined settings.
//
// Parameters:
// - plugin: fluent-bit plugin reference
//
// Returns:
// - S3Config: configuration based on fluent-bit.conf
// - err: error wrapping all errors in config
// - S3Config: Configuration based on fluent-bit.conf
// - err: All errors in config wrapped
func S3New(plugin unsafe.Pointer) (*S3Config, error) {

// slice to hold config errors
// allows config function to return all errors at once
// instead of one at a time
// so user can fix all at once
// Slice holds config errors allowing function to return all errors at once instead of
// one at a time. User can fix all errors at once.
configErrors := []error{}

id, errID := getValueFLBConfig(plugin, "Id")
Expand All @@ -50,21 +48,20 @@ func S3New(plugin unsafe.Pointer) (*S3Config, error) {
File: file,
}

// wrap all errors into one error before returning
// automically excludes nil errors
// Wrap all errors into one error before returning. Automically excludes nil errors.
err := errors.Join(configErrors...)
return config, err
}

// Retrieves individuals values from fluent-bit.conf
// Retrieves individuals values from fluent-bit.conf.
//
// Parameters:
// - plugin: fluent-bit plugin reference
// - configKey: key from fluent-bit.conf
// - configKey: Key from fluent-bit.conf
//
// Returns:
// - configValue
// - err: blank value
// - err: Error if config value is blank
func getValueFLBConfig(plugin unsafe.Pointer, configKey string) (string, error) {
configValue := output.FLBPluginConfigKey(plugin, configKey)

Expand Down
2 changes: 1 addition & 1 deletion internal/constant/constant.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package contains constants
// Package contains constants.
package constant

const S3PluginName = "out_clp_s3"
10 changes: 5 additions & 5 deletions plugins/out_clp_s3/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Builds plugin binary in go container and then runs in fluent-bit container
# Builds plugin binary in go container and then runs in fluent-bit container.

# using bullseye tag to match debian version from fluent-bit image [fluent-bit Debian version]
# same debian version prevents glibc compatibility issues
# Using bullseye tag to match debian version from fluent-bit image [fluent-bit Debian version].
# Matching debian versions prevents glibc compatibility issues.
# [fluent-bit Debian version]: https://github.com/fluent/fluent-bit/blob/master/dockerfiles/Dockerfile
FROM golang:1.22.3-bullseye as builder

Expand All @@ -23,11 +23,11 @@ RUN task build

FROM fluent/fluent-bit:3.0.6

#copy plugin binary to fluent-bit image
# Copy plugin binary to fluent-bit image.
COPY --from=builder /root/plugins/out_clp_s3/out_clp_s3.so /fluent-bit/bin/
COPY --from=builder /root/plugins/out_clp_s3/*.conf /fluent-bit/etc/

#port for listening interface for HTTP Server
# Port for listening interface for HTTP Server.
EXPOSE 2020

CMD ["/fluent-bit/bin/fluent-bit", "--config", "/fluent-bit/etc/fluent-bit.conf"]
10 changes: 2 additions & 8 deletions plugins/out_clp_s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,5 @@ Change [fluent-bit.conf](fluent-bit.conf) to specify
match *
```

[1]: https://go.dev/doc/install
[2]: https://docs.fluentbit.io/manual/installation/getting-started-with-fluent-bit






[1]: https://go.dev/doc/install
[2]: https://docs.fluentbit.io/manual/installation/getting-started-with-fluent-bit
4 changes: 2 additions & 2 deletions plugins/out_clp_s3/fluent-bit.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#sample fluent-bit configuration with output set to clp s3 plugin
#Sample fluent-bit configuration with output set to clp s3 plugin.

[SERVICE]
# Flush
Expand Down Expand Up @@ -32,7 +32,7 @@

# HTTP Server
# ===========
# Enable/Disable the built-in HTTP Server for metrics
# Enable/Disable the built-in HTTP Server for metrics.
http_server Off
http_listen 0.0.0.0
http_port 2020
Expand Down
35 changes: 18 additions & 17 deletions plugins/out_clp_s3/flush/flush.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Package implements method to send data to output
// Package implements methods to send data to output. All data provided by fluent-bit is encoded
// with msgpack.

package flush

import (
Expand All @@ -15,39 +17,38 @@ import (
"github.com/y-scope/fluent-bit-clp/config"
)

// flushes data to file
// Flushes data to file.
//
// Parameters:
// - data: msgpack data
// - length: byte length
// - length: Byte length
// - tag: fluent-bit tag
// - S3Config: configuration based on fluent-bit.conf
// - S3Config: Configuration based on fluent-bit.conf
//
// Returns:
// - err: error flushing data
// - err: Error if flush fails
func File(data unsafe.Pointer, length int, tag string, config *config.S3Config) error {
fullFilePath := filepath.Join(config.Path, config.File)

// If the file doesn't exist, create it, or append to the file
// will still cause error if there is no directory
// If the file doesn't exist, create it, or append to the file. Will still cause error if there
// is no directory
f, err := os.OpenFile(fullFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
return err
}

defer f.Close()

// ================== This code is mostly boilerplate [fluent-bit reference] ================== //
// temporary changes were made so that writes to file instead of stdout
// code will be deleted when switch to IR / send to S3
/* ================== This code is mostly boilerplate [fluent-bit reference] ================== */
// Temporary changes were made to boilerplate so that writes to file instead of stdout.
// TODO: Update code so converts to IR and sends to s3.
// [fluent-bit reference]: https://github.com/fluent/fluent-bit-go/blob/a7a013e2473cdf62d7320822658d5816b3063758/examples/out_multiinstance/out.go#L41
// nolint:revive
dec := output.NewDecoder(data, length)

// temporary change added buffered writer for performance
// simplifies error handling since we can only need to check for error when flushing
// simplifies retry since nothing before error is written to file (written to buffer instead)
// setting size to that provided by fluent-bit to prevent overflow errors
// Buffered writer improves performance and simplifies error handling. Checking for error when
// flushing simplifies retry since nothing before error is written to file (written to buffer
// instead). Buffer size set to value provided by fluent-bit to prevent overflow errors.
w := bufio.NewWriterSize(f, length)

count := 0
Expand All @@ -68,8 +69,8 @@ func File(data unsafe.Pointer, length int, tag string, config *config.S3Config)
timestamp = time.Now()
}

// temporary change so writes to file
// code will be deleted
// Temporary change to boilerplate so writes to file.
// TODO: Update code so converts to IR and sends to s3
_, _ = w.WriteString(fmt.Sprintf("[%d] %s: [%s, {", count, tag, timestamp.String()))

for k, v := range record {
Expand All @@ -81,7 +82,7 @@ func File(data unsafe.Pointer, length int, tag string, config *config.S3Config)
}
/* ================== End of boilerplate ================== */

// If an error occurs writing to a Writer, Writer.Flush will return the error
// If an error occurs writing to a Writer, Writer.Flush will return the error.
err = w.Flush()
return err
}
32 changes: 16 additions & 16 deletions plugins/out_clp_s3/out_clp_s3.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Package defines high-level callback functions required by fluent-bit go
// plugin documentation. See article/repo fo more information [fluent-bit go], [fluent-bit stdout example].
// Package defines high-level callback functions required by fluent-bit go plugin documentation. See
// article/repo fo more information [fluent-bit go], [fluent-bit stdout example].
//
// [fluent-bit go]: https://docs.fluentbit.io/manual/development/golang-output-plugins
// [fluent-bit stdout example]: https://github.com/fluent/fluent-bit-go/tree/master/examples/out_multiinstance
// nolint:revive

// note package name "main" is required by fluent-bit which suppresses go docs
// do not remove export, required for use by fluent-bit C calls
// Note package name "main" is required by fluent-bit which suppresses go docs. Do not remove
// export, required for use by fluent-bit C calls.
package main

import (
Expand All @@ -21,7 +21,7 @@ import (
"github.com/y-scope/fluent-bit-clp/plugins/out_clp_s3/flush"
)

// fluent-bit registration callback
// Required fluent-bit registration callback.
//
// Parameters:
// - def: fluent-bit plugin definition
Expand All @@ -35,40 +35,40 @@ func FLBPluginRegister(def unsafe.Pointer) int {
return output.FLBPluginRegister(def, constant.S3PluginName, "Clp s3 plugin")
}

// fluent-bit initialization callback
// Required fluent-bit initialization callback.
//
// Parameters:
// - def: fluent-bit plugin reference
//
// Returns:
// - code: fluent-bit success code (OK,RETRY,ERROR)
// - code: fluent-bit success code (OK, RETRY, ERROR)
//
//export FLBPluginInit
func FLBPluginInit(plugin unsafe.Pointer) int {
// returns pointer to a config instance based on fluent-bit configuration
// Returns pointer to a config instance based on fluent-bit configuration
config, err := config.S3New(plugin)
if err != nil {
log.Fatalf("Failed to load configuration %s", err)
}

log.Printf("[%s] Init called for id: %s", constant.S3PluginName, config.Id)

// set the context for this instance so that params can be retrieved during flush
// context should only be set once to avoid race condition
// Set the context for this instance so that params can be retrieved during flush. Context
// should only be set once to avoid race condition
output.FLBPluginSetContext(plugin, config)
return output.FLB_OK
}

// fluent-bit flush callback
// Required fluent-bit flush callback.
//
// Parameters:
// - ctx: fluent-bit plugin context
// - data: msgpack data
// - length: byte length
// - length: Byte length
// - tag: fluent-bit tag
//
// Returns:
// - code: fluent-bit success code (OK,RETRY,ERROR)
// - code: fluent-bit success code (OK, RETRY, ERROR)
//
//export FLBPluginFlushCtx
func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int {
Expand All @@ -93,18 +93,18 @@ func FLBPluginExit() int {
return output.FLB_OK
}

// fluent-bit exit callback
// Required fluent-bit exit callback.
//
// Parameters:
// - ctx: fluent-bit plugin context
//
// Returns:
// - code: fluent-bit success code (OK,RETRY,ERROR)
// - code: fluent-bit success code (OK, RETRY, ERROR)
//
//export FLBPluginExitCtx
func FLBPluginExitCtx(ctx unsafe.Pointer) int {
p := output.FLBPluginGetContext(ctx)
// Type assert context back into the original type for the Go variable
// Type assert context back into the original type for the Go variable.
config := (p).(*config.S3Config)
log.Printf("[%s] Exit called for id: %s", constant.S3PluginName, config.Id)
return output.FLB_OK
Expand Down

0 comments on commit 393861e

Please sign in to comment.