Skip to content

Commit 57f5319

Browse files
committed
refactor examples
1 parent 9912ec8 commit 57f5319

27 files changed

Lines changed: 46 additions & 842 deletions

examples/Kafka/Avro/src/Avro.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
<PackageReference Include="Amazon.Lambda.Core" Version="2.5.0"/>
2020
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4"/>
2121
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="2.0.0" />
22-
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="2.0.0" />
23-
2422
</ItemGroup>
2523
<ItemGroup>
2624
<ProjectReference Include="..\..\..\..\libraries\src\AWS.Lambda.Powertools.Kafka.Avro\AWS.Lambda.Powertools.Kafka.Avro.csproj" />
Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,22 @@
1-
using System.Diagnostics;
21
using Amazon.Lambda.Core;
32
using Amazon.Lambda.RuntimeSupport;
4-
using Amazon.Lambda.Serialization.SystemTextJson;
53
using AWS.Lambda.Powertools.Kafka;
64
using AWS.Lambda.Powertools.Kafka.Avro;
75
using AWS.Lambda.Powertools.Kafka.Tests;
86
using AWS.Lambda.Powertools.Logging;
9-
using AWS.Lambda.Powertools.Metrics;
10-
using com.example;
117

12-
// string Handler(ConsumerRecords<AvroKey, AvroProduct> records, ILambdaContext context)
13-
// {
14-
// Metrics.SetNamespace("Avro");
15-
// Metrics.AddMetric("NumberOfRequests", 1, MetricUnit.Count, MetricResolution.High);
16-
//
17-
// foreach (var record in records)
18-
// {
19-
// Logger.LogInformation("Record Key: {@key}", record.Key);
20-
// Logger.LogInformation("Record Value: {@record}", record.Value);
21-
// }
22-
//
23-
// return "Processed " + records.Count() + " records";
24-
// }
25-
//
26-
//
27-
// await LambdaBootstrapBuilder.Create((Func<ConsumerRecords<AvroKey, AvroProduct>, ILambdaContext, string>?)Handler,
28-
// new PowertoolsKafkaAvroSerializer()) // Use PowertoolsKafkaAvroSerializer for Avro serialization
29-
// .Build()
30-
// .RunAsync();
31-
32-
var responseStream = new MemoryStream();
33-
var serializer = new PowertoolsKafkaAvroSerializer();
34-
Task<InvocationResponse> ToUpperAsync(InvocationRequest invocation)
8+
string Handler(ConsumerRecords<AvroKey, AvroProduct> records, ILambdaContext context)
359
{
36-
var stopwatch = Stopwatch.StartNew();
37-
38-
var records = serializer.Deserialize<ConsumerRecords<string, CustomerProfile>>(invocation.InputStream);
39-
4010
foreach (var record in records)
4111
{
42-
Console.WriteLine("Record UserId: {0}", record.Value.user_id);
12+
Logger.LogInformation("Record Value: {@record}", record.Value);
4313
}
44-
45-
stopwatch.Stop();
46-
47-
Metrics.PushSingleMetric("AvroDeserialization-1024",
48-
stopwatch.ElapsedMilliseconds, MetricUnit.Milliseconds, "kafka-dotnet", "service", null,
49-
MetricResolution.High);
50-
51-
Console.WriteLine("Record Count: {0}", records.Count());
52-
Console.WriteLine("Record UserId: {0}", records.First().Value.user_id);
53-
Console.WriteLine("JsonDeserialization: {0:F2}", stopwatch.ElapsedMilliseconds);
54-
55-
responseStream.SetLength(0);
56-
responseStream.Position = 0;
57-
58-
return Task.FromResult(new InvocationResponse(responseStream, false));
14+
15+
return "Processed " + records.Count() + " records";
5916
}
6017

61-
var bootstrap = new LambdaBootstrap(ToUpperAsync);
62-
await bootstrap.RunAsync();
18+
19+
await LambdaBootstrapBuilder.Create((Func<ConsumerRecords<AvroKey, AvroProduct>, ILambdaContext, string>?)Handler,
20+
new PowertoolsKafkaAvroSerializer()) // Use PowertoolsKafkaAvroSerializer for Avro serialization
21+
.Build()
22+
.RunAsync();

