Skip to content

Commit

Permalink
Convert TelemetryCounterType from enum to type
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo committed Jun 29, 2022
1 parent 24e6d42 commit 16dcd7e
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { loggerMock } from '@kbn/logging-mocks';
import { AnalyticsClient } from './analytics_client';
import { shippersMock } from '../shippers/mocks';
import type { TelemetryCounter } from '../events';
import { TelemetryCounterType } from '../events';
import { ContextService } from './context_service';

describe('AnalyticsClient', () => {
Expand Down Expand Up @@ -288,7 +287,7 @@ describe('AnalyticsClient', () => {
const counterEventPromise = lastValueFrom(analyticsClient.telemetryCounter$.pipe(take(1)));

const counter: TelemetryCounter = {
type: TelemetryCounterType.succeeded,
type: 'succeeded',
source: 'a random value',
event_type: 'eventTypeA',
code: '200',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import type {
ShipperClassConstructor,
} from './types';
import type { Event, EventContext, EventType, TelemetryCounter } from '../events';
import { TelemetryCounterType } from '../events';
import { ShippersRegistry } from './shippers_registry';
import { OptInConfigService } from './opt_in_config';
import { ContextService } from './context_service';
Expand Down Expand Up @@ -97,7 +96,7 @@ export class AnalyticsClient implements IAnalyticsClient {
const timestamp = new Date().toISOString();

this.internalTelemetryCounter$.next({
type: TelemetryCounterType.enqueued,
type: 'enqueued',
source: 'client',
event_type: eventType,
code: 'enqueued',
Expand All @@ -107,7 +106,7 @@ export class AnalyticsClient implements IAnalyticsClient {
const eventTypeOpts = this.eventTypeRegistry.get(eventType);
if (!eventTypeOpts) {
this.internalTelemetryCounter$.next({
type: TelemetryCounterType.dropped,
type: 'dropped',
source: 'client',
event_type: eventType,
code: 'UnregisteredType',
Expand Down Expand Up @@ -276,7 +275,7 @@ export class AnalyticsClient implements IAnalyticsClient {
});
if (sentToShipper) {
this.internalTelemetryCounter$.next({
type: TelemetryCounterType.sent_to_shipper,
type: 'sent_to_shipper',
source: 'client',
event_type: eventType,
code: 'OK',
Expand Down
9 changes: 7 additions & 2 deletions packages/analytics/client/src/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
* Side Public License, v 1.
*/

export type { Event, EventType, EventContext, TelemetryCounter } from './types';
export { TelemetryCounterType } from './types';
export type {
Event,
EventType,
EventContext,
TelemetryCounter,
TelemetryCounterType,
} from './types';
42 changes: 14 additions & 28 deletions packages/analytics/client/src/events/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,42 +65,28 @@ export interface EventContext {
export type EventType = string;

/**
* Types of the Telemetry Counter: It allows to differentiate what happened to the events
* Indicates if the event contains data about succeeded, failed or dropped events:
* - enqueued: The event was accepted and will be sent to the shippers when they become available (and opt-in === true).
* - sent_to_shipper: The event was sent to at least one shipper.
* - succeeded: The event was successfully sent by the shipper.
* - failed: There was an error when processing/shipping the event. Refer to the Telemetry Counter's code for the reason.
* - dropped: The event was dropped from the queue. Refer to the Telemetry Counter's code for the reason.
*/
export enum TelemetryCounterType {
/**
* The event was accepted and will be sent to the shippers when they become available (and opt-in === true).
*/
enqueued = 'enqueued',
/**
* The event was sent to at least one shipper.
*/
sent_to_shipper = 'sent_to_shipper',
/**
* The event was successfully sent by the shipper.
*/
succeeded = 'succeeded',
/**
* There was an error when processing/shipping the event.
* Refer to the Telemetry Counter's code for the reason.
*/
failed = 'failed',
/**
* The event was dropped from the queue.
* Refer to the Telemetry Counter's code for the reason.
*/
dropped = 'dropped',
}
export type TelemetryCounterType =
| 'enqueued'
| 'sent_to_shipper'
| 'succeeded'
| 'failed'
| 'dropped';

/**
* Shape of the events emitted by the telemetryCounter$ observable
*/
export interface TelemetryCounter {
/**
* Indicates if the event contains data about succeeded, failed or dropped events.
* Refer to {@link TelemetryCounterType} for more details about each possible value.
* {@link TelemetryCounterType}
*/
type: keyof typeof TelemetryCounterType;
type: TelemetryCounterType;
/**
* Who emitted the event? It can be "client" or the name of the shipper.
*/
Expand Down
9 changes: 7 additions & 2 deletions packages/analytics/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ export type {
EventTypeOpts,
} from './analytics_client';

export type { Event, EventContext, EventType, TelemetryCounter } from './events';
export { TelemetryCounterType } from './events';
export type {
Event,
EventContext,
EventType,
TelemetryCounter,
TelemetryCounterType,
} from './events';

export type {
RootSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function createTelemetryCounterHelper(
code,
error,
}: {
type?: keyof typeof TelemetryCounterType;
type?: TelemetryCounterType;
code?: string;
error?: Error;
} = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import type {
IShipper,
TelemetryCounter,
} from '@kbn/analytics-client';
import { TelemetryCounterType } from '@kbn/analytics-client';
import type { ElasticV3ShipperOptions } from '@kbn/analytics-shippers-elastic-v3-common';
import {
buildHeaders,
Expand Down Expand Up @@ -142,7 +141,7 @@ export class ElasticV3ServerShipper implements IShipper {
const toDrop = events.length - freeSpace;
const droppedEvents = events.splice(-toDrop, toDrop);
this.reportTelemetryCounters(droppedEvents, {
type: TelemetryCounterType.dropped,
type: 'dropped',
code: 'queue_full',
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ export type {
OptInConfig,
ContextProviderOpts,
TelemetryCounter,
TelemetryCounterType,
} from '@kbn/analytics-client';
export { TelemetryCounterType } from '@kbn/analytics-client';

export { AppNavLinkStatus, AppStatus, ScopedHistory } from './application';
export type {
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ export type {
OptInConfig,
ShipperClassConstructor,
TelemetryCounter,
TelemetryCounterType,
} from '@kbn/analytics-client';
export { TelemetryCounterType } from '@kbn/analytics-client';
export type {
AnalyticsServiceSetup,
AnalyticsServicePreboot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import { Subject } from 'rxjs';
import { TelemetryCounterType } from '@kbn/analytics-client';
import type { Event, EventContext, IShipper, TelemetryCounter } from '@kbn/core/public';

export interface Action {
Expand All @@ -26,7 +25,7 @@ export class CustomShipper implements IShipper {
this.actions$.next({ action: 'reportEvents', meta: events });
events.forEach((event) => {
this.telemetryCounter$.next({
type: TelemetryCounterType.succeeded,
type: `succeeded`,
event_type: event.event_type,
code: '200',
count: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import { Subject } from 'rxjs';
import { TelemetryCounterType } from '@kbn/analytics-client';
import type { IShipper, Event, EventContext, TelemetryCounter } from '@kbn/core/server';

export interface Action {
Expand All @@ -26,7 +25,7 @@ export class CustomShipper implements IShipper {
this.actions$.next({ action: 'reportEvents', meta: events });
events.forEach((event) => {
this.telemetryCounter$.next({
type: TelemetryCounterType.succeeded,
type: `succeeded`,
event_type: event.event_type,
code: '200',
count: 1,
Expand Down

0 comments on commit 16dcd7e

Please sign in to comment.