Skip to content

Commit

Permalink
Network view not experimental (#3124)
Browse files Browse the repository at this point in the history
## About the changes
Promoted experimental networkView flag into a configuration that relies
on prometheusApi being configured.

Also, a follow-up on #3054 moving
this code to enterprise because it doesn't make sense to maintain this
code in OSS where it's not being used.
  • Loading branch information
Gastón Fournier committed Feb 15, 2023
1 parent 58f2f83 commit f8d3085
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 56 deletions.
4 changes: 2 additions & 2 deletions frontend/src/component/admin/menu/AdminMenu.tsx
Expand Up @@ -15,7 +15,7 @@ function AdminMenu() {
const { uiConfig, isEnterprise } = useUiConfig();
const { pathname } = useLocation();
const { isBilling } = useInstanceStatus();
const { flags } = uiConfig;
const { flags, networkViewEnabled } = uiConfig;

const activeTab = pathname.split('/')[2];

Expand Down Expand Up @@ -99,7 +99,7 @@ function AdminMenu() {
</CenteredNavLink>
}
/>
{flags.networkView && (
{networkViewEnabled && (
<Tab
value="network"
label={
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/component/menu/routes.ts
Expand Up @@ -474,7 +474,7 @@ export const adminMenuRoutes: INavigationMenuItem[] = [
path: '/admin/network/*',
title: 'Network',
menu: { adminSettings: true },
flag: 'networkView',
configFlag: 'networkViewEnabled',
},
{
path: '/admin/maintenance',
Expand Down
1 change: 1 addition & 0 deletions frontend/src/hooks/api/getters/useUiConfig/defaultValue.ts
Expand Up @@ -28,4 +28,5 @@ export const defaultValue: IUiConfig = {
title: 'Source code on GitHub',
},
],
networkViewEnabled: false,
};
2 changes: 1 addition & 1 deletion frontend/src/interfaces/uiConfig.ts
Expand Up @@ -13,6 +13,7 @@ export interface IUiConfig {
links: ILinks[];
disablePasswordAuth?: boolean;
emailEnabled?: boolean;
networkViewEnabled: boolean;

maintenanceMode?: boolean;
toast?: IProclamationToast;
Expand All @@ -38,7 +39,6 @@ export interface IFlags {
UG?: boolean;
ENABLE_DARK_MODE_SUPPORT?: boolean;
embedProxyFrontend?: boolean;
networkView?: boolean;
maintenance?: boolean;
maintenanceMode?: boolean;
messageBanner?: boolean;
Expand Down
2 changes: 0 additions & 2 deletions src/lib/__snapshots__/create-config.test.ts.snap
Expand Up @@ -77,7 +77,6 @@ exports[`should create default config 1`] = `
"maintenance": false,
"maintenanceMode": false,
"messageBanner": false,
"networkView": false,
"newProjectOverview": false,
"projectStatusApi": false,
"proxyReturnAllToggles": false,
Expand All @@ -98,7 +97,6 @@ exports[`should create default config 1`] = `
"maintenance": false,
"maintenanceMode": false,
"messageBanner": false,
"networkView": false,
"newProjectOverview": false,
"projectStatusApi": false,
"proxyReturnAllToggles": false,
Expand Down
3 changes: 3 additions & 0 deletions src/lib/openapi/spec/ui-config-schema.ts
Expand Up @@ -40,6 +40,9 @@ export const uiConfigSchema = {
strategySegmentsLimit: {
type: 'number',
},
networkViewEnabled: {
type: 'boolean',
},
frontendApiOrigins: {
type: 'array',
items: {
Expand Down
1 change: 1 addition & 0 deletions src/lib/routes/admin-api/config.ts
Expand Up @@ -133,6 +133,7 @@ class ConfigController extends Controller {
strategySegmentsLimit: this.config.strategySegmentsLimit,
frontendApiOrigins: frontendSettings.frontendApiOrigins,
versionInfo: this.versionService.getVersionInfo(),
networkViewEnabled: this.config.prometheusApi !== undefined,
disablePasswordAuth,
maintenanceMode,
};
Expand Down
48 changes: 2 additions & 46 deletions src/lib/services/client-metrics/instance-service.ts
Expand Up @@ -2,7 +2,7 @@ import { applicationSchema } from './schema';
import { APPLICATION_CREATED, CLIENT_REGISTER } from '../../types/events';
import { IApplication } from './models';
import { IUnleashStores } from '../../types/stores';
import { IServerOption, IUnleashConfig } from '../../types/option';
import { IUnleashConfig } from '../../types/option';
import { IEventStore } from '../../types/stores/event-store';
import {
IClientApplication,
Expand All @@ -19,7 +19,6 @@ import { minutesToMilliseconds, secondsToMilliseconds } from 'date-fns';
import { IClientMetricsStoreV2 } from '../../types/stores/client-metrics-store-v2';
import { clientMetricsSchema } from './schema';
import { PartialSome } from '../../types/partial';
import fetch from 'make-fetch-happen';

export default class ClientInstanceService {
apps = {};
Expand All @@ -46,10 +45,6 @@ export default class ClientInstanceService {

private announcementInterval: number;

private serverOption: IServerOption;

readonly prometheusApi;

constructor(
{
clientMetricsStoreV2,
Expand All @@ -67,11 +62,7 @@ export default class ClientInstanceService {
| 'clientInstanceStore'
| 'eventStore'
>,
{
getLogger,
prometheusApi,
server,
}: Pick<IUnleashConfig, 'getLogger' | 'prometheusApi' | 'server'>,
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
bulkInterval = secondsToMilliseconds(5),
announcementInterval = minutesToMilliseconds(5),
) {
Expand All @@ -81,8 +72,6 @@ export default class ClientInstanceService {
this.clientApplicationsStore = clientApplicationsStore;
this.clientInstanceStore = clientInstanceStore;
this.eventStore = eventStore;
this.prometheusApi = prometheusApi;
this.serverOption = server;
this.logger = getLogger(
'/services/client-metrics/client-instance-service.ts',
);
Expand Down Expand Up @@ -220,39 +209,6 @@ export default class ClientInstanceService {
await this.clientApplicationsStore.upsert(applicationData);
}

private toEpoch(d: Date) {
return (d.getTime() - d.getMilliseconds()) / 1000;
}

async getRPS(hoursToQuery: number, limit = 10): Promise<any> {
if (!this.prometheusApi) {
this.logger.warn('Prometheus not configured');
return;
}
const timeoutSeconds = 5;
const basePath = this.serverOption.baseUriPath.replace(/\/$/, '');
const pathQuery = `${basePath}/api/.*`;
const step = '5m';
const rpsQuery = `topk(${limit}, irate (http_request_duration_milliseconds_count{path=~"${pathQuery}"} [${step}]))`;
const query = `sum by(appName, endpoint) (label_replace(${rpsQuery}, "endpoint", "$1", "path", "${basePath}(/api/(?:client/)?[^/\*]*).*"))`;
const end = new Date();
const start = new Date();
start.setHours(end.getHours() - hoursToQuery);

const params = `timeout=${timeoutSeconds}s&start=${this.toEpoch(
start,
)}&end=${this.toEpoch(end)}&step=${step}&query=${encodeURI(query)}`;
const url = `${this.prometheusApi}/api/v1/query_range?${params}`;
let metrics;
const response = await fetch(url);
if (response.ok) {
metrics = await response.json();
} else {
throw new Error(response.statusText);
}
return metrics;
}

async removeInstancesOlderThanTwoDays(): Promise<void> {
return this.clientInstanceStore.removeInstancesOlderThanTwoDays();
}
Expand Down
4 changes: 0 additions & 4 deletions src/lib/types/experimental.ts
Expand Up @@ -30,10 +30,6 @@ const flags = {
process.env.UNLEASH_EXPERIMENTAL_PROXY_RETURN_ALL_TOGGLES,
false,
),
networkView: parseEnvVarBoolean(
process.env.UNLEASH_EXPERIMENTAL_NETWORK_VIEW,
false,
),
maintenance: parseEnvVarBoolean(
process.env.UNLEASH_EXPERIMENTAL_MAINTENANCE,
false,
Expand Down
Expand Up @@ -3705,6 +3705,9 @@ Stats are divided into current and previous **windows**.
"name": {
"type": "string",
},
"networkViewEnabled": {
"type": "boolean",
},
"segmentValuesLimit": {
"type": "number",
},
Expand Down

0 comments on commit f8d3085

Please sign in to comment.