Skip to content

Commit

Permalink
refactor(instr-cucumber): use exported strings for attributes (#2143)
Browse files Browse the repository at this point in the history
Co-authored-by: Amir Blum <amirgiraffe@gmail.com>
  • Loading branch information
david-luna and blumamir committed Apr 29, 2024
1 parent 2d07c18 commit 49d77ce
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions plugins/node/instrumentation-cucumber/README.md
Expand Up @@ -43,6 +43,19 @@ Cucumber instrumentation has currently no options.
| Options | Type | Description |
| ------- | ---- | ----------- |

## Semantic Conventions

This package uses `@opentelemetry/semantic-conventions` version `1.22+`, which implements Semantic Convention [Version 1.7.0](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/semantic_conventions/README.md)

Attributes collected:

| Attribute | Short Description |
| ---------------- | -------------------------------------------------------------------------------- |
| `code.filepath` | The source code file name that identifies the code unit as uniquely as possible. |
| `code.function` | The method or function name, or equivalent. |
| `code.lineno` | The line number in `code.filepath` best representing the operation. |
| `code.namespace` | The "namespace" within which `code.function` is defined. |

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
2 changes: 1 addition & 1 deletion plugins/node/instrumentation-cucumber/package.json
Expand Up @@ -63,7 +63,7 @@
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.51.0",
"@opentelemetry/semantic-conventions": "^1.0.0"
"@opentelemetry/semantic-conventions": "^1.22.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-cucumber#readme"
}
15 changes: 10 additions & 5 deletions plugins/node/instrumentation-cucumber/src/instrumentation.ts
Expand Up @@ -21,7 +21,12 @@ import {
InstrumentationNodeModuleFile,
isWrapped,
} from '@opentelemetry/instrumentation';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMATTRS_CODE_FILEPATH,
SEMATTRS_CODE_FUNCTION,
SEMATTRS_CODE_LINENO,
SEMATTRS_CODE_NAMESPACE,
} from '@opentelemetry/semantic-conventions';

import type * as cucumber from '@cucumber/cucumber';
import type * as messages from '@cucumber/messages';
Expand Down Expand Up @@ -174,10 +179,10 @@ export class CucumberInstrumentation extends InstrumentationBase {
{
kind: SpanKind.CLIENT,
attributes: {
[SemanticAttributes.CODE_FILEPATH]: gherkinDocument.uri,
[SemanticAttributes.CODE_LINENO]: scenario.location.line,
[SemanticAttributes.CODE_FUNCTION]: scenario.name,
[SemanticAttributes.CODE_NAMESPACE]: feature.name,
[SEMATTRS_CODE_FILEPATH]: gherkinDocument.uri,
[SEMATTRS_CODE_LINENO]: scenario.location.line,
[SEMATTRS_CODE_FUNCTION]: scenario.name,
[SEMATTRS_CODE_NAMESPACE]: feature.name,
[AttributeNames.FEATURE_TAGS]: CucumberInstrumentation.mapTags(
feature.tags
),
Expand Down
19 changes: 12 additions & 7 deletions plugins/node/instrumentation-cucumber/test/cucumber.test.ts
Expand Up @@ -21,9 +21,14 @@ import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMATTRS_CODE_FILEPATH,
SEMATTRS_CODE_FUNCTION,
SEMATTRS_CODE_LINENO,
SEMATTRS_CODE_NAMESPACE,
SEMRESATTRS_SERVICE_NAME,
} from '@opentelemetry/semantic-conventions';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';

import * as path from 'path';
import * as assert from 'assert';
Expand All @@ -50,7 +55,7 @@ import { PassThrough } from 'stream';
describe('CucumberInstrumentation', () => {
const provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'CucumberInstrumentation',
[SEMRESATTRS_SERVICE_NAME]: 'CucumberInstrumentation',
}),
});
const memoryExporter = new InMemorySpanExporter();
Expand Down Expand Up @@ -165,10 +170,10 @@ describe('CucumberInstrumentation', () => {
assert(parent, 'Expected a parent span');

assert.deepEqual(parent.attributes, {
[SemanticAttributes.CODE_FILEPATH]: 'test/current.feature',
[SemanticAttributes.CODE_LINENO]: 7,
[SemanticAttributes.CODE_FUNCTION]: 'Button pushing',
[SemanticAttributes.CODE_NAMESPACE]: 'Basic',
[SEMATTRS_CODE_FILEPATH]: 'test/current.feature',
[SEMATTRS_CODE_LINENO]: 7,
[SEMATTRS_CODE_FUNCTION]: 'Button pushing',
[SEMATTRS_CODE_NAMESPACE]: 'Basic',
[AttributeNames.FEATURE_DESCRIPTION]:
' A very basic feature file with a single scenario',
[AttributeNames.FEATURE_LANGUAGE]: 'en',
Expand Down

0 comments on commit 49d77ce

Please sign in to comment.