examples/Kafka/Avro/src/Readme.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# AWS Lambda Function Using Top Level Statements
1+
# Powertools Kafka Avro Lambda Function
22

33
This starter project consists of:
44
* Function.cs - file contain C# top level statements that define the function to be called for each event and starts the Lambda runtime client.
@@ -18,9 +18,10 @@ the Lambda runtime client add the `Amazon.Lambda.RuntimeSupport` NuGet package a
1818
of the file containing top-level statements to start the runtime.
1919

2020
```csharp
21-
await LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer())
22-
.Build()
23-
.RunAsync();
21+
await LambdaBootstrapBuilder.Create((Func<ConsumerRecords<AvroKey, AvroProduct>, ILambdaContext, string>?)Handler,
22+
new PowertoolsKafkaAvroSerializer()) // Use PowertoolsKafkaAvroSerializer for Avro serialization
23+
.Build()
24+
.RunAsync();
2425
```
2526

2627
Pass into the Lambda runtime client a function handler as either an `Action<>` or `Func<>` for the code that

examples/Kafka/Avro/src/aws-lambda-tools-defaults.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
"region": "",
1010
"configuration": "Release",
1111
"function-runtime": "dotnet8",
12-
"function-memory-size": 1024,
12+
"function-memory-size": 512,
1313
"function-timeout": 30,
14-
"function-handler": "Avro.Example",
15-
"function-name": "dotnet-kafka-avro-1024",
16-
"function-role": "arn:aws:iam::992382490249:role/dotnet-kafka-test-role"
14+
"function-handler": "Avro.Example"
1715
}

examples/Kafka/Json/src/Function.cs

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,25 @@
1-
using System.Diagnostics;
21
using System.Text.Json.Serialization;
32
using Amazon.Lambda.Core;
43
using Amazon.Lambda.RuntimeSupport;
54
using AWS.Lambda.Powertools.Kafka;
65
using AWS.Lambda.Powertools.Kafka.Json;
76
using AWS.Lambda.Powertools.Logging;
8-
using AWS.Lambda.Powertools.Metrics;
9-
10-
// string Handler(ConsumerRecords<JsonKey, JsonProduct> records, ILambdaContext context)
11-
// {
12-
// Metrics.SetNamespace("Json");
13-
// Metrics.AddMetric("NumberOfRequests", 1, MetricUnit.Count, MetricResolution.High);
14-
//
15-
// foreach (var record in records)
16-
// {
17-
// Logger.LogInformation("Record Key: {@record.Key}", record.Key);
18-
// Logger.LogInformation("Record Value: {@record}", record.Value);
19-
// }
20-
//
21-
// return "Processed " + records.Count() + " records";
22-
// }
23-
//
24-
//
25-
// await LambdaBootstrapBuilder.Create((Func<ConsumerRecords<JsonKey, JsonProduct>, ILambdaContext, string>?)Handler,
26-
// new PowertoolsKafkaJsonSerializer()) // Use PowertoolsKafkaAvroSerializer for Avro serialization
27-
// .Build()
28-
// .RunAsync();
29-
30-
var responseStream = new MemoryStream();
31-
var serializer = new PowertoolsKafkaJsonSerializer();
32-
33-
Task<InvocationResponse> ToUpperAsync(InvocationRequest invocation)
34-
{
35-
var stopwatch = Stopwatch.StartNew();
367

37-
var records = serializer.Deserialize<ConsumerRecords<JsonKey, Payload>>(invocation.InputStream);
38-
8+
string Handler(ConsumerRecords<JsonKey, Payload> records, ILambdaContext context)
9+
{
3910
foreach (var record in records)
4011
{
41-
Console.WriteLine("Record UserId: {0}", record.Value.UserId);
12+
Logger.LogInformation("Record Value: {@record}", record.Value);
4213
}
4314

44-
stopwatch.Stop();
45-
46-
Metrics.PushSingleMetric("JsonDeserialization-1024",
47-
stopwatch.ElapsedMilliseconds, MetricUnit.Milliseconds, "kafka-dotnet", "service", null,
48-
MetricResolution.High);
49-
50-
Console.WriteLine("Record Count: {0}", records.Count());
51-
Console.WriteLine("Record UserId: {0}", records.First().Value.UserId);
52-
Console.WriteLine("JsonDeserialization: {0:F2}", stopwatch.ElapsedMilliseconds);
53-
54-
responseStream.SetLength(0);
55-
responseStream.Position = 0;
56-
57-
return Task.FromResult(new InvocationResponse(responseStream, false));
15+
return "Processed " + records.Count() + " records";
5816
}
5917

60-
var bootstrap = new LambdaBootstrap(ToUpperAsync);
61-
await bootstrap.RunAsync();
18+
19+
await LambdaBootstrapBuilder.Create((Func<ConsumerRecords<JsonKey, Payload>, ILambdaContext, string>?)Handler,
20+
new PowertoolsKafkaJsonSerializer()) // Use PowertoolsKafkaAvroSerializer for Avro serialization
21+
.Build()
22+
.RunAsync();
6223

6324

6425
public record JsonKey

examples/Kafka/Json/src/Json.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<PackageReference Include="Amazon.Lambda.Core" Version="2.5.0"/>
1717
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4"/>
1818
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="2.0.0" />
19-
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="2.0.0" />
2019
</ItemGroup>
2120
<ItemGroup>
2221
<ProjectReference Include="..\..\..\..\libraries\src\AWS.Lambda.Powertools.Kafka.Json\AWS.Lambda.Powertools.Kafka.Json.csproj" />

examples/Kafka/Json/src/Readme.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# AWS Lambda Function Using Top Level Statements
1+
# Powertools Kafka JSON Lambda Function
22

33
This starter project consists of:
44
* Function.cs - file contain C# top level statements that define the function to be called for each event and starts the Lambda runtime client.
@@ -18,9 +18,10 @@ the Lambda runtime client add the `Amazon.Lambda.RuntimeSupport` NuGet package a
1818
of the file containing top-level statements to start the runtime.
1919

2020
```csharp
21-
await LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer())
22-
.Build()
23-
.RunAsync();
21+
await LambdaBootstrapBuilder.Create((Func<ConsumerRecords<JsonKey, Payload>, ILambdaContext, string>?)Handler,
22+
new PowertoolsKafkaJsonSerializer()) // Use PowertoolsKafkaAvroSerializer for Avro serialization
23+
.Build()
24+
.RunAsync();
2425
```
2526

2627
Pass into the Lambda runtime client a function handler as either an `Action<>` or `Func<>` for the code that

examples/Kafka/Json/src/aws-lambda-tools-defaults.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
"region": "",
1010
"configuration": "Release",
1111
"function-runtime": "dotnet8",
12-
"function-memory-size": 1024,
12+
"function-memory-size": 512,
1313
"function-timeout": 30,
14-
"function-handler": "Json",
15-
"function-name": "dotnet-kafka-json-1024",
16-
"function-role": "arn:aws:iam::992382490249:role/dotnet-kafka-test-role"
14+
"function-handler": "Json"
1715
}
Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,28 @@
1-
using System.Diagnostics;
21
using Amazon.Lambda.Core;
32
using Amazon.Lambda.RuntimeSupport;
43
using AWS.Lambda.Powertools.Kafka;
54
using AWS.Lambda.Powertools.Kafka.Protobuf;
65
using AWS.Lambda.Powertools.Logging;
7-
using AWS.Lambda.Powertools.Metrics;
8-
using Com.Example;
96
using TestKafka;
107

