-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathProductsController.cs
138 lines (120 loc) · 4.43 KB
/
ProductsController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Net;
using System.Web.Http;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.Util;
using Amazon.XRay.Recorder.Core;
using Amazon.XRay.Recorder.Handlers.SqlServer;
using Amazon.XRay.Recorder.Handlers.System.Net;
using SampleEBWebApplication.Models;
namespace SampleEBWebApplication.Controllers
{
public class ProductsController : ApiController
{
private static readonly Lazy<AmazonDynamoDBClient> LazyDdbClient = new Lazy<AmazonDynamoDBClient>(() =>
{
var client = new AmazonDynamoDBClient(EC2InstanceMetadata.Region ?? RegionEndpoint.USWest2);
// var client = new AmazonDynamoDBClient(RegionEndpoint.USWest2); // When running locally, configure with desired region and comment above line of client creation.
return client;
});
private static readonly Lazy<Table> LazyTable = new Lazy<Table>(() =>
{
var tableName = ConfigurationManager.AppSettings["DDB_TABLE_NAME"];
return Table.LoadTable(LazyDdbClient.Value, tableName);
});
public IHttpActionResult GetProduct(int id)
{
try
{
// Trace DynamoDB requests
var product = AWSXRayRecorder.Instance.TraceMethod<Product>("QueryProduct", () => QueryProduct(id));
// Trace out-going HTTP request
AWSXRayRecorder.Instance.TraceMethod("Outgoing Http Request", MakeHttpRequest);
// Trace SQL query
// AWSXRayRecorder.Instance.TraceMethod("Query SQL", () => QuerySql(id));
CustomSubsegment(); // generate custom subsegment
return Ok(product);
}
catch(ProductNotFoundException)
{
return NotFound();
}
}
private void CustomSubsegment()
{
try
{
AWSXRayRecorder.Instance.BeginSubsegment("CustomSubsegment");
// Custom logic
}
catch (Exception e)
{
AWSXRayRecorder.Instance.AddException(e);
}
finally
{
AWSXRayRecorder.Instance.EndSubsegment();
}
}
public IHttpActionResult PostProduct(Product product)
{
try
{
AWSXRayRecorder.Instance.TraceMethod("AddProduct", () => AddProduct(product));
return StatusCode(HttpStatusCode.Created);
}
catch (Exception)
{
return StatusCode(HttpStatusCode.InternalServerError);
}
}
private void MakeHttpRequest()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.amazon.com");
request.GetResponseTraced();
}
private void QuerySql(int id)
{
var connectionString = ConfigurationManager.AppSettings["RDS_CONNECTION_STRING"];
using (var sqlConnection = new SqlConnection(connectionString))
using (var sqlCommand = new TraceableSqlCommand("SELECT " + id, sqlConnection))
{
sqlCommand.Connection.Open();
sqlCommand.ExecuteNonQuery();
}
}
private Product QueryProduct(int id)
{
var item = LazyTable.Value.GetItem(id);
if (item == null)
{
throw new ProductNotFoundException("Can't find a product with id = " + id);
}
return BuildProduct(item);
}
private void AddProduct(Product product)
{
var document = new Document();
document["Id"] = product.Id;
document["Name"] = product.Name;
document["Price"] = product.Price;
LazyTable.Value.PutItem(document);
}
private Product BuildProduct(Document document)
{
var product = new Product();
product.Id = document["Id"].AsInt();
product.Name = document["Name"].AsString();
product.Price = document["Price"].AsDecimal();
return product;
}
private class ProductNotFoundException : Exception
{
public ProductNotFoundException(string message) : base(message) { }
}
}
}