forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support logging in starlark (influxdata#8408)
- Loading branch information
Showing
5 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package starlark | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/influxdata/telegraf" | ||
"go.starlark.net/starlark" | ||
"go.starlark.net/starlarkstruct" | ||
) | ||
|
||
// Builds a module that defines all the supported logging functions which will log using the provided logger | ||
func LogModule(logger telegraf.Logger) *starlarkstruct.Module { | ||
var logFunc = func(t *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
return log(t, b, args, kwargs, logger) | ||
} | ||
return &starlarkstruct.Module{ | ||
Name: "log", | ||
Members: starlark.StringDict{ | ||
"debug": starlark.NewBuiltin("log.debug", logFunc), | ||
"info": starlark.NewBuiltin("log.info", logFunc), | ||
"warn": starlark.NewBuiltin("log.warn", logFunc), | ||
"error": starlark.NewBuiltin("log.error", logFunc), | ||
}, | ||
} | ||
} | ||
|
||
// Logs the provided message according to the level chosen | ||
func log(t *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple, logger telegraf.Logger) (starlark.Value, error) { | ||
var msg starlark.String | ||
if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 1, &msg); err != nil { | ||
return starlark.None, fmt.Errorf("%s: %v", b.Name(), err) | ||
} | ||
switch b.Name() { | ||
case "log.debug": | ||
logger.Debug(string(msg)) | ||
case "log.info": | ||
logger.Info(string(msg)) | ||
case "log.warn": | ||
logger.Warn(string(msg)) | ||
case "log.error": | ||
logger.Error(string(msg)) | ||
default: | ||
return nil, errors.New("method " + b.Name() + " is unknown") | ||
} | ||
return starlark.None, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Example of the way to log a message with all the supported levels | ||
# using the logger of Telegraf. | ||
# | ||
# Example Input: | ||
# log debug="a debug message" 1465839830100400201 | ||
# | ||
# Example Output: | ||
# log debug="a debug message" 1465839830100400201 | ||
|
||
load("logging.star", "log") | ||
# loads log.debug(), log.info(), log.warn(), log.error() | ||
|
||
def apply(metric): | ||
log.debug("debug: {}".format(metric.fields["debug"])) | ||
log.info("an info message") | ||
log.warn("a warning message") | ||
log.error("an error message") | ||
return metric | ||
|