From 4553b29d4a04b5b7e4bf87cad64dc2fc8c740d8e Mon Sep 17 00:00:00 2001 From: Ali Sabzevari Date: Wed, 11 Aug 2021 16:41:36 +0200 Subject: [PATCH] chore: Fix lint warnings in core package (#2405) Co-authored-by: Daniel Dyla --- .../propagation/HttpBaggagePropagator.ts | 2 +- .../opentelemetry-core/src/baggage/utils.ts | 26 ++++++++-------- .../src/common/attributes.ts | 1 + .../src/common/global-error-handler.ts | 6 ++-- .../opentelemetry-core/src/common/time.ts | 30 +++++++++---------- .../opentelemetry-core/src/common/types.ts | 4 ++- .../src/propagation/composite.ts | 2 +- .../src/trace/HttpTraceContextPropagator.ts | 2 +- packages/opentelemetry-core/src/utils/wrap.ts | 2 +- 9 files changed, 40 insertions(+), 35 deletions(-) diff --git a/packages/opentelemetry-core/src/baggage/propagation/HttpBaggagePropagator.ts b/packages/opentelemetry-core/src/baggage/propagation/HttpBaggagePropagator.ts index 3cd023c719..521b4c55cd 100644 --- a/packages/opentelemetry-core/src/baggage/propagation/HttpBaggagePropagator.ts +++ b/packages/opentelemetry-core/src/baggage/propagation/HttpBaggagePropagator.ts @@ -43,7 +43,7 @@ import { * https://w3c.github.io/baggage/ */ export class HttpBaggagePropagator implements TextMapPropagator { - inject(context: Context, carrier: unknown, setter: TextMapSetter) { + inject(context: Context, carrier: unknown, setter: TextMapSetter): void { const baggage = propagation.getBaggage(context); if (!baggage || isTracingSuppressed(context)) return; const keyPairs = getKeyPairs(baggage) diff --git a/packages/opentelemetry-core/src/baggage/utils.ts b/packages/opentelemetry-core/src/baggage/utils.ts index 25935cf8f6..6292866cab 100644 --- a/packages/opentelemetry-core/src/baggage/utils.ts +++ b/packages/opentelemetry-core/src/baggage/utils.ts @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Baggage, baggageEntryMetadataFromString } from '@opentelemetry/api'; +import { Baggage, BaggageEntryMetadata, baggageEntryMetadataFromString } from '@opentelemetry/api'; import { BAGGAGE_ITEMS_SEPARATOR, BAGGAGE_PROPERTIES_SEPARATOR, @@ -21,25 +21,26 @@ import { BAGGAGE_MAX_TOTAL_LENGTH, } from './constants'; -export const serializeKeyPairs = (keyPairs: string[]) => { +type ParsedBaggageKeyValue = { key: string, value: string, metadata: BaggageEntryMetadata | undefined }; + +export function serializeKeyPairs(keyPairs: string[]): string { return keyPairs.reduce((hValue: string, current: string) => { - const value = `${hValue}${ - hValue !== '' ? BAGGAGE_ITEMS_SEPARATOR : '' - }${current}`; + const value = `${hValue}${hValue !== '' ? BAGGAGE_ITEMS_SEPARATOR : '' + }${current}`; return value.length > BAGGAGE_MAX_TOTAL_LENGTH ? hValue : value; }, ''); -}; +} -export const getKeyPairs = (baggage: Baggage): string[] => { +export function getKeyPairs(baggage: Baggage): string[] { return baggage .getAllEntries() .map( ([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value.value)}` ); -}; +} -export const parsePairKeyValue = (entry: string) => { +export function parsePairKeyValue(entry: string): ParsedBaggageKeyValue | undefined { const valueProps = entry.split(BAGGAGE_PROPERTIES_SEPARATOR); if (valueProps.length <= 0) return; const keyPairPart = valueProps.shift(); @@ -55,13 +56,13 @@ export const parsePairKeyValue = (entry: string) => { ); } return { key, value, metadata }; -}; +} /** * Parse a string serialized in the baggage HTTP Format (without metadata): * https://github.com/w3c/baggage/blob/master/baggage/HTTP_HEADER_FORMAT.md */ -export const parseKeyPairsIntoRecord = (value?: string) => { +export function parseKeyPairsIntoRecord(value?: string): Record { if (typeof value !== 'string' || value.length === 0) return {}; return value .split(BAGGAGE_ITEMS_SEPARATOR) @@ -70,7 +71,8 @@ export const parseKeyPairsIntoRecord = (value?: string) => { }) .filter(keyPair => keyPair !== undefined && keyPair.value.length > 0) .reduce>((headers, keyPair) => { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion headers[keyPair!.key] = keyPair!.value; return headers; }, {}); -}; +} diff --git a/packages/opentelemetry-core/src/common/attributes.ts b/packages/opentelemetry-core/src/common/attributes.ts index df6b8f7a57..34fe493d9d 100644 --- a/packages/opentelemetry-core/src/common/attributes.ts +++ b/packages/opentelemetry-core/src/common/attributes.ts @@ -22,6 +22,7 @@ export function sanitizeAttributes(attributes: unknown): SpanAttributes { return out; } + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion for (const [k, v] of Object.entries(attributes!)) { if (isAttributeValue(v)) { if (Array.isArray(v)) { diff --git a/packages/opentelemetry-core/src/common/global-error-handler.ts b/packages/opentelemetry-core/src/common/global-error-handler.ts index e8461aba76..9ef103e404 100644 --- a/packages/opentelemetry-core/src/common/global-error-handler.ts +++ b/packages/opentelemetry-core/src/common/global-error-handler.ts @@ -25,7 +25,7 @@ let delegateHandler = loggingErrorHandler(); * Set the global error handler * @param {ErrorHandler} handler */ -export function setGlobalErrorHandler(handler: ErrorHandler) { +export function setGlobalErrorHandler(handler: ErrorHandler): void { delegateHandler = handler; } @@ -33,8 +33,8 @@ export function setGlobalErrorHandler(handler: ErrorHandler) { * Return the global error handler * @param {Exception} ex */ -export const globalErrorHandler = (ex: Exception) => { +export function globalErrorHandler(ex: Exception): void { try { delegateHandler(ex); } catch {} // eslint-disable-line no-empty -}; +} diff --git a/packages/opentelemetry-core/src/common/time.ts b/packages/opentelemetry-core/src/common/time.ts index ce56ab8605..bb7b2bd3d7 100644 --- a/packages/opentelemetry-core/src/common/time.ts +++ b/packages/opentelemetry-core/src/common/time.ts @@ -115,45 +115,45 @@ export function hrTimeDuration( /** * Convert hrTime to timestamp, for example "2019-05-14T17:00:00.000123456Z" - * @param hrTime + * @param time */ -export function hrTimeToTimeStamp(hrTime: api.HrTime): string { +export function hrTimeToTimeStamp(time: api.HrTime): string { const precision = NANOSECOND_DIGITS; - const tmp = `${'0'.repeat(precision)}${hrTime[1]}Z`; + const tmp = `${'0'.repeat(precision)}${time[1]}Z`; const nanoString = tmp.substr(tmp.length - precision - 1); - const date = new Date(hrTime[0] * 1000).toISOString(); + const date = new Date(time[0] * 1000).toISOString(); return date.replace('000Z', nanoString); } /** * Convert hrTime to nanoseconds. - * @param hrTime + * @param time */ -export function hrTimeToNanoseconds(hrTime: api.HrTime): number { - return hrTime[0] * SECOND_TO_NANOSECONDS + hrTime[1]; +export function hrTimeToNanoseconds(time: api.HrTime): number { + return time[0] * SECOND_TO_NANOSECONDS + time[1]; } /** * Convert hrTime to milliseconds. - * @param hrTime + * @param time */ -export function hrTimeToMilliseconds(hrTime: api.HrTime): number { - return Math.round(hrTime[0] * 1e3 + hrTime[1] / 1e6); +export function hrTimeToMilliseconds(time: api.HrTime): number { + return Math.round(time[0] * 1e3 + time[1] / 1e6); } /** * Convert hrTime to microseconds. - * @param hrTime + * @param time */ -export function hrTimeToMicroseconds(hrTime: api.HrTime): number { - return Math.round(hrTime[0] * 1e6 + hrTime[1] / 1e3); +export function hrTimeToMicroseconds(time: api.HrTime): number { + return Math.round(time[0] * 1e6 + time[1] / 1e3); } /** * check if time is HrTime * @param value */ -export function isTimeInputHrTime(value: unknown) { +export function isTimeInputHrTime(value: unknown): value is api.HrTime { return ( Array.isArray(value) && value.length === 2 && @@ -166,7 +166,7 @@ export function isTimeInputHrTime(value: unknown) { * check if input value is a correct types.TimeInput * @param value */ -export function isTimeInput(value: unknown) { +export function isTimeInput(value: unknown): value is api.HrTime | number | Date { return ( isTimeInputHrTime(value) || typeof value === 'number' || diff --git a/packages/opentelemetry-core/src/common/types.ts b/packages/opentelemetry-core/src/common/types.ts index 6d68be37dc..99ab0d08fd 100644 --- a/packages/opentelemetry-core/src/common/types.ts +++ b/packages/opentelemetry-core/src/common/types.ts @@ -31,9 +31,11 @@ export interface TimeOriginLegacy { * This interface defines the params that are be added to the wrapped function * using the "shimmer.wrap" */ -export interface ShimWrapped { +export interface ShimWrapped extends Function { __wrapped: boolean; + // eslint-disable-next-line @typescript-eslint/ban-types __unwrap: Function; + // eslint-disable-next-line @typescript-eslint/ban-types __original: Function; } diff --git a/packages/opentelemetry-core/src/propagation/composite.ts b/packages/opentelemetry-core/src/propagation/composite.ts index 3a02890bed..245507a110 100644 --- a/packages/opentelemetry-core/src/propagation/composite.ts +++ b/packages/opentelemetry-core/src/propagation/composite.ts @@ -64,7 +64,7 @@ export class CompositePropagator implements TextMapPropagator { * @param context Context to inject * @param carrier Carrier into which context will be injected */ - inject(context: Context, carrier: unknown, setter: TextMapSetter) { + inject(context: Context, carrier: unknown, setter: TextMapSetter): void { for (const propagator of this._propagators) { try { propagator.inject(context, carrier, setter); diff --git a/packages/opentelemetry-core/src/trace/HttpTraceContextPropagator.ts b/packages/opentelemetry-core/src/trace/HttpTraceContextPropagator.ts index 53d3bf11fb..b41d99fbf9 100644 --- a/packages/opentelemetry-core/src/trace/HttpTraceContextPropagator.ts +++ b/packages/opentelemetry-core/src/trace/HttpTraceContextPropagator.ts @@ -71,7 +71,7 @@ export function parseTraceParent(traceParent: string): SpanContext | null { * https://www.w3.org/TR/trace-context/ */ export class HttpTraceContextPropagator implements TextMapPropagator { - inject(context: Context, carrier: unknown, setter: TextMapSetter) { + inject(context: Context, carrier: unknown, setter: TextMapSetter): void { const spanContext = trace.getSpanContext(context); if ( !spanContext || diff --git a/packages/opentelemetry-core/src/utils/wrap.ts b/packages/opentelemetry-core/src/utils/wrap.ts index b566a78bbf..a32da96463 100644 --- a/packages/opentelemetry-core/src/utils/wrap.ts +++ b/packages/opentelemetry-core/src/utils/wrap.ts @@ -20,7 +20,7 @@ import { ShimWrapped } from '../common/types'; * Checks if certain function has been already wrapped * @param func */ -export function isWrapped(func: any) { +export function isWrapped(func: unknown): func is ShimWrapped { return ( typeof func === 'function' && typeof (func as ShimWrapped).__original === 'function' &&