Skip to content

Commit

Permalink
Add connection proxy settings to Elasticsearch output (#395)
Browse files Browse the repository at this point in the history
* Add connection proxy settings to Elasticsearch output

Implements #394
  • Loading branch information
karolz-ms authored Nov 16, 2021
1 parent b76ad2f commit ba585ed
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 9 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -949,8 +949,10 @@ This output writes data to the [Elasticsearch](https://www.elastic.co/products/e
"type": "date_nanos"
}
}
},
"proxy": {
"uri": "https://myESProxy/"
}

}
```
| Field | Values/Types | Required | Description |
Expand All @@ -967,7 +969,8 @@ This output writes data to the [Elasticsearch](https://www.elastic.co/products/e
| `refreshInterval` | string | No | Specifies what refresh interval the index is created with. If not specified, it defaults to 15s.|
| `defaultPipeline` | string | No | Specifies the default ingest node pipeline the index is created with. If not specified, a default pipeline will not be used.|
| `mappings` | object | No | Specifies how documents created by the Elasticsearch output are stored and indexed (index mappings). For more information refer to [Elasticsearch documentation on index mappings](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html). |
| `mappings.properties` | object | No | Specifies property mappings for documents created by the Elasticsearch output. Currently only property *type* mappings can be specified. Supported types are: `text`, `keyword`, `date`, `date_nanos`, `boolean`, `long`, `integer`, `short`, `byte`, `double`, `float`, `half_float`, `scaled_float`, `ip`, `geo_point`, `geo_shape`, and `completion`. |
| `mappings.properties` | object | No | Specifies property mappings for documents created by the Elasticsearch output. Currently only property *type* mappings can be specified. Supported types are: `text`, `keyword`, `date`, `date_nanos`, `boolean`, `long`, `integer`, `short`, `byte`, `double`, `float`, `half_float`, `scaled_float`, `ip`, `geo_point`, `geo_shape`, and `completion`. |
| `proxy` | object | No | Specifies connection proxy settings. Valid properties are `uri` (proxy URI, i.e. address), `userName` and `userPassword`. |

*Standard metadata support*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Diagnostics.EventFlow.Configuration
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ public class ElasticSearchOutputConfiguration: ItemConfiguration
public string DefaultPipeline { get; set; }

public ElasticSearchMappingsConfiguration Mappings { get; set; }
public ElasticSearchProxyConfiguration Proxy { get; set; }

public ElasticSearchOutputConfiguration()
{
NumberOfShards = DefaultNumberOfShards;
NumberOfReplicas = DefaultNumberOfReplicas;
RefreshInterval = DefaultRefreshInterval;
Mappings = new ElasticSearchMappingsConfiguration();
Proxy = new ElasticSearchProxyConfiguration();
}

public ElasticSearchOutputConfiguration DeepClone()
Expand All @@ -53,7 +55,8 @@ public ElasticSearchOutputConfiguration DeepClone()
NumberOfReplicas = this.NumberOfReplicas,
RefreshInterval = this.RefreshInterval,
DefaultPipeline = this.DefaultPipeline,
Mappings = this.Mappings.DeepClone()
Mappings = this.Mappings.DeepClone(),
Proxy = this.Proxy.DeepClone()
};

return other;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Diagnostics.EventFlow.Configuration
{
public class ElasticSearchProxyConfiguration
{
public string Uri { get; set; }
public string UserName { get; set; }
public string UserPassword { get; set; }

internal ElasticSearchProxyConfiguration DeepClone()
{
var other = new ElasticSearchProxyConfiguration();
other.Uri = this.Uri;
other.UserName = this.UserName;
other.UserPassword = this.UserPassword;
return other;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,19 @@ private void Initialize(ElasticSearchOutputConfiguration esOutputConfiguration)
connectionSettings = connectionSettings.BasicAuthentication(userName, password);
}

if (!string.IsNullOrWhiteSpace(esOutputConfiguration.Proxy.Uri))
{
if (Uri.TryCreate(esOutputConfiguration.Proxy.Uri, UriKind.Absolute, out Uri proxyUri))
{
connectionSettings.Proxy(proxyUri, esOutputConfiguration.Proxy.UserName, esOutputConfiguration.Proxy.UserPassword);
}
else
{
var errorMessage = $"{nameof(ElasticSearchOutput)}: proxy URI setting value '{esOutputConfiguration.Proxy.Uri}' is not valid";
healthReporter.ReportWarning(errorMessage, EventFlowContextIdentifiers.Configuration);
}
}

this.connectionData.Client = new ElasticClient(connectionSettings);
this.connectionData.LastIndexName = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>Provides an output implementation that sends diagnostics data to Elasticsearch.</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<VersionPrefix>2.7.3</VersionPrefix>
<VersionPrefix>2.7.4</VersionPrefix>
<Authors>Microsoft</Authors>
<TargetFrameworks>net471;netstandard2.0</TargetFrameworks>
<AssemblyName>Microsoft.Diagnostics.EventFlow.Outputs.ElasticSearch</AssemblyName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ public void VerifyAllConfigOptionsMappedToConfigObject()
["type"] = "date_nanos"
}
}
}
},
["proxy"] = new Dictionary<string, object>
{
["uri"] = "https://proxy.local/net/esproxy",
["userName"] = "esuser",
["userPassword"] = "verysecret"
},
}
}
};
Expand Down Expand Up @@ -74,6 +80,11 @@ public void VerifyAllConfigOptionsMappedToConfigObject()
Assert.NotNull(esOutputConfiguration.Mappings.Properties);
Assert.NotNull(esOutputConfiguration.Mappings.Properties["timestamp"]);

Assert.NotNull(esOutputConfiguration.Proxy);
Assert.Equal("https://proxy.local/net/esproxy", esOutputConfiguration.Proxy.Uri);
Assert.Equal("esuser", esOutputConfiguration.Proxy.UserName);
Assert.Equal("verysecret", esOutputConfiguration.Proxy.UserPassword);

Assert.Equal("date_nanos", esOutputConfiguration.Mappings.Properties["timestamp"].Type);
}
}
Expand Down

0 comments on commit ba585ed

Please sign in to comment.