Skip to content

Commit

Permalink
chore: update usage of @opentelemetry/semantic-conventions to avoid d…
Browse files Browse the repository at this point in the history
…eprecated namespaces (#713)

[Since v1.22.0](https://github.com/open-telemetry/opentelemetry-js/blob/main/CHANGELOG.md#rocket-enhancement-3), the semantic conventions package exports individual strings and deprecated the object namespace approach. Note, this is not a breaking change or affect our support in any way.
  • Loading branch information
aabmass committed May 2, 2024
1 parent be4ae61 commit 211582e
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 138 deletions.
6 changes: 2 additions & 4 deletions package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"dependencies": {
"@google-cloud/opentelemetry-resource-util": "^2.1.0",
"@google-cloud/precise-date": "^4.0.0",
"@opentelemetry/semantic-conventions": "^1.0.0",
"google-auth-library": "^9.0.0",
"googleapis": "^135.0.0"
},
Expand Down
17 changes: 12 additions & 5 deletions packages/opentelemetry-cloud-trace-exporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,22 @@ By default, OpenTelemetry resource attributes which do not map to a monitored re

For example, if you are setting up a resource with the "service" semantic attributes:
```typescript
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const {
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_SERVICE_NAMESPACE,
SEMRESATTRS_SERVICE_VERSION,
SEMRESATTRS_SERVICE_INSTANCE_ID,
} = require('@opentelemetry/semantic-conventions');

const provider = new NodeTracerProvider({
// ...
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'things-service',
[SemanticResourceAttributes.SERVICE_NAMESPACE]: 'things',
[SemanticResourceAttributes.SERVICE_VERSION]: '1.0.0',
[SemanticResourceAttributes.SERVICE_INSTANCE_ID]: 'abc123',

SEMRESATTRS_SERVICE_NAME,
[SEMRESATTRS_SERVICE_NAME]: 'things-service',
[SEMRESATTRS_SERVICE_NAMESPACE]: 'things',
[SEMRESATTRS_SERVICE_VERSION]: '1.0.0',
[SEMRESATTRS_SERVICE_INSTANCE_ID]: 'abc123',
}),
})
```
Expand Down
6 changes: 3 additions & 3 deletions packages/opentelemetry-resource-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@
"typescript": "4.9.5"
},
"peerDependencies": {
"@opentelemetry/resources": "^1.0.0",
"@opentelemetry/semantic-conventions": "^1.0.0"
"@opentelemetry/resources": "^1.0.0"
},
"dependencies": {
"gcp-metadata": "^6.0.0"
"gcp-metadata": "^6.0.0",
"@opentelemetry/semantic-conventions": "^1.22.0"
}
}
87 changes: 51 additions & 36 deletions packages/opentelemetry-resource-util/src/detector/detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,33 @@
// limitations under the License.

import {
SemanticResourceAttributes as Semconv,
CloudPlatformValues,
CloudProviderValues,
CLOUDPLATFORMVALUES_GCP_APP_ENGINE,
CLOUDPLATFORMVALUES_GCP_CLOUD_FUNCTIONS,
CLOUDPLATFORMVALUES_GCP_CLOUD_RUN,
CLOUDPLATFORMVALUES_GCP_COMPUTE_ENGINE,
CLOUDPLATFORMVALUES_GCP_KUBERNETES_ENGINE,
CLOUDPROVIDERVALUES_GCP,
SEMRESATTRS_CLOUD_ACCOUNT_ID,
SEMRESATTRS_CLOUD_AVAILABILITY_ZONE,
SEMRESATTRS_CLOUD_PLATFORM,
SEMRESATTRS_CLOUD_PROVIDER,
SEMRESATTRS_CLOUD_REGION,
SEMRESATTRS_FAAS_INSTANCE,
SEMRESATTRS_FAAS_NAME,
SEMRESATTRS_FAAS_VERSION,
SEMRESATTRS_HOST_ID,
SEMRESATTRS_HOST_NAME,
SEMRESATTRS_HOST_TYPE,
SEMRESATTRS_K8S_CLUSTER_NAME,
} from '@opentelemetry/semantic-conventions';

import {Attributes} from '@opentelemetry/api';
import {Detector, DetectorSync, Resource} from '@opentelemetry/resources';
import * as gce from './gce';
import * as gke from './gke';
import * as metadata from 'gcp-metadata';
import * as faas from './faas';
import * as gae from './gae';
import * as metadata from 'gcp-metadata';
import {Attributes} from '@opentelemetry/api';
import * as gce from './gce';
import * as gke from './gke';

