I currently am writing my custom json serializer in combination with my custom file output plugin. (For usecase explanation see bottom)
Now i want to use the config variables from the output plugin inside my json serializer.
E.g. here are some of the config variables:
Files []string `toml:"files"`
RotationInterval internal.Duration `toml:"rotation_interval"`
RotationMaxSize internal.Size `toml:"rotation_max_size"`
RotationMaxArchives int `toml:"rotation_max_archives"`
myCustomVariable int `toml:"my_custom_variable"`
...
Now i’d like to use myCustomVariable inside the json.go Serializer.
What would be a good way to achieve this?
Example Solution:
Would i create a Serializer attribute with a new set-function in my json serializer like this?
func (s *Serializer) setMyVar(int myVar) () {
s.myVar = myVar
}
type Serializer struct {
TimestampUnits time.Duration
int myVar
}
And call this in the output plugin in the SetSerializer-function?
func (f *File) SetSerializer(serializer serializers.Serializer) {
f.serializer = serializer
f.serializer.setMyVar(f.myCustomVariable)
}
I thought what i outlined above would be a good way, but then i am messing with the serializer signature: functions/attributes and this might entail doing some further configurations in other files to register these changes (and i do not know where i need to do this)
Disclaimer: i was able to create my own json-serializer and my own file-output plugin, but ONLY when they use the same function/attribute signatures.
Is there some way better how i can access these config-variables from inside the serializer?
Is is even okay that i do this? If not i’d be very happy to get some suggestions how to do it the right/intended way.
Usecase explanation: (indirectly relevant for question)
My usecase is, i want that the metrics i receive to be NOT appended to a output file, but
that the metrics will replace old metrics. Since i only care about the last metrics for each.
E.g. my output file metrics.json contains the following:
...
"metric1": 18.9,
"metric2": 20.9,
"metric3": 1.0,
...
if i now receive a new value: “metric1”: 8.5
i want that the my metrics.json afterwards looks like this (replaced):
...
"metric1": 8.5,
"metric2": 20.9,
"metric3": 1.0,
...
and NOT like this (appended, as it is the case with the default json serializer and file output):
...
"metric1": 18.9,
"metric2": 20.9,
"metric3": 1.0,
"metric1": 8.5,
...
Thanks for reading any suggestions appreciated.