question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When publishing a message allow setting Cloudevent attributes / Pub-Sub routing

See original GitHub issue

Proposal

Hi,

As we have the Pub/Sub routing feature, it’ll be very useful to be able to set specific Cloudevent attributes when publishing the message.

At this point, the Publish implementation accepts only the message data, and there seems to be no other way to override the event.type or another attribute, other than making a direct HTTP request to the sidecar with the application/cloudevents+json raw data.

Another useful feature would be to specify tracing headers for scenarios like the outbox pattern where a separate process publishes the messages, in order to preserve correct tracing

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:7
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mcriocommented, Oct 8, 2022

Hi @ChristianWeyer

Something along these lines:

// Dapr Json conventions
JsonSerializerOptions jsonOptions = new ();
jsonOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
jsonOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
jsonOptions.PropertyNameCaseInsensitive = true;

// `message` must be marked as object otherwise the serializer will serialize just the interface members
var cloudEvent = new { Type = routingKey, Data = (object)message };
string jsonCloudEvent = JsonSerializer.Serialize<object>(cloudEvent, jsonOptions);

var requestContent = new StringContent(
    jsonCloudEvent,
    Encoding.UTF8,
    "application/cloudevents+json"
);

var request = new HttpRequestMessage
{
    RequestUri = new Uri(
        $"/v1.0/publish/{pubSubName}/{topicName}",
        UriKind.Relative
    ),
    Method = HttpMethod.Post,
    Content = requestContent,
};
if (!string.IsNullOrWhiteSpace(traceIdentifier))
{
    request.Headers.Add("traceparent", traceIdentifier);
}

HttpResponseMessage response = await _httpClient.SendAsync(request).ConfigureAwait(false);
1reaction
mcriocommented, Oct 8, 2022

Hi @ChristianWeyer I haven’t had yet the chance to try it out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Publishing & subscribing messages with Cloudevents
To enable message routing and provide additional context with each message, Dapr uses the CloudEvents 1.0 specification as its message format.
Read more >
How-To: Route messages to different event handlers
With pub/sub routing, you use expressions to route CloudEvents (based on their contents) to different URIs/paths and event handlers in your application.
Read more >
Publish messages to topics | Cloud Pub/Sub
In the Topic details page under Messages, click Publish message. In the Message body field, enter the message data. Optional: Add message attributes....
Read more >
CloudEvents format - HTTP protocol binding | Eventarc
Target services receive events from event routers as HTTP POST requests formatted to the CloudEvents version 1.0 HTTP protocol binding ...
Read more >
CloudEvents Basics
In this post, let's explore CloudEvents in more depth. ... CloudEvents, at its core, defines a set of metadata, called “context attributes”.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found