Skip to content

Instantly share code, notes, and snippets.

@tripodsan
Created March 27, 2020 02:52
Show Gist options
  • Save tripodsan/9e0f04e1a5572c0ead3d53d93fab0a2f to your computer and use it in GitHub Desktop.
Save tripodsan/9e0f04e1a5572c0ead3d53d93fab0a2f to your computer and use it in GitHub Desktop.
Adobe I/O Runtime Transaction ID behaviour

Transaction ID behaviour.

see ./transaction.js:

function main(params) {
  const { __ow_headers: headers = {} } = params;
  const body = {
    transactionId: process.env.__OW_TRANSACTION_ID,
    requestId: headers['x-request-id'] || 'n/a',
  };
  return {
    statusCode: 200,
    headers: { 'Content-Type': 'application/json'},
    body,
  };
}

the process.env.__OW_TRANSACTION_ID is set based on the x-request-id header:

$ curl -sD - https://adobeioruntime.net/api/v1/web/tripod/default/transaction
HTTP/1.1 200 OK
...
x-openwhisk-activation-id: 5cb3efd2c86d4088b3efd2c86d208847
X-Request-ID: jJZA1hZ5wKCfNEvalA0cnTnWiUd5Alfo

{
  "requestId": "jJZA1hZ5wKCfNEvalA0cnTnWiUd5Alfo",
  "transactionId": "jJZA1hZ5wKCfNEvalA0cnTnWiUd5Alfo"
}

the x-request-id header can also be provided:

$ curl -sD - https://adobeioruntime.net/api/v1/web/tripod/default/transaction -H 'x-request-id: toby-was-here'
HTTP/1.1 200 OK
...
x-openwhisk-activation-id: 3d8ecb11ff65448b8ecb11ff65c48b92
X-Request-ID: toby-was-here

{
  "requestId": "toby-was-here",
  "transactionId": "toby-was-here"
}
$

invoking via wsk client obviously doesn't have a __ow_headers:

$ wsk action invoke -r transaction
{
    "body": {
        "requestId": "n/a",
        "transactionId": "QzysZzIC9npxTUlgHWnBeA5xXs5iYtUw"
    },
    "headers": {
        "Content-Type": "application/json"
    },
    "statusCode": 200
}

and even if adding it to the params, the process.env.__OW_TRANSACTION_ID is not set:

$ cat transaction.json
{
  "__ow_headers": {
    "x-request-id": "toby-was-here"
  }
}

$ wsk action invoke -r transaction -P transaction.json
{
    "body": {
        "requestId": "toby-was-here",
        "transactionId": "ye3WgpMwvta3CZfaiWojP8pa88wwzTJ7"
    },
    "headers": {
        "Content-Type": "application/json"
    },
    "statusCode": 200
}

but invoking the API directly, works:

$ curl -sD - -X POST \
    -H "x-request-id: toby-was-here" \
    -u "$WSK_AUTH" \
    'https://adobeioruntime.net/api/v1/namespaces/tripod/actions/transaction?blocking=true&result=true' 
 
HTTP/1.1 200 OK
...
x-openwhisk-activation-id: dc3cbcf5d7014258bcbcf5d70152588c
X-Request-ID: toby-was-here
Content-Length: 123
Connection: keep-alive

{"body":{"requestId":"n/a","transactionId":"toby-was-here"},"headers":{"Content-Type":"application/json"},"statusCode":200}

patching the openwhisk client

the openwhisk client can be patched in order to propagate the transaction id:

    const ow = openwhisk();
    const { client } = ow.actions;
    const original_params = client.params.bind(client);
    client.params = async (...args) => {
      const ps = await original_params(...args);
      ps.headers['x-request-id'] = process.env.__OW_TRANSACTION_ID;
      return ps;
    };

so, invoking another action:

    const ret = await ow.actions.invoke({
      name: 'transaction',
      blocking: true,
      result: true,
    });
    body.invoke_result = ret;

works:

$ curl -sD - "https://adobeioruntime.net/api/v1/web/tripod/default/transaction?recursive=true" -H 'x-request-id: toby-was-here'
HTTP/1.1 200 OK
...
x-openwhisk-activation-id: a6626cec6fbf4dfda26cec6fbf8dfdc1
X-Request-ID: toby-was-here

{
  "invoke_result": {
    "body": {
      "requestId": "n/a",
      "transactionId": "toby-was-here"
    },
    "headers": {
      "Content-Type": "application/json"
    },
    "statusCode": 200
  },
  "requestId": "toby-was-here",
  "transactionId": "toby-was-here"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment