Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

容器化、扩展、优化 #2

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
e092497
添加运维文件
Nov 16, 2018
baaa84b
添加容器化到k8s的相关文件
Nov 16, 2018
60ff313
修改小心模板
Nov 16, 2018
57ebb85
调整文件位置
Nov 16, 2018
b602a73
添加配置文件
Nov 16, 2018
90edb72
修改值名称
Nov 16, 2018
708c0c8
添加环境和来源
Nov 16, 2018
23bf9d1
添加环境信息
Nov 16, 2018
9f3ed4a
优化httpclient使用方式
Nov 16, 2018
52b1c57
修改配置代码
Nov 16, 2018
0d0849f
添加序列化时间区域性设置
Nov 16, 2018
60197f9
修改命名空间
Nov 16, 2018
73e4222
Update README.md
justmine66 Nov 16, 2018
01d8143
Update README.md
justmine66 Nov 16, 2018
a6de3f7
Update README.md
justmine66 Nov 16, 2018
ecbca22
Update README.md
justmine66 Nov 16, 2018
a0127c1
Add files via upload
justmine66 Nov 16, 2018
a65a2dc
Update README.md
justmine66 Nov 16, 2018
d1a2680
Update README.md
justmine66 Nov 16, 2018
2502d25
Update README.md
justmine66 Nov 16, 2018
5fb311d
Update README.md
justmine66 Nov 16, 2018
0f13d37
Update README.md
justmine66 Nov 16, 2018
1e7850e
Update README.md
justmine66 Nov 16, 2018
1d935e7
Update README.md
justmine66 Nov 16, 2018
48b50d9
Update README.md
justmine66 Nov 16, 2018
8be07be
Update README.md
justmine66 Nov 16, 2018
a8fd709
Update README.md
justmine66 Nov 16, 2018
f107411
Update README.md
justmine66 Nov 16, 2018
affbe0b
Update README.md
justmine66 Nov 16, 2018
547ca53
Update README.md
justmine66 Nov 16, 2018
f5f0158
Update README.md
justmine66 Nov 16, 2018
e6c16e5
Update README.md
justmine66 Nov 16, 2018
63ddc4a
Update README.md
justmine66 Nov 16, 2018
c175961
Update README.md
justmine66 Nov 16, 2018
6c29381
Update README.md
justmine66 Nov 16, 2018
9355033
Update README.md
justmine66 Nov 16, 2018
5c9cee8
Update web.yml
justmine66 Nov 16, 2018
414aa48
Update README.md
justmine66 Nov 16, 2018
6dc25e4
Update README.md
justmine66 Nov 19, 2018
67e3a0b
Update README.md
justmine66 Nov 19, 2018
681966b
Update README.md
justmine66 Nov 26, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,47 @@
# exceptionless-webhooks
一个 Exceptionless 相关 WebHooks 项目。
## 目标
DingTalk(钉钉)

# 目的
当Exceptionless触发配置的WebHooks通知类型时,如Error、LogError,发送消息到 DingTalk(钉钉),实时感知运维报警提醒。

# 效果图

![Image text](https://github.com/justmine66/exceptionless-webhooks/blob/master/result.png)

# 步骤
## 1、部署
请选择一种部署方式。
### docker

``` shell
docker run -d -p 8000:80 justmine/exceptionless.api.webhook:0.0.0;
```

### kubernetes(推荐)

[deployment.yml](https://github.com/justmine66/exceptionless-webhooks/blob/master/k8s/web.yml)

``` shell
kubectl apply -f https://github.com/justmine66/exceptionless-webhooks/blob/master/k8s/web.yml;
```
## 2、配置

请先部署好webhook钩子。

### Exceptionless

Admin => Projects => Integrations => Add Web Hook:

![Image text](https://github.com/justmine66/exceptionless-webhooks/blob/master/config.png)

>注意:如果你的项目Webhook配置选项没有Error、LogEroor类型,说明Exceptionless团队还没有合并我的PR,请先拉取我的项目:[后端](https://github.com/justmine66/Exceptionless)、[前端](https://github.com/justmine66/Exceptionless.UI),前后端都需要拉取最新项目代码。

# changes

1. 添加容器化部署脚本,支持docker、kubernetes。
2. 扩展事件模型,添加环境、来源等信息。
3. 升级项目为netcoreapp2.1。
4. 优化httpclient使用方式。
5. 本地化事件模型时间。

# 如有疑惑,欢迎QQ。
3538307147
26 changes: 26 additions & 0 deletions build/CI.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
set build-base-path=../exceptionless.webhooks
set outpust-base-path=../../build
set username=justmine

for /f "delims=> tokens=2" %%i in ('findstr "<VersionMajor>.*</VersionMajor>" version.props')do set "Major=%%i";
for /f "delims=> tokens=2" %%j in ('findstr "<VersionMinor>.*</VersionMinor>" version.props')do set "Minor=%%j";
for /f "delims=> tokens=2" %%h in ('findstr "<VersionPatch>.*</VersionPatch>" version.props')do set "Patch=%%h";

set Major=%Major:~0,1%
set Minor=%Minor:~0,1%
set Patch=%Patch:~0,1%
set Version=%Major%.%Minor%.%Patch%

set first=Web
set version=%Version%
set imagename=exceptionless.api.webhook
set imagefullname=%username%/%imagename%:%version%

dotnet publish %build-base-path%/%first%/%first%.csproj -o %outpust-base-path%/%first% -c release
docker build -t %imagefullname% ./%first%
docker push %imagefullname%

rd /s /q %first%

pause
goto EOF
9 changes: 9 additions & 0 deletions build/version.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>
<PropertyGroup>
<VersionMajor>0</VersionMajor>
<VersionMinor>0</VersionMinor>
<VersionPatch>0</VersionPatch>
<VersionQuality></VersionQuality>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
</PropertyGroup>
</Project>
Binary file added config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Exceptionless.WebHook.DingTalk.Utilitys
namespace ExceptionLess.WebHook.DingTalk.Utilities
{
public class StringUtilitys
public class StringUtility
{
public static string ToCamelCase(string str)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="2.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.4.0" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.4" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="2.1.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="2.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.5.1" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.1.0" />
<PackageReference Include="Rabbit.Extensions.DependencyInjection" Version="1.0.0-preview5" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
using Newtonsoft.Json;
using System;

namespace Exceptionless.WebHook.Abstractions
namespace ExceptionLess.WebHook.Abstractions
{
public class ExceptionlessEventModel
public class ExceptionLessEventModel
{
public string Environment { get; set; }

public string Id { get; set; }
public string Url { get; set; }

[JsonProperty("occurrence_date")]
public DateTime OccurrenceDate { get; set; }

public string Type { get; set; }
public string Source { get; set; }
public string Message { get; set; }

[JsonProperty("project_id")]
public string ProjectId { get; set; }

[JsonProperty("project_Name")]
public string ProjectName { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
using System.Text;
using System.Threading.Tasks;

namespace Exceptionless.WebHook.Abstractions
namespace ExceptionLess.WebHook.Abstractions
{
public class FileTemplateService : ISingletonDependency, IDisposable
{
#region Field

private readonly IMemoryCache _memoryCache;
private readonly PhysicalFileProvider _physicalFileProvider;
private readonly PhysicalFileProvider _fileProvider;

#endregion Field

Expand All @@ -24,7 +24,7 @@ public class FileTemplateService : ISingletonDependency, IDisposable
public FileTemplateService(IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
_physicalFileProvider = new PhysicalFileProvider(AppContext.BaseDirectory);
_fileProvider = new PhysicalFileProvider(AppContext.BaseDirectory);
}

#endregion Constructor
Expand All @@ -40,17 +40,17 @@ public async Task<string> GetContent(string templateFile, object model)
var properties = model.GetType().GetProperties();
var builder = new StringBuilder(template);

var propertys = new Dictionary<string, string>();
var propCache = new Dictionary<string, string>();
string GetPropertyValue(PropertyInfo property)
{
if (propertys.TryGetValue(property.Name, out var value))
if (propCache.TryGetValue(property.Name, out var value))
{
return value;
}

value = property.GetValue(model)?.ToString();

propertys[property.Name] = value;
propCache[property.Name] = value;

return value;
}
Expand All @@ -72,8 +72,8 @@ private async Task<string> GetTemplate(string templateFile)
{
return await _memoryCache.GetOrCreateAsync(templateFile, async entry =>
{
entry.AddExpirationToken(_physicalFileProvider.Watch(templateFile));
var fileInfo = _physicalFileProvider.GetFileInfo(templateFile);
entry.AddExpirationToken(_fileProvider.Watch(templateFile));
var fileInfo = _fileProvider.GetFileInfo(templateFile);
if (!fileInfo.Exists)
return string.Empty;

Expand All @@ -92,7 +92,7 @@ private async Task<string> GetTemplate(string templateFile)
public void Dispose()
{
_memoryCache?.Dispose();
_physicalFileProvider?.Dispose();
_fileProvider?.Dispose();
}

#endregion IDisposable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Exceptionless.WebHook.Abstractions
namespace ExceptionLess.WebHook.Abstractions
{
public interface IWebHookProvider : ISingletonDependency
{
string Name { get; }

Task ProcessAsync(ExceptionlessEventModel model, IDictionary<string, string> parameters);
Task ProcessAsync(ExceptionLessEventModel model, IDictionary<string, string> parameters);
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
using Exceptionless.WebHook.Abstractions;
using Exceptionless.WebHook.DingTalk.Messages;
using Exceptionless.WebHook.DingTalk.Services;
using ExceptionLess.WebHook.Abstractions;
using ExceptionLess.WebHook.DingTalk.Messages;
using ExceptionLess.WebHook.DingTalk.Services;
using Microsoft.AspNetCore.Hosting;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Exceptionless.WebHook.DingTalk
namespace ExceptionLess.WebHook.DingTalk
{
public class DingTalkWebHookProvider : IWebHookProvider
{
private readonly FileTemplateService _fileTemplateService;
private readonly MessageService _messageService;
private readonly IHostingEnvironment _environment;

public DingTalkWebHookProvider(FileTemplateService fileTemplateService, MessageService messageService)
public DingTalkWebHookProvider(
FileTemplateService fileTemplateService,
MessageService messageService,
IHostingEnvironment environment)
{
_fileTemplateService = fileTemplateService;
_messageService = messageService;
_environment = environment;
}

#region Implementation of IWebHookProvider

public string Name { get; } = "DingTake";

public async Task ProcessAsync(ExceptionlessEventModel model, IDictionary<string, string> parameters)
public async Task ProcessAsync(ExceptionLessEventModel model, IDictionary<string, string> parameters)
{
parameters.TryGetValue("accessToken", out var accessToken);

model.Environment = _environment.EnvironmentName;
var content = await _fileTemplateService.GetContent("markdownTemplate.md", model);

await _messageService.SendAsync(
new Uri($"https://oapi.dingtalk.com/robot/send?access_token={accessToken}"),
new DingTalkRequestMessage
{
Data = new MarkdownDingTalkMessage("Exceptionless 有新的事件", content)
Data = new MarkdownDingTalkMessage($"[{_environment.EnvironmentName}]ExceptionLess有新事件", content)
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Exceptionless.WebHook.Abstractions\Exceptionless.WebHook.Abstractions.csproj" />
<ProjectReference Include="..\ExceptionLess.WebHook.Abstractions\ExceptionLess.WebHook.Abstractions.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions">
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.hosting.abstractions\2.1.1\lib\netstandard2.0\Microsoft.AspNetCore.Hosting.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.Http">
<HintPath>C:\Users\justmine\.nuget\packages\microsoft.extensions.http\2.1.1\lib\netstandard2.0\Microsoft.Extensions.Http.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Exceptionless.WebHook.DingTalk.Messages
namespace ExceptionLess.WebHook.DingTalk.Messages
{
public class DingTalkRequestMessage
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json;

namespace Exceptionless.WebHook.DingTalk.Messages
namespace ExceptionLess.WebHook.DingTalk.Messages
{
public class DingTalkResponseMessage
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Exceptionless.WebHook.DingTalk.Messages
namespace ExceptionLess.WebHook.DingTalk.Messages
{
public enum MessageType
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json;

namespace Exceptionless.WebHook.DingTalk.Messages
namespace ExceptionLess.WebHook.DingTalk.Messages
{
public abstract class DingTalkMessageBase
{
Expand Down
Loading