6
6
import com .oracle .bmc .secrets .model .Base64SecretBundleContentDetails ;
7
7
import com .oracle .bmc .secrets .requests .GetSecretBundleRequest ;
8
8
import com .oracle .bmc .secrets .responses .GetSecretBundleResponse ;
9
- import oracle .observability .metrics .MetricsExporter ;
10
9
import oracle .ucp .jdbc .PoolDataSource ;
11
10
import oracle .ucp .jdbc .PoolDataSourceFactory ;
12
11
import org .apache .commons .codec .binary .Base64 ;
13
12
import org .slf4j .Logger ;
14
13
import org .slf4j .LoggerFactory ;
15
14
16
- import java .io .File ;
17
- import java .io .IOException ;
15
+ import java .io .*;
18
16
import java .sql .SQLException ;
19
17
import java .util .HashMap ;
20
18
import java .util .Map ;
@@ -26,61 +24,110 @@ public class ObservabilityExporter {
26
24
public File DEFAULT_METRICS_FILE ;
27
25
public String CUSTOM_METRICS = System .getenv ("CUSTOM_METRICS" ); //
28
26
public String QUERY_TIMEOUT = System .getenv ("QUERY_TIMEOUT" ); // "5"
27
+ public static final String CONTEXT = "context" ;
28
+ public static final String REQUEST = "request" ;
29
+
30
+ //Single/global datasource config related....
29
31
public String DATABASE_MAXIDLECONNS = System .getenv ("DATABASE_MAXIDLECONNS" ); // "0"
30
32
public String DATABASE_MAXOPENCONNS = System .getenv ("DATABASE_MAXOPENCONNS" ); // "10"
31
33
public static String DATA_SOURCE_NAME = System .getenv ("DATA_SOURCE_NAME" ); //eg %USER%/$(dbpassword)@%PDB_NAME%_tp
32
- public static String DATA_SOURCE_USER = System .getenv ("DATA_SOURCE_USER" ); //eg %USER%/$(dbpassword)@%PDB_NAME%_tp
33
- public static String DATA_SOURCE_PASSWORD = System .getenv ("DATA_SOURCE_PASSWORD" ); //eg %USER%/$(dbpassword)@%PDB_NAME%_tp
34
- public static String DATA_SOURCE_SERVICENAME = System .getenv ("DATA_SOURCE_SERVICENAME" ); //eg %USER%/$(dbpassword)@%PDB_NAME%_tp
35
- public String TNS_ADMIN = System .getenv ("TNS_ADMIN" ); //eg /msdataworkshop/creds
36
- public String OCI_REGION = System .getenv ("OCI_REGION" ); //eg us-ashburn-1
37
- public String VAULT_SECRET_OCID = System .getenv ("VAULT_SECRET_OCID" ); //eg ocid....
38
- public String OCI_CONFIG_FILE = System .getenv ("OCI_CONFIG_FILE" ); //eg "~/.oci/config"
39
- public String OCI_PROFILE = System .getenv ("OCI_PROFILE" ); //eg "DEFAULT"
40
- public static final String CONTEXT = "context" ;
41
- public static final String REQUEST = "request" ;
34
+ //if all three of the following exist, they are internally concatenated and override/used as DATA_SOURCE_NAME
35
+ public static String DATA_SOURCE_USER = System .getenv ("DATA_SOURCE_USER" ); //eg %USER%
36
+ public static String DATA_SOURCE_PASSWORD = System .getenv ("DATA_SOURCE_PASSWORD" ); //eg $(dbpassword)
37
+ public static String DATA_SOURCE_SERVICENAME = System .getenv ("DATA_SOURCE_SERVICENAME" ); //eg %PDB_NAME%_tp
38
+ public static String TNS_ADMIN = System .getenv ("TNS_ADMIN" ); //eg /msdataworkshop/creds
42
39
43
- static {
40
+ public static String OCI_REGION = System .getenv ("OCI_REGION" ); //eg us-ashburn-1
41
+ public static String VAULT_SECRET_OCID = System .getenv ("VAULT_SECRET_OCID" ); //eg ocid....
42
+ public static String OCI_CONFIG_FILE = System .getenv ("OCI_CONFIG_FILE" ); //eg "~/.oci/config"
43
+ public static String OCI_PROFILE = System .getenv ("OCI_PROFILE" ); //eg "DEFAULT"
44
44
45
+ //MULTI_DATASOURCE_CONFIG related....
46
+ public static String MULTI_DATASOURCE_CONFIG = System .getenv ("MULTI_DATASOURCE_CONFIG" );
47
+ public static final String SERVICE_NAME_STRING = "serviceName" ;
48
+ public static final String USER_NAME_STRING = "userName" ;
49
+ public static final String PASSWORD_STRING = "password" ;
50
+ public static final String TNS_ADMIN_STRING = "TNS_ADMIN" ;
51
+ public static final String PASSWORD_OCID_STRING = "passwordOCID" ;
52
+ public static final String OCI_CONFIG_FILE_STRING = "ociConfigFile" ;
53
+ public static final String OCI_REGION_STRING = "ociRegion" ;
54
+ public static final String OCI_PROFILE_STRING = "ociProfile" ;
55
+
56
+ static { // not really necessary but gives information that a global datasource is in use
45
57
if (DATA_SOURCE_USER != null && DATA_SOURCE_PASSWORD != null && DATA_SOURCE_SERVICENAME != null ) {
46
58
DATA_SOURCE_NAME = DATA_SOURCE_USER + "/" + DATA_SOURCE_PASSWORD + "@" + DATA_SOURCE_SERVICENAME ;
47
59
LOGGER .info ("DATA_SOURCE_NAME = DATA_SOURCE_USER + \" /\" + DATA_SOURCE_PASSWORD + \" @\" + DATA_SOURCE_SERVICENAME" );
48
60
//eg %USER%/$(dbpassword)@%PDB_NAME%_tp
49
61
}
50
62
}
51
- PoolDataSource observabilityDB ;
63
+ PoolDataSource globalObservabilityDB ;
64
+
65
+ //This map is used for multi-datasource scraping, both when using dns target string and config
52
66
Map <String , PoolDataSource > dataSourceNameToDataSourceMap = new HashMap <>();
53
67
68
+ //This map is used for multi-datasource scraping when using config only
69
+ public static Map <String , DataSourceConfig > dataSourceNameToDataSourceConfigMap = new HashMap <>();
70
+
71
+ //used by logs and tracing exporters as they do not currently support multi-datasource config
54
72
public PoolDataSource getPoolDataSource () throws SQLException {
55
- return getPoolDataSource (DATA_SOURCE_NAME );
73
+ return getPoolDataSource (DATA_SOURCE_NAME , false );
56
74
}
57
- public PoolDataSource getPoolDataSource (String dataSourceName ) throws SQLException {
58
- if (dataSourceName .equals (DATA_SOURCE_NAME )) {
59
- if (observabilityDB != null ) return observabilityDB ;
60
- return observabilityDB = getDataSource (DATA_SOURCE_NAME );
75
+
76
+ public PoolDataSource getPoolDataSource (String dataSourceName , boolean isScrapeByName ) throws SQLException {
77
+ if (DATA_SOURCE_NAME != null && dataSourceName .equals (DATA_SOURCE_NAME )) {
78
+ if (globalObservabilityDB != null ) return globalObservabilityDB ;
79
+ return globalObservabilityDB = getDataSource (DATA_SOURCE_NAME );
61
80
} else {
62
81
if (dataSourceNameToDataSourceMap .containsKey (dataSourceName ) && dataSourceNameToDataSourceMap .get (dataSourceName ) != null )
63
82
return dataSourceNameToDataSourceMap .get (dataSourceName );
64
- PoolDataSource poolDataSource = getDataSource (dataSourceName );
83
+
84
+ System .out .println ("putting dataSourceName:" + dataSourceName + " isScrapeByName:" + isScrapeByName +
85
+ " ObservabilityExporter.dataSourceNameToDataSourceConfigMap.get(dataSourceName):" +
86
+ ObservabilityExporter .dataSourceNameToDataSourceConfigMap .get (dataSourceName ));
87
+ PoolDataSource poolDataSource = isScrapeByName ?
88
+ getDataSource (ObservabilityExporter .dataSourceNameToDataSourceConfigMap .get (dataSourceName ))
89
+ :getDataSource (dataSourceName );
65
90
dataSourceNameToDataSourceMap .put (dataSourceName , poolDataSource );
66
91
return poolDataSource ;
67
92
}
68
93
}
69
94
70
95
private PoolDataSource getDataSource (String dataSourceName ) throws SQLException {
71
- PoolDataSource poolDataSource = PoolDataSourceFactory .getPoolDataSource ();
72
- poolDataSource .setConnectionFactoryClassName ("oracle.jdbc.pool.OracleDataSource" );
73
96
String user = dataSourceName .substring (0 , dataSourceName .indexOf ("/" ));
74
97
String pw = dataSourceName .substring (dataSourceName .indexOf ("/" ) + 1 , dataSourceName .indexOf ("@" ));
75
98
String serviceName = dataSourceName .substring (dataSourceName .indexOf ("@" ) + 1 );
76
- String url = "jdbc:oracle:thin:@" + serviceName + "?TNS_ADMIN=" + TNS_ADMIN ;
99
+ return getPoolDataSource (dataSourceName , user , pw , serviceName , TNS_ADMIN ,
100
+ VAULT_SECRET_OCID , OCI_CONFIG_FILE , OCI_PROFILE , OCI_REGION , false );
101
+ }
102
+ private PoolDataSource getDataSource (DataSourceConfig dataSourceConfig ) throws SQLException {
103
+ return getPoolDataSource (dataSourceConfig .getDataSourceName (),
104
+ dataSourceConfig .getUserName (),
105
+ dataSourceConfig .getPassword (),
106
+ dataSourceConfig .getServiceName (),
107
+ dataSourceConfig .getTNS_ADMIN (),
108
+ dataSourceConfig .getPasswordOCID (),
109
+ dataSourceConfig .getOciConfigFile (),
110
+ dataSourceConfig .getOciProfile (),
111
+ dataSourceConfig .getOciRegion (),
112
+ true );
113
+ }
114
+
115
+ private PoolDataSource getPoolDataSource (
116
+ String dataSourceName , String user , String pw , String serviceName , String tnsAdmin ,
117
+ String vaultSecretOcid , String ociConfigFile , String ociProfile , String ociRegion , boolean isScrapeByName ) throws SQLException {
118
+ System .out .println ("getPoolDataSource dataSourceName = " + dataSourceName + ", user = " + user + ", pw = " + pw + ", serviceName = " + serviceName + ", vaultSecretOcid = " + vaultSecretOcid + ", ociConfigFile = " + ociConfigFile + ", ociProfile = " + ociProfile + ", ociRegion = " + ociRegion + ", isScrapeByName = " + isScrapeByName );
119
+ PoolDataSource poolDataSource = PoolDataSourceFactory .getPoolDataSource ();
120
+ poolDataSource .setConnectionFactoryClassName ("oracle.jdbc.pool.OracleDataSource" );
121
+ String url = "jdbc:oracle:thin:@" + serviceName + "?TNS_ADMIN=" + tnsAdmin ;
77
122
poolDataSource .setURL (url );
78
123
poolDataSource .setUser (user );
79
- if (VAULT_SECRET_OCID == null || VAULT_SECRET_OCID .trim ().equals ("" ) || !dataSourceName .equals (DATA_SOURCE_NAME )) {
124
+ if (VAULT_SECRET_OCID == null || VAULT_SECRET_OCID .trim ().equals ("" ) ||
125
+ //vault is not supported with scrape by dns currently, only with scrape by datasource name and global datasource
126
+ (!isScrapeByName && !dataSourceName .equals (DATA_SOURCE_NAME )) ) {
80
127
poolDataSource .setPassword (pw );
81
128
} else {
82
129
try {
83
- poolDataSource .setPassword (getPasswordFromVault ());
130
+ poolDataSource .setPassword (getPasswordFromVault (vaultSecretOcid , ociConfigFile , ociProfile , ociRegion ));
84
131
} catch (IOException e ) {
85
132
throw new SQLException (e );
86
133
}
@@ -89,18 +136,18 @@ private PoolDataSource getDataSource(String dataSourceName) throws SQLException
89
136
}
90
137
91
138
92
- public String getPasswordFromVault () throws IOException {
139
+ public String getPasswordFromVault (String vaultSecretOcid , String ociConfigFile , String ociProfile , String ociRegion ) throws IOException {
93
140
SecretsClient secretsClient ;
94
- if (OCI_CONFIG_FILE == null || OCI_CONFIG_FILE .trim ().equals ("" )) {
141
+ if (ociConfigFile == null || ociConfigFile .trim ().equals ("" )) {
95
142
secretsClient = new SecretsClient (InstancePrincipalsAuthenticationDetailsProvider .builder ().build ());
96
143
} else {
97
- String profile = OCI_PROFILE ==null || OCI_PROFILE .trim ().equals ("" ) ? "DEFAULT" : OCI_PROFILE ;
98
- secretsClient = new SecretsClient (new ConfigFileAuthenticationDetailsProvider (OCI_CONFIG_FILE , profile ));
144
+ String profile = ociProfile ==null || ociProfile .trim ().equals ("" ) ? "DEFAULT" : ociProfile ;
145
+ secretsClient = new SecretsClient (new ConfigFileAuthenticationDetailsProvider (ociConfigFile , profile ));
99
146
}
100
- secretsClient .setRegion (OCI_REGION );
147
+ secretsClient .setRegion (ociRegion );
101
148
GetSecretBundleRequest getSecretBundleRequest = GetSecretBundleRequest
102
149
.builder ()
103
- .secretId (VAULT_SECRET_OCID )
150
+ .secretId (vaultSecretOcid )
104
151
.stage (GetSecretBundleRequest .Stage .Current )
105
152
.build ();
106
153
GetSecretBundleResponse getSecretBundleResponse = secretsClient .getSecretBundle (getSecretBundleRequest );
0 commit comments