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

SNS MessageAttribute are mixed up #711

Closed
matthias-Q opened this issue Nov 10, 2023 · 8 comments
Closed

SNS MessageAttribute are mixed up #711

matthias-Q opened this issue Nov 10, 2023 · 8 comments
Labels
bug 🐞 Something isn't working

Comments

@matthias-Q
Copy link

Hi,
I am trying to publish a SNS message with four MessageAttributes. The key/value pairs in the list that I provide to sns$publish differs from the one received from the topic.

Here is a minimal example:

send_message <- function(topic_arn, dest, s3paths, bucket, prefix, message_type) {

  msg_attributes = list(
    files=list(DataType="String.Array", StringValue=paste0(s3paths, collapse = ',')),
    parquet_schema=list(DataType="String", StringValue=paste0("s3://", bucket, "/schemas/", prefix, ".json")),
    message_type=list(DataType="String", StringValue=message_type),
    platform=list(DataType="String", StringValue=dest)
  )

  sns <- paws::sns()
  sns$publish(
    Message = "my message",
    TopicArn = topic_arn,
    MessageStructure="string",
    MessageAttributes=msg_attributes
  )
  return(msg_attributes)
}


bucket = "mybucket"
s3paths = c(
    "a", "b", "c"
)

topic_arn = "arn:aws:sns:eu-central-1:12345678:my_topic"

prefix = "my_prefix"
message_type = "my_message_type"


send_message(
    topic_arn,
    "platform_A",
    s3paths,
    bucket,
    prefix,
    message_type
)

This is the message that I receive:

{
  "Type" : "Notification",
  "MessageId" : "...",
  "TopicArn" : "...",
  "Message" : "my message",
  "Timestamp" : "2023-11-10T12:38:19.337Z",
  "SignatureVersion" : "1",
  "Signature" : "...",
  "SigningCertURL" : "...",
  "UnsubscribeURL" : "...",
  "MessageAttributes" : {
    "files" : {"Type":"String.Array","Value":"/path/a,/path/b,/path/c"},
    "parquet_schema" : {"Type":"String","Value":"my_message_type"},
    "message_type" : {"Type":"String","Value":"s3://mybucket/schemas/my_prefix.json"},
    "platform" : {"Type":"String","Value":"south"}
  }
}

As you can see parquet_schema and message_type have swapped their values.

@DyfanJones
Copy link
Member

Ah sorry about that, I will look into it shortly.

@DyfanJones DyfanJones added the bug 🐞 Something isn't working label Nov 10, 2023
@DyfanJones
Copy link
Member

@matthias-Q what version of paws and paws.common are you using please? :)

@matthias-Q
Copy link
Author

packageVersion("paws")
[1] ‘0.4.0’
packageVersion("paws.common")
[1] ‘0.6.3’

@DyfanJones
Copy link
Member

@matthias-Q I think I have found a bug in query_parse it seems to want to order stuff. This is causing the bug. I will see how easy of a fix it is :D

@DyfanJones
Copy link
Member

DyfanJones commented Nov 10, 2023

OK I have a fix :)

remotes::install_github("dyfanjones/paws/paws.common", ref = "fix_query_mapping")
send_message <- function(topic_arn, dest, s3paths, bucket, prefix, message_type) {
  
  msg_attributes = list(
    files=list(DataType="String.Array", StringValue=paste0(s3paths, collapse = ',')),
    parquet_schema=list(DataType="String", StringValue=paste0("s3://", bucket, "/schemas/", prefix, ".json")),
    message_type=list(DataType="String", StringValue=message_type),
    platform=list(DataType="String", StringValue=dest)
  )
  
  sns <- paws::sns()
  sns$publish(
    Message = "my message",
    TopicArn = topic_arn,
    MessageStructure="string",
    MessageAttributes=msg_attributes
  )
  return(msg_attributes)
}


bucket = "mybucket"
s3paths = c(
  "a", "b", "c"
)

topic_arn = "arn:aws:sns:eu-central-1:12345678:my_topic"

prefix = "my_prefix"
message_type = "my_message_type"

# options(paws.log_level = 3)
resp <- send_message(
  topic_arn,
  "platform_A",
  s3paths,
  bucket,
  prefix,
  message_type
)

resp |> jsonlite::toJSON(pretty = T, auto_unbox = T)
#> {
#>   "files": {
#>     "DataType": "String.Array",
#>     "StringValue": "a,b,c"
#>   },
#>   "parquet_schema": {
#>     "DataType": "String",
#>     "StringValue": "s3://mybucket/schemas/my_prefix.json"
#>   },
#>   "message_type": {
#>     "DataType": "String",
#>     "StringValue": "my_message_type"
#>   },
#>   "platform": {
#>     "DataType": "String",
#>     "StringValue": "platform_A"
#>   }
#> }

client <- paws::s3()
message <- client$get_object(
  Bucket = bucket, Key = sprintf("schemas/%s.json", prefix)
)$Body |> rawToChar() |> jsonlite::fromJSON(simplifyDataFrame = F)

message$Records[[1]]$Sns$MessageAttributes$parquet_schema$Value
#> [1] "s3://mybucket/schemas/my_prefix.json"

Created on 2023-11-10 with reprex v2.0.2

Note: for test purposes my SNS just outputted the message the AWS S3 using a basic AWS Lambda.

Sadly this has just missed the paws.common 0.6.4 cran release. @matthias-Q are you ok to use r-universe/ dev version of now until paws.common 0.6.5 is released?

@matthias-Q
Copy link
Author

matthias-Q commented Nov 10, 2023

Wow, thanks for the quick fix. How often are these cran releases? We could easily wait for a week. I will test it right away on my machine, but on the production system we would like to have a release version.

[Update]: tested the dev version and I confirm that it now works as expected. Thanks!

@DyfanJones
Copy link
Member

Usually I don't want to pester the cran guys with weekly releases.

I have requested paws.common-0.6.4 cran to be cancelled so that I can get this fix into the latest paws.common release (over the weekend hopefully). I will give an update as soon as I hear back from the cran

@DyfanJones
Copy link
Member

Closing this ticket as paws.common 0.6.4 has been released to cran.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants