Skip to content

Commit

Permalink
chore: Semantic Conventions export individual strings
Browse files Browse the repository at this point in the history
  • Loading branch information
MSNev committed Nov 17, 2023
1 parent 51be418 commit b39b6dd
Show file tree
Hide file tree
Showing 11 changed files with 4,282 additions and 507 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/

### :rocket: (Enhancement)

* chore: Semantic Conventions export individual strings [4185](https://github.com/open-telemetry/opentelemetry-js/issues/4185)

### :books: (Refine Doc)

### :house: (Internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import { SDK_INFO } from '@opentelemetry/core';
import * as assert from 'assert';
import { IResource, Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMRESATTRS_TELEMETRY_SDK_LANGUAGE,
SEMRESATTRS_TELEMETRY_SDK_NAME,
SEMRESATTRS_TELEMETRY_SDK_VERSION,
SemanticResourceAttributes,
} from '@opentelemetry/semantic-conventions';

/**
* Test utility method to validate a cloud resource
Expand Down Expand Up @@ -199,9 +204,9 @@ export const assertTelemetrySDKResource = (
}
) => {
const defaults = {
name: SDK_INFO.NAME,
language: SDK_INFO.LANGUAGE,
version: SDK_INFO.VERSION,
name: SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_NAME],
language: SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE],
version: SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_VERSION],
};
validations = { ...defaults, ...validations };

Expand Down Expand Up @@ -317,14 +322,17 @@ const assertHasOneLabel = (prefix: string, resource: Resource): void => {

assert.ok(
hasOne,
'Resource must have one of the following attributes: ' +
'Must have one node Resource(s) starting with [' +
prefix +
'] matching the following attributes: ' +
Object.entries(SemanticResourceAttributes)
.reduce((result, [key, value]) => {
if (key.startsWith(prefix)) {
result.push(value);
}
return result;
})
.join(', ')
.join(', ') +
JSON.stringify(Object.keys(SemanticResourceAttributes))
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import * as assert from 'assert';
import { RAW_ENVIRONMENT } from '@opentelemetry/core';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { envDetector, IResource } from '../../../src';
import {
assertEmptyResource,
Expand All @@ -41,9 +40,9 @@ describeBrowser('envDetector() on web browser', () => {
it('should return resource information from environment variable', async () => {
const resource: IResource = await envDetector.detect();
assertWebEngineResource(resource, {
[SemanticResourceAttributes.WEBENGINE_NAME]: 'chromium',
[SemanticResourceAttributes.WEBENGINE_VERSION]: '99',
[SemanticResourceAttributes.WEBENGINE_DESCRIPTION]: 'Chromium',
name: 'chromium',
version: '99',
description: 'Chromium',
});
assert.strictEqual(resource.attributes['custom.key'], 'custom value');
});
Expand Down
16 changes: 12 additions & 4 deletions packages/opentelemetry-resources/test/resource-assertions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
*/

import { SDK_INFO } from '@opentelemetry/core';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMRESATTRS_TELEMETRY_SDK_LANGUAGE,
SEMRESATTRS_TELEMETRY_SDK_NAME,
SEMRESATTRS_TELEMETRY_SDK_VERSION,
SemanticResourceAttributes,
} from '@opentelemetry/semantic-conventions';
import { Resource } from '../src/Resource';
import {
assertCloudResource,
Expand Down Expand Up @@ -132,9 +137,12 @@ describe('assertK8sResource', () => {
describe('assertTelemetrySDKResource', () => {
it('uses default validations', () => {
const resource = new Resource({
[SemanticResourceAttributes.TELEMETRY_SDK_NAME]: SDK_INFO.NAME,
[SemanticResourceAttributes.TELEMETRY_SDK_LANGUAGE]: SDK_INFO.LANGUAGE,
[SemanticResourceAttributes.TELEMETRY_SDK_VERSION]: SDK_INFO.VERSION,
[SemanticResourceAttributes.TELEMETRY_SDK_NAME]:
SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_NAME],
[SemanticResourceAttributes.TELEMETRY_SDK_LANGUAGE]:
SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE],
[SemanticResourceAttributes.TELEMETRY_SDK_VERSION]:
SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_VERSION],
});
assertTelemetrySDKResource(resource, {});
});
Expand Down
20 changes: 14 additions & 6 deletions packages/opentelemetry-resources/test/util/resource-assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import { SDK_INFO } from '@opentelemetry/core';
import * as assert from 'assert';
import { IResource } from '../../src/IResource';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMRESATTRS_TELEMETRY_SDK_LANGUAGE,
SEMRESATTRS_TELEMETRY_SDK_NAME,
SEMRESATTRS_TELEMETRY_SDK_VERSION,
SemanticResourceAttributes,
} from '@opentelemetry/semantic-conventions';

/**
* Test utility method to validate a cloud resource
Expand Down Expand Up @@ -199,9 +204,9 @@ export const assertTelemetrySDKResource = (
}
) => {
const defaults = {
name: SDK_INFO.NAME,
language: SDK_INFO.LANGUAGE,
version: SDK_INFO.VERSION,
name: SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_NAME],
language: SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE],
version: SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_VERSION],
};
validations = { ...defaults, ...validations };

Expand Down Expand Up @@ -382,14 +387,17 @@ const assertHasOneLabel = (prefix: string, resource: IResource): void => {

assert.ok(
hasOne,
'Resource must have one of the following attributes: ' +
'Must have one Resource(s) starting with [' +
prefix +
'] matching the following attributes: ' +
Object.entries(SemanticResourceAttributes)
.reduce((result, [key, value]) => {
if (key.startsWith(prefix)) {
result.push(value);
}
return result;
})
.join(', ')
.join(', ') +
JSON.stringify(Object.keys(SemanticResourceAttributes))
);
};
101 changes: 101 additions & 0 deletions packages/opentelemetry-semantic-conventions/src/internal/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export const ACCOUNT_DOT = 'account.';
export const ARN = 'arn';
export const AWS = 'aws';
export const AZURE = 'azure';
export const CASSANDRA_DOT = 'cassandra.';
export const CARRIER_DOT = 'carrier.';
export const CLOUD_DOT = 'cloud.';
export const CLUSTER_DOT = 'cluster.';
export const CODE_DOT = 'code.';
export const COLLECTION = 'collection';
export const COMMAND = 'command';
export const COMPRESSED = 'compressed';
export const CONTAINER_DOT = 'container.';
export const CONTENT = 'content';
export const CONNECTION = 'connection';
export const COORDINATOR_DOT = 'coordinator.';
export const CRON = 'cron';
export const DAEMON = 'daemon';
export const DB_DOT = 'db.';
export const DEPLOYMENT_DOT = 'deployment.';
export const DESCRIPTION = 'description';
export const DEVICE_DOT = 'device.';
export const DOCUMENT_DOT = 'document.';
export const DOT = '.';
export const DYNAMODB_DOT = 'dynamodb.';
export const ECS_DOT = 'ecs.';
export const EKS_DOT = 'eks.';
export const EXECUTABLE_DOT = 'executable.';
export const ENDUSER_DOT = 'enduser.';
export const EXCEPTION_DOT = 'exception.';
export const FAAS_DOT = 'faas.';
export const GLOBAL = 'global';
export const GROUP_DOT = 'group.';
export const KAFKA_DOT = 'kafka.';
export const K8S_DOT = 'k8s.';
export const HTTP_DOT = 'http.';
export const HOST = 'host';
export const ID = 'id';
export const IMAGE_DOT = 'image.';
export const INDEX = 'index';
export const INSTANCE = 'instance';
export const INVOKED = 'invoked';
export const JOB = 'job';
export const JSONRPC_DOT = 'jsonrpc.';
export const LAMBDA_DOT = 'lambda.';
export const LENGTH = 'length';
export const LOCAL = 'local';
export const LOG_DOT = 'log.';
export const MESSAGE = 'message';
export const MESSAGING_DOT = 'messaging.';
export const MODEL_DOT = 'model.';
export const MONGODB_DOT = 'mongodb.';
export const MSSQL_DOT = 'mssql.';
export const NAME = 'name';
export const NET_DOT = 'net.';
export const NODE_DOT = 'node.';
export const OS_DOT = 'os.';
export const PEER_DOT = 'peer.';
export const PORT = 'port';
export const PROCESS_DOT = 'process.';
export const PROTOCOL = 'protocol';
export const PROVISIONED = 'provisioned';
export const READ = 'read';
export const REQUEST = 'request';
export const REPLICA = 'replica';
export const RESPONSE = 'response';
export const RPC_DOT = 'rpc.';
export const RUNTIME_DOT = 'runtime.';
export const SDK_DOT = 'sdk.';
export const SECONDARY = 'secondary';
export const SERVICE_DOT = 'service.';
export const SET_DOT = 'set.';
export const SPACE = 'space';
export const STATEFUL = 'stateful';
export const STREAM_DOT = 'stream.';
export const TASK_DOT = 'task.';
export const TELEMETRY_DOT = 'telemetry.';
export const THREAD_DOT = 'thread.';
export const TYPE = 'type';
export const UID = 'uid';
export const UN = 'un';
export const UNDERSCORE = '_';
export const WEBENGINE_DOT = 'webengine.';
export const WRITE = 'write';
export const VERSION = 'version';
34 changes: 34 additions & 0 deletions packages/opentelemetry-semantic-conventions/src/internal/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Creates a const map from the given values
* @param values - An array of values to be used as keys and values in the map.
* @returns A populated version of the map with the values and keys derived from the values.
*/
export const createConstMap = <T>(values: Array<T[keyof T]>): T => {
// eslint-disable-next-line prefer-const, @typescript-eslint/no-explicit-any
let res: any = {};
const len = values.length;
for (let lp = 0; lp < len; lp++) {
const val = values[lp];
if (val) {
res[String(val).toUpperCase().replace(/[-.]/g, '_')] = val;
}
}

return res as T;
};
Loading

0 comments on commit b39b6dd

Please sign in to comment.