处理横切问题,例如 Web 应用程序组件的内部统计报告
我正在尝试为 OLTP Web 应用程序的内部组件实现统计报告。例如,我想近乎实时地跟踪以下内容的使用情况或性能:成功/失败登录的数量、nhibernate 会话的数量、服务 HTTP 请求的时间、不同类型的交易数量(订单、访问等)。所有这些统计数据都通过 UDP 发送到 statsd 服务器 (https://github.com/etsy/statsd) 并使用 Graphite 绘制为图表。
该应用程序使用依赖注入来连接内部组件。我想将统计报告集中到 statsd 服务器自己的类中,并将其隐藏在界面下。然而,我觉得在应用程序的每个组件中注入统计报告类/接口的实例,报告性能或使用数据是一种味道。在我看来,性能报告事件应该是一个跨领域的关注点,就像日志记录一样。
对于这样的要求,您如何进行内部设计?您会使用构造函数注入、静态方法(例如 PerformanceCounters.Increment("name.of.counter"))来调用我的受监视组件吗?
如果对上下文有任何帮助,该应用程序是用 C# 完成的,并使用 ASP.NET 和 Castle Windsor 作为 IoC。
谢谢, 罗伯特
I am trying to implement statistics reporting for internal components of an OLTP web application. For example, I want to track near real time the usage or performance of things like: number of successful/failed logins, number of nhibernate sessions, time to serve a HTTP request, number transactions of different types (orders, accesses, etc). All these stats are sent via UDP to a statsd server (https://github.com/etsy/statsd) and plotted as charts with Graphite.
The application is using dependency injection for wiring internal components. I want to centralize statistics reporting towards statsd server in a class of its own and hide it under an interface. However, I feel like injecting an instance of the stats reporting class/interface in every component of the application which is reporting performance or usage data to be a smell. It seems to me that performance reporting events should be something like a cross cutting concern, pretty much like logging.
How do you approach the internal design for such a request? Would you approach with constructor injection, static methods (e.g. PerformanceCounters.Increment("name.of.counter")) which are called my monitored components or how?
If it is of any help for context, the app is done in C# and is using ASP.NET and Castle Windsor as IoC.
Thanks,
Robert
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用 Spring.Net 来做类似的事情。据我所知,Spring 与 IoC 的 Castle Windsor 非常相似,Spring 使用 CastleWindsor 创建动态代理。这些可以用于 Spring 支持的 AOP。学习曲线很短,框架有很好的文档记录。配置完性能方面后,将其应用到您的方法中应该非常容易。如果您想要小样本,请告诉我。
I use Spring.Net for something like that. Spring is very like Castle Windsor for IoC, as far as I have heard of, and Spring uses CastleWindsor for creating dynamic proxies. These ones you can use for AOP, which is supported by Spring. The learning curve is short, the framework is very well documented. Once your performance aspect is configured, applying it to your methods should be more than easy. Let me know if you want a small sample.
我认为您指的是面向方面的编程。 Castle Windsor 与大多数其他 IoC 容器一样支持 AOP。
正如您所说,您不会在每个组件中注入统计报告类/接口的实例。相反,在您的配置中,您用拦截器包装每个组件。该组件对统计报告一无所知;每次调用组件之前和之后都会调用拦截器,并且可以决定是否以及向何处发送跟踪信息。它非常适合日志记录、跟踪、性能计数、故障处理以及您提到的所有横切问题。
The design pattern I think you are referring to is Aspect Oriented Programming. Castle Windsor supports AOP as do most other IoC containers.
You don't inject an instance of the stats reporting class/interface in every component, as you say. Rather, in your configuration, you wrap each component with an interceptor. The component knows nothing about stats reporting; the interceptor is invoked before and after every call to your component and can decide whether and where to send trace information. It's perfect for things like logging, tracing, performance counting, failure handling, and all those cross-cutting concerns you mention.