Skip to content

[FFM-1266] metrics processor done #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ servers:
description: no host specified
- url: 'http://localhost:3000/api/1.0'
description: CfClient description
tags:
- name: client
- name: metrics
paths:
'/client/env/{environmentUUID}/feature-configs':
get:
summary: Get all feature flags activations
description: All feature flags with activations in project environment
operationId: GetFeatureConfig
tags:
- client
parameters:
- name: environmentUUID
in: path
Expand All @@ -39,6 +44,8 @@ paths:
get:
summary: Get feature config
operationId: GetFeatureConfigByIdentifier
tags:
- client
parameters:
- name: identifier
in: path
Expand Down Expand Up @@ -66,6 +73,8 @@ paths:
summary: Retrieve all segments.
description: Used to retrieve all segments for certain account id.
operationId: GetAllSegments
tags:
- client
parameters:
- name: environmentUUID
in: path
Expand Down Expand Up @@ -97,6 +106,8 @@ paths:
summary: Retrieve a segment by identifier
description: Used to retrieve a segment for a certain account id by identifier
operationId: GetSegmentByIdentifier
tags:
- client
parameters:
- name: identifier
in: path
Expand Down Expand Up @@ -132,6 +143,8 @@ paths:
summary: Authenticate with the admin server.
description: Used to retrieve all target segments for certain account id.
operationId: Authenticate
tags:
- client
requestBody:
content:
application/json:
Expand All @@ -156,6 +169,8 @@ paths:
get:
summary: Get feature evaluations for target
operationId: GetEvaluations
tags:
- client
parameters:
- name: environmentUUID
in: path
Expand Down Expand Up @@ -187,6 +202,8 @@ paths:
get:
summary: Get feature evaluations for target
operationId: GetEvaluationByIdentifier
tags:
- client
parameters:
- name: environmentUUID
in: path
Expand Down Expand Up @@ -215,10 +232,37 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Evaluation'
'/metrics/{environment}':
post:
tags:
- metrics
summary: Send metrics to the Analytics server.
description: Send metrics to Analytics server
operationId: postMetrics
parameters:
- $ref: '#/components/parameters/environmentPathParam'
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Metrics'
security:
- BearerAuth: []
responses:
'200':
description: OK
'401':
$ref: '#/components/responses/Unauthenticated'
'403':
$ref: '#/components/responses/Unauthorized'
'500':
$ref: '#/components/responses/InternalServerError'
/stream:
get:
summary: Stream endpoint.
operationId: Stream
tags:
- client
parameters:
- in: header
name: API-Key
Expand Down Expand Up @@ -572,6 +616,7 @@ components:
- itemCount
- pageSize
- pageIndex
- pageIndex
Evaluation:
type: object
properties:
Expand All @@ -591,6 +636,66 @@ components:
type: array
items:
$ref: '#/components/schemas/Evaluation'
KeyValue:
type: object
properties:
key:
type: string
value:
type: string
required:
- key
- value
TargetData:
type: object
properties:
identifier:
type: string
name:
type: string
attributes:
type: array
items:
$ref: '#/components/schemas/KeyValue'
required:
- name
- identifier
- attributes
MetricsData:
type: object
properties:
timestamp:
type: integer
format: int64
example: 1608175465
description: time at when this data was recorded
count:
type: integer
metricsType:
type: string
enum:
- FFMETRICS
description: This can be of type FeatureMetrics
attributes:
type: array
items:
$ref: '#/components/schemas/KeyValue'
required:
- attributes
- count
- timestamp
- metricsType
Metrics:
type: object
properties:
targetData:
type: array
items:
$ref: '#/components/schemas/TargetData'
metricsData:
type: array
items:
$ref: '#/components/schemas/MetricsData'
securitySchemes:
BearerAuth:
type: http
Expand Down Expand Up @@ -621,3 +726,11 @@ components:
application/json:
schema:
$ref: '#/components/schemas/Error'
parameters:
environmentPathParam:
name: environment
in: path
required: true
description: environment parameter in query.
schema:
type: string
20 changes: 17 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { SimpleCache } from "./cache";
import { ConsoleLog } from "./log";
import { Options } from "./types";
import { SimpleCache } from './cache';
import { ConsoleLog } from './log';
import { Options } from './types';

export const ONE_HUNDRED = 100;