11-
// string Handler(ConsumerRecords<ProtobufKey, ProtobufProduct> records, ILambdaContext context)
12-
// {
13-
// Metrics.SetNamespace("Proto");
14-
// Metrics.AddMetric("NumberOfRequests", 1, MetricUnit.Count, MetricResolution.High);
15-
//
16-
// foreach (var record in records)
17-
// {
18-
// foreach (var header in record.Headers)
19-
// {
20-
// Console.WriteLine($"{header.Key}: {ToDecimalString(header.Value)}");
21-
// }
22-
//
23-
// foreach (var header in record.Headers.DecodedValues())
24-
// {
25-
// Console.WriteLine($"{header.Key}: {header.Value}");
26-
// }
27-
//
28-
// Logger.LogInformation("Record Key: {@key}", record.Key);
29-
// Logger.LogInformation("Record Value: {@record}", record.Value);
30-
// }
31-
//
32-
// return "Processed " + records.Count() + " records";
33-
// }
34-
//
35-
// await LambdaBootstrapBuilder.Create((Func<ConsumerRecords<ProtobufKey, ProtobufProduct>, ILambdaContext, string>?)Handler,
36-
// new PowertoolsKafkaProtobufSerializer()) // Use PowertoolsKafkaAvroSerializer for Avro serialization
37-
// .Build()
38-
// .RunAsync();
39-
40-
var responseStream = new MemoryStream();
41-
var serializer = new PowertoolsKafkaProtobufSerializer();
42-
43-
Task<InvocationResponse> ToUpperAsync(InvocationRequest invocation)
8+
string Handler(ConsumerRecords<ProtobufKey, ProtobufProduct> records, ILambdaContext context)
449
{
45-
var stopwatch = Stopwatch.StartNew();
46-
47-
var records = serializer.Deserialize<ConsumerRecords<string, CustomerProfile>>(invocation.InputStream);
48-
4910
foreach (var record in records)
5011
{
51-
Console.WriteLine("Record Key: {0}", record.Key);
52-
foreach (var header in record.Headers)
53-
{
54-
Console.WriteLine($"{header.Key}: {ToDecimalString(header.Value)}");
55-
}
56-
5712
foreach (var header in record.Headers.DecodedValues())
5813
{
5914
Console.WriteLine($"{header.Key}: {header.Value}");
6015
}
6116

62-
Console.WriteLine("Record UserId: {0}", record.Value);
17+
Logger.LogInformation("Record Key: {@key}", record.Key);
18+
Logger.LogInformation("Record Value: {@record}", record.Value);
6319
}
6420

65-
stopwatch.Stop();
66-
67-
Metrics.PushSingleMetric("ProtoDeserialization-512",
68-
stopwatch.ElapsedMilliseconds, MetricUnit.Milliseconds, "kafka-dotnet", "service", null,
69-
MetricResolution.High);
70-
71-
Console.WriteLine("Record Count: {0}", records.Count());
72-
Console.WriteLine("JsonDeserialization: {0:F2}", stopwatch.ElapsedMilliseconds);
73-
74-
responseStream.SetLength(0);
75-
responseStream.Position = 0;
76-
77-
return Task.FromResult(new InvocationResponse(responseStream, false));
21+
return "Processed " + records.Count() + " records";
7822
}
7923

80-
static string ToDecimalString(byte[] bytes)
81-
{
82-
if (bytes == null || bytes.Length == 0)
83-
{
84-
return "[]";
85-
}
86-
87-
return "[" + string.Join(", ", bytes) + "]";
88-
}
24+
await LambdaBootstrapBuilder.Create((Func<ConsumerRecords<ProtobufKey, ProtobufProduct>, ILambdaContext, string>?)Handler,
25+
new PowertoolsKafkaProtobufSerializer()) // Use PowertoolsKafkaAvroSerializer for Avro serialization
26+
.Build()
27+
.RunAsync();
8928

90-
var bootstrap = new LambdaBootstrap(ToUpperAsync);
91-
await bootstrap.RunAsync();

examples/Kafka/Protobuf/src/Protobuf.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<PackageReference Include="Amazon.Lambda.Core" Version="2.5.0" />
1717
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4" />
1818
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="2.0.0" />
19-
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="2.0.0" />
2019
<PackageReference Include="Grpc.Tools" Version="2.72.0">
2120
<PrivateAssets>all</PrivateAssets>
2221
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

0 commit comments

Comments
 (0)