Skip to content

Commit

Permalink
Configure whether mixpanel is enabled with env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Collins authored and Chris Collins committed Apr 27, 2022
1 parent b811254 commit 19b0bb8
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.linkedin.datahub.graphql.generated.RecommendationContent;
import com.linkedin.datahub.graphql.generated.SearchAcrossLineageResult;
import com.linkedin.datahub.graphql.generated.SearchResult;
import com.linkedin.datahub.graphql.generated.TrackingConfiguration;
import com.linkedin.datahub.graphql.generated.UsageQueryResult;
import com.linkedin.datahub.graphql.generated.UserUsageCounts;
import com.linkedin.datahub.graphql.generated.VisualConfiguration;
Expand Down Expand Up @@ -225,6 +226,7 @@ public class GmsGraphQLEngine {
private final AuthenticationConfiguration authenticationConfiguration;
private final AuthorizationConfiguration authorizationConfiguration;
private final VisualConfiguration visualConfiguration;
private final TrackingConfiguration trackingConfiguration;

private final DatasetType datasetType;
private final CorpUserType corpUserType;
Expand Down Expand Up @@ -292,6 +294,7 @@ public GmsGraphQLEngine() {
null,
null,
false,
null,
null);
}

Expand All @@ -311,7 +314,8 @@ public GmsGraphQLEngine(
final AuthorizationConfiguration authorizationConfiguration,
final GitVersion gitVersion,
final boolean supportsImpactAnalysis,
final VisualConfiguration visualConfiguration
final VisualConfiguration visualConfiguration,
final TrackingConfiguration trackingConfiguration
) {

this.entityClient = entityClient;
Expand All @@ -332,6 +336,7 @@ public GmsGraphQLEngine(
this.authenticationConfiguration = Objects.requireNonNull(authenticationConfiguration);
this.authorizationConfiguration = Objects.requireNonNull(authorizationConfiguration);
this.visualConfiguration = visualConfiguration;
this.trackingConfiguration = trackingConfiguration;

this.datasetType = new DatasetType(entityClient);
this.corpUserType = new CorpUserType(entityClient);
Expand Down Expand Up @@ -569,7 +574,7 @@ private void configureQueryResolvers(final RuntimeWiring.Builder builder) {
this.ingestionConfiguration,
this.authenticationConfiguration,
this.authorizationConfiguration,
supportsImpactAnalysis, this.visualConfiguration))
supportsImpactAnalysis, this.visualConfiguration, this.trackingConfiguration))
.dataFetcher("me", new AuthenticatedResolver<>(
new MeResolver(this.entityClient)))
.dataFetcher("search", new AuthenticatedResolver<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.linkedin.datahub.graphql.generated.PoliciesConfig;
import com.linkedin.datahub.graphql.generated.Privilege;
import com.linkedin.datahub.graphql.generated.ResourcePrivileges;
import com.linkedin.datahub.graphql.generated.TrackingConfiguration;
import com.linkedin.datahub.graphql.generated.VisualConfiguration;
import com.linkedin.metadata.config.IngestionConfiguration;
import com.linkedin.metadata.version.GitVersion;
Expand All @@ -34,6 +35,7 @@ public class AppConfigResolver implements DataFetcher<CompletableFuture<AppConfi
private final AuthorizationConfiguration _authorizationConfiguration;
private final boolean _supportsImpactAnalysis;
private final VisualConfiguration _visualConfiguration;
private final TrackingConfiguration _trackingConfiguration;

public AppConfigResolver(
final GitVersion gitVersion,
Expand All @@ -42,14 +44,16 @@ public AppConfigResolver(
final AuthenticationConfiguration authenticationConfiguration,
final AuthorizationConfiguration authorizationConfiguration,
final boolean supportsImpactAnalysis,
final VisualConfiguration visualConfiguration) {
final VisualConfiguration visualConfiguration,
final TrackingConfiguration trackingConfiguration) {
_gitVersion = gitVersion;
_isAnalyticsEnabled = isAnalyticsEnabled;
_ingestionConfiguration = ingestionConfiguration;
_authenticationConfiguration = authenticationConfiguration;
_authorizationConfiguration = authorizationConfiguration;
_supportsImpactAnalysis = supportsImpactAnalysis;
_visualConfiguration = visualConfiguration;
_trackingConfiguration = trackingConfiguration;
}

@Override
Expand Down Expand Up @@ -97,6 +101,7 @@ public CompletableFuture<AppConfig> get(final DataFetchingEnvironment environmen
appConfig.setManagedIngestionConfig(ingestionConfig);
appConfig.setAuthConfig(authConfig);
appConfig.setVisualConfig(_visualConfiguration);
appConfig.setTrackingConfig(_trackingConfiguration);

return CompletableFuture.completedFuture(appConfig);
}
Expand Down
15 changes: 15 additions & 0 deletions datahub-graphql-core/src/main/resources/app.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ type AppConfig {
Configurations related to visual appearance, allows styling the UI without rebuilding the bundle
"""
visualConfig: VisualConfiguration!

"""
Configurations related to tracking users in the app
"""
trackingConfig: TrackingConfiguration!
}

"""
Expand All @@ -124,6 +129,16 @@ type VisualConfiguration {
logoUrl: String
}

"""
Configurations related to tracking users in the app
"""
type TrackingConfiguration {
"""
Env variable for whether or not third party logging should be enabled for this instance
"""
isThirdPartyLoggingEnabled: Boolean
}

"""
Configurations related to Lineage
"""
Expand Down
15 changes: 14 additions & 1 deletion datahub-web-react/src/AppConfigProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react';
import React, { useEffect } from 'react';
import './App.less';
import { THIRD_PARTY_LOGGING_KEY } from './app/analytics/analytics';
import { checkAuthStatus } from './app/auth/checkAuthStatus';
import { AppConfigContext, DEFAULT_APP_CONFIG } from './appConfigContext';
import { useAppConfigQuery } from './graphql/app.generated';

Expand All @@ -10,6 +12,17 @@ const AppConfigProvider = ({ children }: { children: React.ReactNode }) => {
refetch();
};

useEffect(() => {
if (appConfigData && appConfigData.appConfig) {
if (appConfigData.appConfig.trackingConfig.isThirdPartyLoggingEnabled) {
localStorage.setItem(THIRD_PARTY_LOGGING_KEY, 'true');
checkAuthStatus(); // identify in analyitcs once we receive config response
} else {
localStorage.setItem(THIRD_PARTY_LOGGING_KEY, 'false');
}
}
}, [appConfigData]);

return (
<AppConfigContext.Provider
value={{ config: appConfigData?.appConfig || DEFAULT_APP_CONFIG, refreshContext: refreshAppConfig }}
Expand Down
29 changes: 29 additions & 0 deletions datahub-web-react/src/app/analytics/__tests__/analytics.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getMergedTrackingOptions, THIRD_PARTY_LOGGING_KEY } from '../analytics';

describe('getMergedTrackingOptions', () => {
it('should update or create an options object with mixpanel set to the value of what is in localStorage', () => {
// before anything is set in localStorage
let trackingOptions = getMergedTrackingOptions();
expect(trackingOptions).toMatchObject({
plugins: {
mixpanel: false,
},
});

localStorage.setItem(THIRD_PARTY_LOGGING_KEY, 'false');
trackingOptions = getMergedTrackingOptions();
expect(trackingOptions).toMatchObject({
plugins: {
mixpanel: false,
},
});

localStorage.setItem(THIRD_PARTY_LOGGING_KEY, 'true');
trackingOptions = getMergedTrackingOptions();
expect(trackingOptions).toMatchObject({
plugins: {
mixpanel: true,
},
});
});
});
22 changes: 19 additions & 3 deletions datahub-web-react/src/app/analytics/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ import { getBrowserId } from '../browserId';

const appName = 'datahub-react';

export const THIRD_PARTY_LOGGING_KEY = 'isThirdPartyLoggingEnabled';

const analytics = Analytics({
app: appName,
plugins: plugins.filter((plugin) => plugin.isEnabled).map((plugin) => plugin.plugin),
});

const { NODE_ENV } = process.env;

export function getMergedTrackingOptions(options?: any) {
return {
...options,
plugins: {
mixpanel: JSON.parse(localStorage.getItem(THIRD_PARTY_LOGGING_KEY) || 'false'),
},
};
}

export default {
...analytics,
page: (data?: PageData, options?: any, callback?: (...params: any[]) => any) => {
const modifiedData = {
...data,
Expand All @@ -29,7 +39,8 @@ export default {
if (NODE_ENV === 'test') {
return null;
}
return analytics.page(modifiedData, options, callback);
const trackingOptions = getMergedTrackingOptions(options);
return analytics.page(modifiedData, trackingOptions, callback);
},
event: (event: Event, options?: any, callback?: (...params: any[]) => any): Promise<any> => {
const eventTypeName = EventType[event.type];
Expand All @@ -45,6 +56,11 @@ export default {
if (NODE_ENV === 'test') {
return Promise.resolve();
}
return analytics.track(eventTypeName, modifiedEvent, options, callback);
const trackingOptions = getMergedTrackingOptions(options);
return analytics.track(eventTypeName, modifiedEvent, trackingOptions, callback);
},
identify: (userId: string, traits?: any, options?: any, callback?: ((...params: any[]) => any) | undefined) => {
const trackingOptions = getMergedTrackingOptions(options);
return analytics.identify(userId, traits, trackingOptions, callback);
},
};
3 changes: 3 additions & 0 deletions datahub-web-react/src/appConfigContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export const DEFAULT_APP_CONFIG = {
authConfig: {
tokenAuthEnabled: false,
},
trackingConfig: {
isThirdPartyLoggingEnabled: false,
},
};

export const AppConfigContext = React.createContext<{
Expand Down
3 changes: 3 additions & 0 deletions datahub-web-react/src/graphql/app.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ query appConfig {
visualConfig {
logoUrl
}
trackingConfig {
isThirdPartyLoggingEnabled
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.linkedin.gms.factory.common;

import com.linkedin.datahub.graphql.generated.TrackingConfiguration;
import javax.annotation.Nonnull;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class TrackingConfigFactory {
@Value("${trackingConfig.isThirdPartyLoggingEnabled}")
private Boolean isThirdPartyLoggingEnabled;

@Nonnull
@Bean(name = "trackingConfig")
protected TrackingConfiguration getInstance() {
TrackingConfiguration config = new TrackingConfiguration();
config.setIsThirdPartyLoggingEnabled(isThirdPartyLoggingEnabled);

return config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.linkedin.datahub.graphql.GraphQLEngine;
import com.linkedin.datahub.graphql.analytics.service.AnalyticsService;
import com.linkedin.datahub.graphql.generated.VisualConfiguration;
import com.linkedin.datahub.graphql.generated.TrackingConfiguration;
import com.linkedin.entity.client.JavaEntityClient;
import com.linkedin.gms.factory.auth.DataHubTokenServiceFactory;
import com.linkedin.gms.factory.common.GitVersionFactory;
Expand Down Expand Up @@ -97,6 +98,10 @@ public class GraphQLEngineFactory {
@Qualifier("visualConfig")
private VisualConfiguration _visualConfiguration;

@Autowired
@Qualifier("trackingConfig")
private TrackingConfiguration _trackingConfiguration;

@Value("${platformAnalytics.enabled}") // TODO: Migrate to DATAHUB_ANALYTICS_ENABLED
private Boolean isAnalyticsEnabled;

Expand All @@ -120,7 +125,8 @@ protected GraphQLEngine getInstance() {
_configProvider.getAuthorization(),
_gitVersion,
_graphService.supportsMultiHop(),
_visualConfiguration
_visualConfiguration,
_trackingConfiguration
).builder().build();
}
return new GmsGraphQLEngine(
Expand All @@ -139,7 +145,8 @@ protected GraphQLEngine getInstance() {
_configProvider.getAuthorization(),
_gitVersion,
_graphService.supportsMultiHop(),
_visualConfiguration
_visualConfiguration,
_trackingConfiguration
).builder().build();
}
}
3 changes: 3 additions & 0 deletions metadata-service/factories/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ visualConfig:
assets:
logoUrl: ${REACT_APP_LOGO_URL:#{null}}

trackingConfig:
isThirdPartyLoggingEnabled: ${IS_THIRD_PARTY_LOGGING_ENABLED:false}

# Storage Layer
ebean:
username: ${EBEAN_DATASOURCE_USERNAME:datahub}
Expand Down

0 comments on commit 19b0bb8

Please sign in to comment.