Skip to content

Commit

Permalink
Document gRPC message metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Sep 25, 2024
1 parent 8612ba0 commit 23ec317
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/sources/next/javascript-api/k6-net-grpc/stream/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Using a gRPC client creates a stream. The client should be connected to the serv
| [Stream.write(message)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/stream-write) | Writes a message to the stream. |
| [Stream.on(event, handler)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/stream-on) | Sets up handler functions for various events on the gRPC stream. |
| [Stream.end()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/stream-end) | Signals to the server that the client has finished sending. |
| [EventHandler](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/event-handler) | The handler function for various events on the gRPC stream. |
| [Metadata](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/message-metadata) | The metadata of a gRPC stream's message. |

### Examples

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: 'Event Handler'
description: 'The handler function for various events on the gRPC stream.'
weight: 50
---

# Event Handler

The function to call for various events on the gRPC stream. It is set up using the [`stream.on()`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/stream-on) method.

| Name | Type | Description |
| ---------- | ------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------- |
| `data` | object or [`Error`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/stream-error) | It's either object with the data from server (a message) or an error object, in case of `error` event |
| `metadata` | [`Metadata`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/message-metadata) | The object that represents the gRPC stream's message metadata. |

### Example

<div class="code-group" data-props='{"labels": ["A handler metadata example"], "lineNumbers": [true]}'>

```javascript
import { Client, Stream } from 'k6/net/grpc';
import { sleep } from 'k6';

const client = new Client();
client.load([], '../../grpc_server/route_guide.proto');

export default () => {
if (__ITER == 0) {
client.connect('127.0.0.1:10000', { plaintext: true });
}

const stream = new Stream(client, 'main.RouteGuide/RecordRoute');

// sets up a handler for the data (server sends data) event
stream.on('data', (stats, metadata) => {
console.log('It took', stats.elapsedTime, 'seconds');
console.log('This message has been received:', metadata.ts);
});

stream.on('end', function () {
// The server has finished sending
client.close();
});

sleep(1);
};
```

</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: 'Metadata'
description: "The metadata of a gRPC stream's message."
weight: 60
---

# Metadata

The `Metadata` is an object that represents the gRPC stream's message.

| Name | Type | Description |
| ------------- | ------ | -------------------------------------------------------------------------------------------- |
| `Metadata.ts` | number | Contains the timestamp of the original event. For example, when a message has been received. |

### Example

<div class="code-group" data-props='{"labels": ["A handler metadata example"], "lineNumbers": [true]}'>

```javascript
import { Client, Stream } from 'k6/net/grpc';
import { sleep } from 'k6';

const client = new Client();
client.load([], '../../grpc_server/route_guide.proto');

export default () => {
if (__ITER == 0) {
client.connect('127.0.0.1:10000', { plaintext: true });
}

const stream = new Stream(client, 'main.RouteGuide/RecordRoute');

// sets up a handler for the data (server sends data) event
stream.on('data', (stats, metadata) => {
console.log('It took', stats.elapsedTime, 'seconds');
console.log('This message has been received:', metadata.ts);
});

stream.on('end', function () {
// The server has finished sending
client.close();
});

sleep(1);
};
```

</div>
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ aliases:

Set up handler functions for various events on the gRPC stream.

| Parameter | Type | Description |
| --------- | -------- | -------------------------------------------- |
| event | string | The event name to define a handler for. |
| handler | function | The function to call when the event happens. |
| Parameter | Type | Description |
| --------- | ---------------------------------------------------------------------------------------------------------- | -------------------------------------------- |
| event | string | The event name to define a handler for. |
| handler | [`EventHandler`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/event-handler) | The function to call when the event happens. |

Possible events:

Expand Down
2 changes: 2 additions & 0 deletions docs/sources/next/shared/javascript-api/k6-net-grpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ The [`k6/net/grpc` module](https://grafana.com/docs/k6/<K6_VERSION>/javascript-a
| [Stream.on(event, handler)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/stream-on) | Adds a new listener to one of the possible stream events. |
| [Stream.write(message)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/stream-write) | Writes a message to the stream. |
| [Stream.end()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/stream-end) | Signals to the server that the client has finished sending. |
| [EventHandler](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/event-handler) | The function to call for various events on the gRPC stream. |
| [Metadata](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/message-metadata) | The metadata of a gRPC stream’s message. |

0 comments on commit 23ec317

Please sign in to comment.