export const FF_METRIC_TYPE = 'FFMETRICS',
FEATURE_IDENTIFIER_ATTRIBUTE = 'featureIdentifier',
FEATURE_NAME_ATTRIBUTE = 'featureName',
VARIATION_IDENTIFIER_ATTRIBUTE = 'variationIdentifier',
VARIATION_VALUE_ATTRIBUTE = 'featureValue',
TARGET_ATTRIBUTE = 'target',
SDK_VERSION_ATTRIBUTE = 'SDK_VERSION',
SDK_VERSION = '1.0.0',
SDK_TYPE_ATTRIBUTE = 'SDK_TYPE',
SDK_TYPE = 'server',
SDK_LANGUAGE_ATTRIBUTE = 'SDK_LANGUAGE',
SDK_LANGUAGE = 'python',
GLOBAL_TARGET = 'global';

export const SEGMENT_MATCH_OPERATOR = 'segmentMatch',
IN_OPERATOR = 'in',
EQUAL_OPERATOR = 'equal',
Expand Down
8 changes: 4 additions & 4 deletions src/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export class Evaluator {
defaultValue = false,
): boolean {
const fc = this.query.getFlag(identifier);
if (fc.kind !== FeatureConfigKindEnum.Boolean) {
if (!fc || fc.kind !== FeatureConfigKindEnum.Boolean) {
return defaultValue;
}

Expand All @@ -290,7 +290,7 @@ export class Evaluator {
defaultValue = '',
): string {
const fc = this.query.getFlag(identifier);
if (fc.kind !== FeatureConfigKindEnum.String) {
if (!fc || fc.kind !== FeatureConfigKindEnum.String) {
return defaultValue;
}

Expand All @@ -308,7 +308,7 @@ export class Evaluator {
defaultValue = 0,
): number {
const fc = this.query.getFlag(identifier);
if (fc.kind !== FeatureConfigKindEnum.Int) {
if (!fc || fc.kind !== FeatureConfigKindEnum.Int) {
return defaultValue;
}

Expand All @@ -326,7 +326,7 @@ export class Evaluator {
defaultValue = {},
): Record<string, unknown> {
const fc = this.query.getFlag(identifier);
if (fc.kind !== FeatureConfigKindEnum.Json) {
if (!fc || fc.kind !== FeatureConfigKindEnum.Json) {
return defaultValue;
}

Expand Down
34 changes: 24 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import jwt_decode from 'jwt-decode';
import { Claims, Options, Target } from './types';
import { Configuration, DefaultApi } from './openapi';
import { Configuration, ClientApi } from './openapi';
import { VERSION } from './version';
import { PollingProcessor } from './polling';
import { StreamProcessor } from './streaming';
import * as events from 'events';
import { Evaluator } from './evaluator';
import { defaultOptions } from './constants';
import { Repository, StorageRepository } from './repository';
import { MetricsProcessor, MetricsProcessorInterface } from './metrics';

const log = defaultOptions.logger;
export class CfClient {
private evaluator: Evaluator;
private repository: Repository;
private api: DefaultApi;
private api: ClientApi;
private sdkKey: string;
private authToken: string;
private environment: string;
Expand All @@ -23,13 +24,19 @@ export class CfClient {
private eventBus = new events.EventEmitter();
private pollProcessor: PollingProcessor;
private streamProcessor: StreamProcessor;
private metricsProcessor: MetricsProcessorInterface;

constructor(sdkKey: string, options: Options = {}) {
this.sdkKey = sdkKey;
this.options = { ...defaultOptions, ...options };

this.configuration = new Configuration({
basePath: this.options.baseUrl,
baseOptions: {
headers: {
'User-Agent': `NodeJsSDK/${VERSION}`,
},
},
});

this.repository = new StorageRepository(
Expand All @@ -38,7 +45,7 @@ export class CfClient {
);
this.evaluator = new Evaluator(this.repository);

this.api = new DefaultApi(this.configuration);
this.api = new ClientApi(this.configuration);
this.run();
}

Expand All @@ -53,11 +60,6 @@ export class CfClient {

this.environment = decoded.environment;
this.cluster = decoded.clusterIdentifier || '1';
this.configuration.baseOptions = {
headers: {
'User-Agent': `NodeJsSDK/${VERSION}`,
},
};
return this.authToken;
}

Expand All @@ -71,9 +73,8 @@ export class CfClient {
this.eventBus,
this.repository,
);

// start processors
this.pollProcessor.start();

if (this.options.enableStream) {
this.streamProcessor = new StreamProcessor(
this.api,
Expand All @@ -89,6 +90,16 @@ export class CfClient {
this.streamProcessor.start();
}

if (this.options.enableAnalytics) {
this.metricsProcessor = MetricsProcessor(
this.environment,
this.cluster,
this.configuration,
this.options,
);
this.metricsProcessor.start();
}

log.info('finished setting up processors');
}

Expand Down Expand Up @@ -129,5 +140,8 @@ export class CfClient {
if (this.options.enableStream) {
this.streamProcessor.close();
}
if (this.options.enableAnalytics) {
this.metricsProcessor.close();
}
}
}
Loading