Skip to content
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

json: ser/de bytes as base64 strings not an array of bytes #2471

Merged
merged 4 commits into from
Feb 5, 2025

Conversation

adriangb
Copy link
Contributor

@adriangb adriangb commented Dec 25, 2024

I believe this is how the proto <-> json spec defines that binary data should be handled: https://protobuf.dev/programming-guides/json/

I confirmed by exporting data from Python:

from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import ExportTraceServiceRequest
from opentelemetry.proto.common.v1.common_pb2 import AnyValue, KeyValue
from opentelemetry.proto.resource.v1.resource_pb2 import Resource
from opentelemetry.proto.trace.v1.trace_pb2 import ResourceSpans
from google.protobuf.json_format import MessageToJson


export = ExportTraceServiceRequest(
    resource_spans=[
        ResourceSpans(
            resource=Resource(
                attributes=[
                    KeyValue(key="key", value=AnyValue(bytes_value=b"value"))
                ]
            )
        )
    ]
)

json = MessageToJson(export, use_integers_for_enums=True)

print(json)

with open("export.json", "w") as f:
    f.write(json)
{
  "resourceSpans": [
    {
      "resource": {
        "attributes": [
          {
            "key": "key",
            "value": {
              "bytesValue": "dmFsdWU="
            }
          }
        ]
      }
    }
  ]
}

@adriangb adriangb requested a review from a team as a code owner December 25, 2024 03:47
Copy link

codecov bot commented Dec 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.1%. Comparing base (775f1f9) to head (42807da).
Report is 1 commits behind head on main.

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #2471   +/-   ##
=====================================
  Coverage   79.1%   79.1%           
=====================================
  Files        118     118           
  Lines      22508   22508           
=====================================
  Hits       17821   17821           
  Misses      4687    4687           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@adriangb
Copy link
Contributor Author

I'm not familiar with the codebase so I took a guess as to where to apply the fix. I tried to add an integration test but I could not get a failure case to fail a test so I'm not sure if that's even in the right place either. Some pointers would be appreciated.

@adriangb
Copy link
Contributor Author

Hi @lalitb @TommyCpp would one of you mind reviewing or pinging an appropriate reviewer (sorry if this is not relevant to you, I just went off of git history)? Thanks

@lalitb lalitb added the integration tests Run integration tests label Jan 2, 2025
@adriangb
Copy link
Contributor Author

adriangb commented Jan 9, 2025

@lalitb I can't seem to get tests to agree on the order of items in attributes arrays. I'm not sure if they're getting sorted in one place but not another or something. Otherwise all tests seem to be passing. I'll point out that I had to implement bytes support for the rust sdk itself, maybe that should be it's own PR but if so it will block this PR.

@adriangb
Copy link
Contributor Author

Hi, just checking if there's anything else I can do to move this along. Thanks!

@adriangb
Copy link
Contributor Author

We've got some conflicts now. I'm happy to resolve them, or split this PR up, but I need a bit of guidance on how you'd like to proceed. Thanks!

@adriangb
Copy link
Contributor Author

adriangb commented Feb 4, 2025

Hi folks pinging again. I'm willing to do the work here and this seems like an important issue (invalid data being generated) but I need some input from the dev team, this is my first PR to the repo. cc @cijothomas as main commiter.

@adriangb adriangb force-pushed the fix-json-deser-bytes branch 2 times, most recently from 4eda296 to bdd9c23 Compare February 4, 2025 18:03
@lalitb
Copy link
Member

lalitb commented Feb 4, 2025

Hi folks pinging again. I'm willing to do the work here and this seems like an important issue (invalid data being generated) but I need some input from the dev team, this is my first PR to the repo. cc @cijothomas as main commiter.

@adriangb Sorry on delay on this. I believe the CI failure is for the ordering of resource attributes. But see my comment here if that makes sense.

@adriangb adriangb force-pushed the fix-json-deser-bytes branch from 8c7d87f to 7d6ea07 Compare February 5, 2025 01:35
@adriangb adriangb requested a review from lalitb February 5, 2025 01:36
@cijothomas
Copy link
Member

@adriangb Thanks for helping!
I am curious to know if you are impacted by this or you are pro-actively addressing this? Given the appenders maintained in this repo does not today produce value of type array of bytes, I am curious - are there other ways people are using the crates?

@adriangb
Copy link
Contributor Author

adriangb commented Feb 5, 2025

I use the crate to load telemetry exported as JSON by a browser SDK that follows the OTLP JSON spec, so yes I was hitting this in production.

The bigger picture is that I use the crate as part of the ingestion portion of OTEL backend written in Rust: https://pydantic.dev/logfire

@adriangb
Copy link
Contributor Author

adriangb commented Feb 5, 2025

And thank you both for getting this across the line!

@cijothomas cijothomas merged commit 0e751b4 into open-telemetry:main Feb 5, 2025
21 checks passed
@cijothomas
Copy link
Member

https://pydantic.dev/logfire

Thanks for sharing more context. We were earlier considering removing publishing the opentelemetry-proto crate itself, as it was considered an internal dependency for the opentelemetry-otlp itself! But later decided to keep it!

@adriangb
Copy link
Contributor Author

adriangb commented Feb 5, 2025

I'm glad you did!

@lalitb
Copy link
Member

lalitb commented Feb 5, 2025

Yes, interestingly, this crate is being utilized more than we initially expected. Another example: #2487.

@cijothomas
Copy link
Member

Created an issue to track getting it to 1.0
#2615

@adriangb If you have bandwidth, please do check and see any gaps before we can make it a stable crate. (ofcource, the opentelemetry and opentelemetry_sdk crates must be 1.0 as a pre-req, and that is scheduled by mid year.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration tests Run integration tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants