Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan committed Dec 12, 2024
1 parent c1135eb commit 3a05fb1
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 31 deletions.
19 changes: 8 additions & 11 deletions packages/opentelemetry-resources/src/ResWithEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { Attributes, AttributeValue, diag } from '@opentelemetry/api';
import { Entity, EntityRef, mergeEntities } from './entity';
import { DetectedResourceAttributes, ResourceAttributes } from './types';
import { isPromiseLike } from './utils';
import { identity, isPromiseLike } from './utils';

export class ResWithEntity {
private _rawAttributes: [string, AttributeValue | Promise<AttributeValue>][];
Expand Down Expand Up @@ -48,16 +48,13 @@ export class ResWithEntity {

return [
k,
v.then(
v => v,
err => {
diag.debug(
"a resource's async attributes promise rejected: %s",
err
);
return [k, undefined];
}
),
v.then(identity, err => {
diag.debug(
"a resource's async attributes promise rejected: %s",
err
);
return [k, undefined];
}),
];
}

Expand Down
15 changes: 15 additions & 0 deletions packages/opentelemetry-resources/src/entity-detector.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* 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.
*/
import { Entity } from './entity';

/*
Expand Down
37 changes: 25 additions & 12 deletions packages/opentelemetry-resources/src/entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
/*
* 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.
*/

import { Attributes, AttributeValue, diag } from '@opentelemetry/api';
import { IDetectedEntity } from './types';
import { isPromiseLike } from './utils';
import { identity, isPromiseLike } from './utils';

export interface EntityRef {
type: string;
Expand Down Expand Up @@ -29,16 +45,13 @@ export class Entity {

return [
k,
v.then(
v => v,
err => {
diag.debug(
"a resource's async attributes promise rejected: %s",
err
);
return [k, undefined];
}
),
v.then(identity, err => {
diag.debug(
"a resource's async attributes promise rejected: %s",
err
);
return [k, undefined];
}),
];
}

Expand Down Expand Up @@ -147,7 +160,7 @@ export function mergeEntities(...entities: Entity[]): Entity[] {
}

function attrsEqual(obj1: Attributes, obj2: Attributes) {
if (Object.keys(obj1).length != Object.keys(obj2).length) {
if (Object.keys(obj1).length !== Object.keys(obj2).length) {
return false;
}

Expand Down
17 changes: 16 additions & 1 deletion packages/opentelemetry-resources/src/resource-provider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
/*
* 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.
*/
import { Resource } from './Resource';

/**
* A Registry which provides the {@link Resource} to the SDK.
*
*
* Note: this does not do much initially, but eventually will be extended for resource mutation over time.
* */
export class ResourceProvider {
Expand Down
11 changes: 7 additions & 4 deletions packages/opentelemetry-resources/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface Detector {
/**
* Interface for a synchronous Resource Detector. In order to detect attributes asynchronously, a detector
* can pass a Promise as the second parameter to the Resource constructor.
*
*
* @deprecated please use {@link ResourceEntityDetector}
*/
export interface DetectorSync {
Expand All @@ -50,16 +50,19 @@ export interface ResourceEntityDetector {
export type IDetectedResource = {
resourceAttributes: DetectedResourceAttributes;
entities: IDetectedEntity[];
}
};

export type IDetectedEntity = {
type: string;
schema_url?: string;
identifier: Attributes;
attributes?: DetectedResourceAttributes;
}
};

/**
* Represents a set of detected synchronous and asynchronous resource attributes.
*/
export type DetectedResourceAttributes = Record<string, AttributeValue | Promise<AttributeValue>>;
export type DetectedResourceAttributes = Record<
string,
AttributeValue | Promise<AttributeValue>
>;
10 changes: 8 additions & 2 deletions packages/opentelemetry-resources/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
* limitations under the License.
*/

export const isPromiseLike = <R>(val: any): val is PromiseLike<R> => {
export const isPromiseLike = <R>(val: unknown): val is PromiseLike<R> => {
return (
val !== null && typeof val === 'object' && typeof val.then === 'function'
val !== null &&
typeof val === 'object' &&
typeof Object.getOwnPropertyDescriptor(val, 'then')?.value === 'function'
);
};

export function identity<T>(_: T): T {
return _;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Resource, Detector, ResourceDetectionConfig, IResource } from '../../src';
import {
Resource,
Detector,
ResourceDetectionConfig,
IResource,
} from '../../src';
import * as assert from 'assert';

// DO NOT MODIFY THIS DETECTOR: Previous detectors used Resource as IResource did not yet exist.
Expand Down

0 comments on commit 3a05fb1

Please sign in to comment.