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

NOISSUE - Set subtopic for messages published outside adapters #1867

Closed
wants to merge 6 commits into from

Conversation

SammyOina
Copy link
Contributor

@SammyOina SammyOina commented Jul 21, 2023

Signed-off-by: SammyOina sammyoina@gmail.com

What does this do?

Sets message subtopic to subject/routing key when not set as in the case for messages published using external clients as in heartbeat service of mainflux/agent#56.

To test on nats:

git clone https://github.com/nats-io/nats.go
cd github.com/nats-io/nats.go/examples/nats-pub
go run main.go -s http://localhost:4222 heartbeat.<service-name>.<service-type> ""; 

Using rabbitmq:

package main

import (
	"context"
	"log"
	"time"

	amqp "github.com/rabbitmq/amqp091-go"
)

func failOnError(err error, msg string) {
	if err != nil {
		log.Panicf("%s: %s", msg, err)
	}
}

func main() {
	conn, err := amqp.Dial("amqp://mainflux:mainflux@localhost:5672/")
	failOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()
	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	body := ""
	err = ch.PublishWithContext(ctx,
		"mainflux-exchange",  // exchange
		"heartbeat.svc.name", // routing key
		false,                // mandatory
		false,                // immediate
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        []byte(body),
		})
	failOnError(err, "Failed to publish a message")
	log.Printf(" [x] Sent %s\n", body)
}

Which issue(s) does this PR fix/relate to?

None

List any changes that modify/break current functionality

Have you included tests for your changes?

No

Did you document any new/modified functionality?

Notes

@SammyOina SammyOina marked this pull request as ready for review July 21, 2023 12:00
@SammyOina SammyOina requested a review from a team as a code owner July 21, 2023 12:00
@codecov
Copy link

codecov bot commented Jul 21, 2023

Codecov Report

Merging #1867 (13b8ebb) into master (106b710) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 13b8ebb differs from pull request most recent head 053edd6. Consider uploading reports for the commit 053edd6 to get more accurate results

@@           Coverage Diff           @@
##           master    #1867   +/-   ##
=======================================
  Coverage   64.73%   64.74%           
=======================================
  Files         118      118           
  Lines        9702     9704    +2     
=======================================
+ Hits         6281     6283    +2     
  Misses       2753     2753           
  Partials      668      668           
Files Changed Coverage Δ
things/policies/auth.pb.go 11.36% <ø> (ø)
things/policies/auth_grpc.pb.go 0.00% <ø> (ø)
users/policies/auth.pb.go 4.47% <ø> (ø)
users/policies/auth_grpc.pb.go 0.00% <ø> (ø)
pkg/messaging/nats/pubsub.go 72.72% <100.00%> (+0.35%) ⬆️
pkg/messaging/rabbitmq/publisher.go 68.18% <100.00%> (ø)
pkg/messaging/rabbitmq/pubsub.go 69.04% <100.00%> (+0.37%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Signed-off-by: SammyOina <sammyoina@gmail.com>
Signed-off-by: SammyOina <sammyoina@gmail.com>
Signed-off-by: SammyOina <sammyoina@gmail.com>
@drasko
Copy link
Contributor

drasko commented Jul 24, 2023

I think it is safe to close this one, as Subject can be derived from broker's metadata in the subscription handler.

@drasko drasko closed this Jul 24, 2023
Signed-off-by: SammyOina <sammyoina@gmail.com>
Signed-off-by: SammyOina <sammyoina@gmail.com>
@SammyOina SammyOina reopened this Jul 24, 2023
@SammyOina SammyOina changed the title NOISSUE - Add subject to broker subject mainflux message NOISSUE - Set subtopic for messages published outside adapters Jul 24, 2023
@@ -162,6 +162,9 @@ func (ps *pubsub) natsHandler(h messaging.MessageHandler) broker.MsgHandler {
ps.logger.Warn(fmt.Sprintf("Failed to unmarshal received message: %s", err))
return
}
if msg.Subtopic == "" {
msg.Subtopic = m.Subject // set for messages not published using adapters.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed? This is confusing.

If there is no subtopic, it should stay an empty string.

Signed-off-by: SammyOina <sammyoina@gmail.com>
@SammyOina
Copy link
Contributor Author

I am going to update the example in the docs instead which should have the same result

@SammyOina SammyOina closed this Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants