|
14 | 14 | using Azure.DataApiBuilder.Service.Exceptions; |
15 | 15 | using Microsoft.Data.SqlClient; |
16 | 16 | using Microsoft.Extensions.Logging; |
| 17 | +using Microsoft.IdentityModel.Tokens; |
| 18 | +using Npgsql; |
17 | 19 |
|
18 | 20 | [assembly: InternalsVisibleTo("Azure.DataApiBuilder.Service.Tests")] |
19 | 21 | namespace Azure.DataApiBuilder.Config; |
@@ -105,11 +107,15 @@ public static bool TryParseConfig(string json, |
105 | 107 |
|
106 | 108 | DataSource ds = config.GetDataSourceFromDataSourceName(dataSourceKey); |
107 | 109 |
|
108 | | - // Add Application Name for telemetry for MsSQL |
| 110 | + // Add Application Name for telemetry for MsSQL or PgSql |
109 | 111 | if (ds.DatabaseType is DatabaseType.MSSQL && replaceEnvVar) |
110 | 112 | { |
111 | 113 | updatedConnection = GetConnectionStringWithApplicationName(connectionValue); |
112 | 114 | } |
| 115 | + else if (ds.DatabaseType is DatabaseType.PostgreSQL && replaceEnvVar) |
| 116 | + { |
| 117 | + updatedConnection = GetPgSqlConnectionStringWithApplicationName(connectionValue); |
| 118 | + } |
113 | 119 |
|
114 | 120 | ds = ds with { ConnectionString = updatedConnection }; |
115 | 121 | config.UpdateDataSourceNameToDataSource(dataSourceName, ds); |
@@ -235,4 +241,52 @@ internal static string GetConnectionStringWithApplicationName(string connectionS |
235 | 241 | // Return the updated connection string. |
236 | 242 | return connectionStringBuilder.ConnectionString; |
237 | 243 | } |
| 244 | + |
| 245 | + /// <summary> |
| 246 | + /// It adds or replaces a property in the connection string with `Application Name` property. |
| 247 | + /// If the connection string already contains the property, it appends the property `Application Name` to the connection string, |
| 248 | + /// else add the Application Name property with DataApiBuilder Application Name based on hosted/oss platform. |
| 249 | + /// </summary> |
| 250 | + /// <param name="connectionString">Connection string for connecting to database.</param> |
| 251 | + /// <returns>Updated connection string with `Application Name` property.</returns> |
| 252 | + internal static string GetPgSqlConnectionStringWithApplicationName(string connectionString) |
| 253 | + { |
| 254 | + // If the connection string is null, empty, or whitespace, return it as is. |
| 255 | + if (string.IsNullOrWhiteSpace(connectionString)) |
| 256 | + { |
| 257 | + return connectionString; |
| 258 | + } |
| 259 | + |
| 260 | + string applicationName = ProductInfo.GetDataApiBuilderUserAgent(); |
| 261 | + |
| 262 | + // Create a StringBuilder from the connection string. |
| 263 | + NpgsqlConnectionStringBuilder connectionStringBuilder; |
| 264 | + try |
| 265 | + { |
| 266 | + connectionStringBuilder = new NpgsqlConnectionStringBuilder(connectionString); |
| 267 | + } |
| 268 | + catch (Exception ex) |
| 269 | + { |
| 270 | + throw new DataApiBuilderException( |
| 271 | + message: DataApiBuilderException.CONNECTION_STRING_ERROR_MESSAGE, |
| 272 | + statusCode: HttpStatusCode.ServiceUnavailable, |
| 273 | + subStatusCode: DataApiBuilderException.SubStatusCodes.ErrorInInitialization, |
| 274 | + innerException: ex); |
| 275 | + } |
| 276 | + |
| 277 | + // If the connection string does not contain the `Application Name` property, add it. |
| 278 | + // or if the connection string contains the `Application Name` property, replace it with the DataApiBuilder Application Name. |
| 279 | + if (connectionStringBuilder.ApplicationName.IsNullOrEmpty()) |
| 280 | + { |
| 281 | + connectionStringBuilder.ApplicationName = applicationName; |
| 282 | + } |
| 283 | + else |
| 284 | + { |
| 285 | + // If the connection string contains the `ApplicationName` property with a value, update the value by adding the DataApiBuilder Application Name. |
| 286 | + connectionStringBuilder.ApplicationName += $",{applicationName}"; |
| 287 | + } |
| 288 | + |
| 289 | + // Return the updated connection string. |
| 290 | + return connectionStringBuilder.ConnectionString; |
| 291 | + } |
238 | 292 | } |
0 commit comments