async function detect(): Promise<Resource> {
if (!(await metadata.isAvailable())) {
Expand Down Expand Up @@ -56,12 +71,12 @@ async function gkeResource(): Promise<Resource> {
]);

return await makeResource({
[Semconv.CLOUD_PLATFORM]: CloudPlatformValues.GCP_KUBERNETES_ENGINE,
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_GCP_KUBERNETES_ENGINE,
[zoneOrRegion.type === 'zone'
? Semconv.CLOUD_AVAILABILITY_ZONE
: Semconv.CLOUD_REGION]: zoneOrRegion.value,
[Semconv.K8S_CLUSTER_NAME]: k8sClusterName,
[Semconv.HOST_ID]: hostId,
? SEMRESATTRS_CLOUD_AVAILABILITY_ZONE
: SEMRESATTRS_CLOUD_REGION]: zoneOrRegion.value,
[SEMRESATTRS_K8S_CLUSTER_NAME]: k8sClusterName,
[SEMRESATTRS_HOST_ID]: hostId,
});
}

Expand All @@ -75,11 +90,11 @@ async function cloudRunResource(): Promise<Resource> {
]);

return await makeResource({
[Semconv.CLOUD_PLATFORM]: CloudPlatformValues.GCP_CLOUD_RUN,
[Semconv.FAAS_NAME]: faasName,
[Semconv.FAAS_VERSION]: faasVersion,
[Semconv.FAAS_INSTANCE]: faasInstance,
[Semconv.CLOUD_REGION]: faasCloudRegion,
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_GCP_CLOUD_RUN,
[SEMRESATTRS_FAAS_NAME]: faasName,
[SEMRESATTRS_FAAS_VERSION]: faasVersion,
[SEMRESATTRS_FAAS_INSTANCE]: faasInstance,
[SEMRESATTRS_CLOUD_REGION]: faasCloudRegion,
});
}

Expand All @@ -93,11 +108,11 @@ async function cloudFunctionsResource(): Promise<Resource> {
]);

return await makeResource({
[Semconv.CLOUD_PLATFORM]: CloudPlatformValues.GCP_CLOUD_FUNCTIONS,
[Semconv.FAAS_NAME]: faasName,
[Semconv.FAAS_VERSION]: faasVersion,
[Semconv.FAAS_INSTANCE]: faasInstance,
[Semconv.CLOUD_REGION]: faasCloudRegion,
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_GCP_CLOUD_FUNCTIONS,
[SEMRESATTRS_FAAS_NAME]: faasName,
[SEMRESATTRS_FAAS_VERSION]: faasVersion,
[SEMRESATTRS_FAAS_INSTANCE]: faasInstance,
[SEMRESATTRS_CLOUD_REGION]: faasCloudRegion,
});
}

Expand All @@ -118,12 +133,12 @@ async function gaeResource(): Promise<Resource> {
]);

return await makeResource({
[Semconv.CLOUD_PLATFORM]: CloudPlatformValues.GCP_APP_ENGINE,
[Semconv.FAAS_NAME]: faasName,
[Semconv.FAAS_VERSION]: faasVersion,
[Semconv.FAAS_INSTANCE]: faasInstance,
[Semconv.CLOUD_AVAILABILITY_ZONE]: zone,
[Semconv.CLOUD_REGION]: region,
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_GCP_APP_ENGINE,
[SEMRESATTRS_FAAS_NAME]: faasName,
[SEMRESATTRS_FAAS_VERSION]: faasVersion,
[SEMRESATTRS_FAAS_INSTANCE]: faasInstance,
[SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: zone,
[SEMRESATTRS_CLOUD_REGION]: region,
});
}

Expand All @@ -136,21 +151,21 @@ async function gceResource(): Promise<Resource> {
]);

return await makeResource({
[Semconv.CLOUD_PLATFORM]: CloudPlatformValues.GCP_COMPUTE_ENGINE,
[Semconv.CLOUD_AVAILABILITY_ZONE]: zoneAndRegion.zone,
[Semconv.CLOUD_REGION]: zoneAndRegion.region,
[Semconv.HOST_TYPE]: hostType,
[Semconv.HOST_ID]: hostId,
[Semconv.HOST_NAME]: hostName,
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_GCP_COMPUTE_ENGINE,
[SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: zoneAndRegion.zone,
[SEMRESATTRS_CLOUD_REGION]: zoneAndRegion.region,
[SEMRESATTRS_HOST_TYPE]: hostType,
[SEMRESATTRS_HOST_ID]: hostId,
[SEMRESATTRS_HOST_NAME]: hostName,
});
}

async function makeResource(attrs: Attributes): Promise<Resource> {
const project = await metadata.project<string>('project-id');

return new Resource({
[Semconv.CLOUD_PROVIDER]: CloudProviderValues.GCP,
[Semconv.CLOUD_ACCOUNT_ID]: project,
[SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_GCP,
[SEMRESATTRS_CLOUD_ACCOUNT_ID]: project,
...attrs,
});
}
Expand Down
Loading

0 comments on commit 211582e

Please sign in to comment.