Skip to content

Carry headers and foreign fields

Warning

MassTransit 9 is commercially licensed. MassTransit 9 and later is under the MassTransit Commercial Software License Agreement (massient.com/license), a paid subscription with no exemption for testing. This format was captured from MassTransit 8, which is Apache-2.0, and the module's own .NET test programs stay on 8.x (envelope-masstransit#1). The Go module itself does not depend on MassTransit.

Send an extension

An extension is written into the envelope's headers object, under its own name, as a string:

e.Extensions = map[string]string{"tenant": "continuity"}
"headers": {"tenant": "continuity"}

Names follow envelope.ValidExtensionName: lower-case ASCII letters and digits. The consumer program in the module prints every header MassTransit's deserializer returns as a header: line. Subject, DataSchema and DataContentType also travel here, as ce-subject, ce-dataschema and ce-datacontenttype.

Every extension value must be safe to log. See the event model.

Send a trace

Set the traceparent extension, which the format writes as a header of that name:

e.Extensions = map[string]string{"traceparent": "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01"}

Read a trace

A MassTransit producer's trace is a header. The format promotes it:

  • A traceparent in the envelope's headers is the extension traceparent, because it is a string under a valid extension name.
  • A traceparent only the transport carries is promoted too. The envelope's wins when both exist.
  • A case variant such as Traceparent is never promoted. It is foreign.
trace := e.Extensions["traceparent"]

Read the correlation ID

MassTransit's correlationId, conversationId, requestId and initiatorId are not part of the event. They arrive as foreign headers:

if ids := foreign.Headers["correlationId"]; len(ids) > 0 {
    logger.Info("correlated", "correlation", ids[0])
}

The key is the envelope member's name, in camel case. A null member is absent.

Read a header you set on the producer

A header the producer set with Headers.Set("Tenant", ...) is in the envelope's headers. If its name is a valid extension name, which means lower case only, it is an extension. If not, as with Tenant, it is in foreign.Headers["Tenant"]. MassTransit also mirrors it onto the transport, and the format lists it once.

Check for a conflict

If the transport carries MessageId, traceparent or another header the event already holds, with a different value, the envelope's value stays in the event and the key is in foreign.Conflicts:

for _, key := range foreign.Conflicts {
    logger.Warn("transport disagrees with the envelope", "key", key, "transport", foreign.Headers[key])
}

What you cannot send

Foreign is read-only. The format writes only the members and headers above, so a correlation ID received from MassTransit does not go back out on a reply. To correlate, put an identifier in an extension.