Skip to content

JSON attributes do not show up as valid json in event attributes #2103

Description

@MagneticNeedle

Describe the bug
A clear and concise description of what the bug is.
When a valid json is passed as value of a custom event atrribute, In logs it is show as a string and not a json object

To Reproduce

# in the handler

    def emit(self, record):
        env = os.getenv("APP_ENV", "local")
        log_ctx: LogCtxDict = record.__dict__.get("log_ctx", {})
        tags_dict = log_ctx.pop('tags', {})
        tags = [f"{k}:{v}" for k, v in tags_dict.items()]
        tags.append("env:{}".format(env))
        tags_merged = ",".join(tags)
        body = HTTPLog(
            [
                HTTPLogItem(
                    ddsource="python",
                    ddtags=tags_merged,
                    hostname="vv-anyscale-ray-aps1",
                    message=self.format(record),
                    service="vv-ap-thumbnails-v1-{}-service".format(env),
                    timestamp=str(datetime.fromtimestamp(record.created).astimezone().timestamp()),
                    level=record.levelname.lower(),
                    **record.__dict__.get("log_ctx", {})
                )
            ]
        )
        try:
            with ApiClient(self.dd_configuration) as api_client:
                resp = LogsApi(api_client).submit_log(content_encoding=ContentEncoding.GZIP, body=body)
                if "errors" in resp:
                    self.handleError(record)

        except (Exception, ):
            self.handleError(record)




logger.debug(
    "Test debug log",
    log_ctx={
        "x_request_body": json.dumps({"clip_id": "66abc57369bbe0000782ef42"}),
    }
)

Expected behavior
A clear and concise description of what you expected to happen.
The object should be consumed and shown as json,

Screenshots
If applicable, add screenshots to help explain your problem.
image

Environment and Versions (please complete the following information):
A clear and precise description of your setup:

$ poetry show datadog-api-client
 name         : datadog-api-client                         
 version      : 2.27.0                                     
 description  : Collection of all Datadog Public endpoints 

dependencies
 - certifi *
 - python-dateutil *
 - typing-extensions >=4.0.0
 - urllib3 >=1.15

for logs parsing default python pipleline is used
image

Activity

  1. MagneticNeedle commented on Aug 22, 2024

    @MagneticNeedle
    Author

    ok its because in HTTPLogItem the additional_properties_type is set to return to only str

    https://github.com/DataDog/datadog-api-client-python/blob/master/src/datadog_api_client/v2/model/http_log_item.py#L19

    This is my workaround for now

    class HTTPLogItemWithDict(HTTPLogItem):
        @cached_property
        def additional_properties_type(self):
            return str, dict
    
    
    class HTTPLogWithDict(HTTPLog):
        @cached_property
        def openapi_types(self):
            return {
                "value": ([HTTPLogItemWithDict],),
            }

    then in the API client code

                with ApiClient(self.dd_configuration) as api_client:
                    log_api = LogsApi(api_client)
                    log_api._submit_log_endpoint.params_map["body"]["openapi_types"] = (
                        HTTPLogWithDict,
                    )
                    resp = log_api.submit_log(body=body)
                    if "errors" in resp:
                        self.handleError(record)

    is this an ok workaround ? and why is HTTPLogItem limited to only str ? when the api supports json in body

    image

  2. github-actions commented on Dec 11, 2024

    @github-actions

    Thanks for your contribution!

    This issue has been automatically marked as stale because it has not had activity in the last 30 days. Note that the issue will not be automatically closed, but this notification will remind us to investigate why there's been inactivity. Thank you for participating in the Datadog open source community.

    If you would like this issue to remain open:

    1. Verify that you can still reproduce the issue in the latest version of this project.

    2. Comment that the issue is still reproducible and include updated details requested in the issue template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions