Skip to content

Commit

Permalink
Add documentation on how to propagate context information (#798)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrandl authored Sep 6, 2024
1 parent 3f55afa commit d69b553
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions website/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,35 @@ Or, use `weaver single deploy`:
$ weaver single deploy weaver.toml
```

## Context Propagation

You can propagate metadata information from a component method caller to the
callee. The metadata is propagated to the callee even if the caller and the callee
are not colocated in the same process.

The metadata is a map from string to string, stored in context.Context. You can
add the map to a context by calling `NewContext` and retrieve it by calling
`FromContext`:

```go
...
// Attach metadata with key "save_operation" and value "true" to the context.
// Call the Add method on the adder component.
ctx := context.Background()
ctx = metadata.NewContext(ctx, map[string]string{"save_operation": "true"})
adder.Add(ctx, 1, 2)
...
// Retrieve the metadata from the context
func (*adder) Add(ctx context.Context, x, y int) (int, error) {
meta, ok := metadata.FromContext(ctx)
if ok {
save := meta["save_operation"]
...
}
...
}
```

# Logging

<div hidden class="todo">
Expand Down

0 comments on commit d69b553

Please sign in to comment.