-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
add Event#from_json method #4631
add Event#from_json method #4631
Conversation
I will push a json_lines codec PR for this shortly to show the usage in a backward compatible way. |
the build fail is because of the ruby-maven problem I believe, fixed in #4630. I will merge and rebase here to see. |
@colinsurprenant isn't this a bit strange? Shouldn't we be just rewriting JSON Lines to be a java plugin? |
Just to clarify, we've basically moved one codec into the Event class, isn't this not a pattern we want to encourage? |
@andrewvc well, ultimately yes, having the codec in Java will be awesome, until then, this is the best option I believe. In any case, I think that if we have a |
2ac5ac2
to
8b4ab3b
Compare
rebased against master, let's see if it solves the ruby-maven bobo |
@andrewvc also, this does not moves the codec into the event class, it moves the equivalent of |
now the error is
which seems unrelated to these commits 😕 |
Ok, I am able to reproduce the build failure when on Java 7 and running
IMO it is not related to this PR. Will create a separate issue. The complete error is
|
@colinsurprenant makes sense for now! So long as we don't add any more from/to methods I'm 👍 |
ok, cool. who's next? 😬 |
try { | ||
return RubyString.newString(context.runtime, event.toJson()); | ||
} catch (Exception e) { | ||
throw new RaiseException(context.runtime, context.runtime.getModule("LogStash").defineOrGetModuleUnder("Json").getClass("GeneratorError"), e.getMessage(), true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this usage of defineOrGetModuleUnder
is slightly confusing.
If you end up defining it, then wouldn't the proceeding getClass
return null? since nothing is using it after that fact, I suppose it is fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is and AFAIK this is the only way to get a reference of an inner module. maybe this could be established earlier on in the Library load so that if this namespace does not exist it will crash on startup and not at exception time. thanks, good observation.
in general, what is our stance on testing for our java-core + ruby integration? It looks like you chose to not introduce java tests in favor of doing ruby testing, should we stick to doing both? or is that only important once more of core becomes java. or did I miss the java tests somewhere? |
@tal good point. in this case I did not include specific java tests and probably I should. there is a potential redundancy issue but I think it's fair that some tests overlaps but both are required since they both define different interfaces. |
I don't view it as redundant. Since we going to be using direct java more and more, I view it as future-proofing :) |
@talevy fair enough, I agree. |
@talevy I think I covered what we discussed, initializing the error classes at load time and added Java Event#fromJson tests. |
heh, tests pass now on Java 7 http://build-eu-00.elastic.co/job/Logstash_PR/2170/ |
Hi |
I'm fine with that, we can assign all my tickets to the real @tal. |
lol - sorry to have pulled you in @tal and thanks for stopping by and say Hi 😬 |
the related PR for the json_lines codec is at logstash-plugins/logstash-codec-json_lines#19 |
@colinsurprenant - are you doing the json codec too? |
@colinsurprenant - I am presuming that the Java tests are only done in your IDE or is there some rspec magic that runs the Java tests too? |
@guyboertje |
@guyboertje good point, I actually forgot about the plain json codec and there is a subtlety in there... it has 2 behaviours depending on the input json structure type:
I am not sure on the best strategy for this latter case. I could easily change the contract of Hm. I believe changing the contract to
or
would be acceptable? the added benefit would be to also support the array case in json_lines? Thoughts? |
The reason for my question about the Java tests is: "Can the reviewer rely on the Jenkins test green tick in this PR to know that ALL the tests have been run and pass?". I guess at the moment the reviewer must assume that if you are committing the jar file - the Java tests must have passed. Is this the best we can do or can we make Travis do the |
@colinsurprenant - I saw the json codec with its two operations, that is way I asked. 😄 I vote we change the |
|
@colinsurprenant - you give me more credit than I'm due. I did not leap as far as thinking that what happens in the json codec will affect the Java Event fromJson contract design. I only meant that there is an optimisation opportunity therein. But I was also thinking, what happens if the JSON is a well formed array of scalars - but not suited for LS Event generation? |
@guyboertje in this case I think the best/only thing to do is to honour the legacy behaviour which will be to throw an exception but the new implementation will throw an exception sooner by making sure the json array only contains maps, as for the legacy implementation would crash later when trying to initialize a new Event with a scalar parameter value. I just finished the refactor to handle the json array of map case and I am a bit unhappy because it causes non negligible overhead in creating collections and converting these to Ruby and then the codec has to iterate the result. I am trying to see how this could be optimized... |
@guyboertje I think I'll play the dynamic game and have |
I finally decided on a middle ground, Note that this last commit introduces a bit more changes because I decided to upgrade Jackson which was really old and it is using a new namespace in v2 |
👍 on returning an array. |
@talevy ^^ added test for partial invalid json array |
sweet. any other todos? things look good to me |
no, all good on my side. wanna merge. 🙏 |
LGTM |
Object o = mapper.readValue(json, Object.class); | ||
// we currently only support Map or Array json objects | ||
if (o instanceof Map) { | ||
result = new Event[]{ new Event((Map)o) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice 👍
LGTM |
great! squashing and merging. |
from_json POC add Event#from_json with corresponding specs pre-inititalize error class constants tests for Event#from_json support array of events in from_json, upgrade to latest jackson add test for partially invalid json array
777306b
to
ad6cb41
Compare
merged into master and 2.x |
will fix #4595
This introduces
Event#from_json
which takes as argument a json string and returns a newEvent
object. The idea here is to update the json_lines codec to use this method to avoid double type conversion by first parsing the json to a Ruby structure then passing that structure to the Java Event and go through unnecessary type conversions. the POC has shown a ~50% performance increase using this.Note that this PR adds specs for the input corner cases behaviour of the current deserialization behaviour to make sure
from_json
behaves the same way.