Skip to content

Conversation

@swalkinshaw
Copy link
Contributor

@swalkinshaw swalkinshaw commented Jul 12, 2023

Fix the logic for adding graphql.validation.error events on validate spans.

The errors variable would always end up being a serialized "[]" value which wasn't nil and an empty graphql.validation.error event would be added.

super.tap do |response|
errors = response[:errors]&.compact&.map(&:to_h)&.to_json
unless errors.nil?
errors = response[:errors]&.compact&.map(&:to_h) || []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might still be overly defensive but the root cause is that the graphql-ruby gem always returns an array: https://github.com/rmosolgo/graphql-ruby/blob/bde9b64d7df0cdc28045a9231d3219a800052ffc/lib/graphql/static_validation/validator.rb#L54-L56

Fix the logic for adding `graphql.validation.error` events on `validate` spans.

The `errors` variable would always end up being a serialized `"[]"`
empty array which wasn't nil and an empty `graphql.validation.error`
event would be added.
@swalkinshaw swalkinshaw force-pushed the graphql-instrumentation-fix-validation-events branch from 2b648c1 to acd1455 Compare July 12, 2023 15:40
'graphql.validation.error',
attributes: {
'message' => errors
'message' => errors.to_json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that GraphQL depends on JSON being required. Is that correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate to do this but I am feeling a little worried about JSON serialization failing for some reason. Is there anything in the errors object that may link back to objects generated by frameworks like Rails?

I have been bitten too many times by as_json called on X without :only option. You must explicitly declare the keys you wish to serialize

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something else comes to mind. Is there a community consensus around semconv for these kinds of validation errors?
Should we submit an otep for this? cc: @open-telemetry/ruby-contrib-approvers ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate to do this but I am feeling a little worried about JSON serialization failing for some reason. Is there anything in the errors object that may link back to objects generated by frameworks like Rails?

I don't think that's possible since this is only for static validation errors specifically, which are built by the graphql-ruby gem. Here's the representation: https://github.com/rmosolgo/graphql-ruby/blob/f05ea569af0ceea6eea41919b7bb61f3eb06f2cd/lib/graphql/static_validation/error.rb

Example of one: https://github.com/rmosolgo/graphql-ruby/blob/f05ea569af0ceea6eea41919b7bb61f3eb06f2cd/lib/graphql/static_validation/rules/argument_literals_are_compatible_error.rb#L21-L41

This to_json existed before as well so it's not new.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK thanks. I have created a discussion here in the hopes to get some eyes/opinions about what to do about this going forward.

open-telemetry/opentelemetry-specification#3624

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like there is a tracking issue for this now: open-telemetry/semantic-conventions#182

Given that there isn't a consensus and the semconv is evolving I am going to take another look at the PR Thank you again for your contributions!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arielvalentin arielvalentin added the help wanted Extra attention is needed label Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

help wanted Extra attention is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants