Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions typescript-sandbox/src/mocha/observability.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import { logs } from '../platform/observability/logging/accumulate-logs';
import { listRunning } from '../platform/observability/visibility/list';
import { startWithSearchAttributes } from '../platform/observability/visibility/client';
import { yourWorkflow } from '../platform/observability/visibility/workflows';
import {
customBoolKey,
customDoubleKey,
customIntKey,
customKeywordListKey,
} from '../platform/observability/visibility/search-attributes';
import { tracedWorkflow } from '../platform/observability/tracing/workflows';
import assert from 'assert';

Expand Down Expand Up @@ -216,21 +222,22 @@ describe('Observability', function () {
});

it('sets Search Attributes at start and upserts them in the Workflow', async () => {
const atStart = await startWithSearchAttributes(testEnv.client);
assert.deepEqual(atStart.CustomIntField, [2]);
assert.deepEqual(atStart.CustomBoolField, [true]);
const final = await testEnv.client.workflow.getHandle('search-attributes-example-0').result();
assert.deepEqual(final.CustomIntField, [3]);
assert.deepEqual(final.CustomDoubleField, [3.14]);
assert.equal(final.CustomBoolField, undefined);
assert.equal(await startWithSearchAttributes(testEnv.client), 2);
const { typedSearchAttributes } = await testEnv.client.workflow.getHandle('search-attributes-example-0').describe();
assert.equal(typedSearchAttributes.get(customIntKey), 3);
assert.equal(typedSearchAttributes.get(customDoubleKey), 3.14);
assert.equal(typedSearchAttributes.get(customBoolKey), undefined);
assert.deepEqual(typedSearchAttributes.get(customKeywordListKey), ['keywordA', 'keywordB']);
});

it('removes a Search Attribute by setting it to an empty array', async () => {
const result = await testEnv.client.workflow.execute(yourWorkflow, {
it('removes a Search Attribute by setting it to null', async () => {
const handle = await testEnv.client.workflow.start(yourWorkflow, {
taskQueue: 'search-attributes',
workflowId: 'remove-search-attribute',
});
assert.equal(result.CustomIntField, undefined);
await handle.result();
const { typedSearchAttributes } = await handle.describe();
assert.equal(typedSearchAttributes.get(customIntKey), undefined);
});
});

Expand Down
32 changes: 19 additions & 13 deletions typescript-sandbox/src/platform/observability/visibility/client.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import { Client, WorkflowExecutionDescription } from '@temporalio/client';
import { Client } from '@temporalio/client';
import { example } from './workflows';
import {
customBoolKey,
customDatetimeKey,
customIntKey,
customKeywordListKey,
customTextKey,
} from './search-attributes';

export async function startWithSearchAttributes(client: Client): Promise<WorkflowExecutionDescription['searchAttributes']> {
export async function startWithSearchAttributes(client: Client): Promise<number | undefined> {
// @@@SNIPSTART typescript-observability-set-search-attributes
const handle = await client.workflow.start(example, {
taskQueue: 'search-attributes',
workflowId: 'search-attributes-example-0',
searchAttributes: {
CustomIntField: [2],
CustomKeywordListField: ['keywordA', 'keywordB'],
CustomBoolField: [true],
CustomDatetimeField: [new Date()],
CustomTextField: [
'String field is for text. When queried, it will be tokenized for partial match. StringTypeField cannot be used in Order By',
],
},
typedSearchAttributes: [
{ key: customIntKey, value: 2 },
{ key: customKeywordListKey, value: ['keywordA', 'keywordB'] },
{ key: customBoolKey, value: true },
{ key: customDatetimeKey, value: new Date() },
{ key: customTextKey, value: 'Text values are tokenized, so you can query them for a partial match.' },
],
});

const { searchAttributes } = await handle.describe();
const { typedSearchAttributes } = await handle.describe();
const customInt = typedSearchAttributes.get(customIntKey); // number | undefined
// @@@SNIPEND
await handle.result();
return searchAttributes;
return customInt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @@@SNIPSTART typescript-observability-define-search-attribute-keys
import { defineSearchAttributeKey } from '@temporalio/common';

export const customIntKey = defineSearchAttributeKey('CustomIntField', 'INT');
export const customKeywordListKey = defineSearchAttributeKey('CustomKeywordListField', 'KEYWORD_LIST');
export const customBoolKey = defineSearchAttributeKey('CustomBoolField', 'BOOL');
export const customDatetimeKey = defineSearchAttributeKey('CustomDatetimeField', 'DATETIME');
export const customTextKey = defineSearchAttributeKey('CustomTextField', 'TEXT');
export const customDoubleKey = defineSearchAttributeKey('CustomDoubleField', 'DOUBLE');
// @@@SNIPEND
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import { SearchAttributes, upsertSearchAttributes, workflowInfo } from '@temporalio/workflow';
import { upsertSearchAttributes, workflowInfo } from '@temporalio/workflow';
import { customBoolKey, customDoubleKey, customIntKey } from './search-attributes';

// @@@SNIPSTART typescript-observability-upsert-search-attributes
export async function example(): Promise<SearchAttributes> {
const customInt = (workflowInfo().searchAttributes.CustomIntField?.[0] as number) || 0;
upsertSearchAttributes({
// overwrite the existing CustomIntField: [2]
CustomIntField: [customInt + 1],
export async function example(): Promise<void> {
const customInt = workflowInfo().typedSearchAttributes.get(customIntKey) ?? 0;
upsertSearchAttributes([
// overwrite the existing CustomIntField value of 2
{ key: customIntKey, value: customInt + 1 },

// delete the existing CustomBoolField: [true]
CustomBoolField: [],
// remove the existing CustomBoolField
{ key: customBoolKey, value: null },

// add a new value
CustomDoubleField: [3.14],
});
return workflowInfo().searchAttributes;
{ key: customDoubleKey, value: 3.14 },
]);
}
// @@@SNIPEND

// @@@SNIPSTART typescript-observability-remove-search-attribute
export async function yourWorkflow(): Promise<SearchAttributes> {
upsertSearchAttributes({ CustomIntField: [1] });
export async function yourWorkflow(): Promise<void> {
upsertSearchAttributes([{ key: customIntKey, value: 1 }]);

// ... later, to remove:
upsertSearchAttributes({ CustomIntField: [] });
upsertSearchAttributes([{ key: customIntKey, value: null }]);
// @@@SNIPEND
return workflowInfo().searchAttributes;
}
Loading