Skip to content

Commit

Permalink
Update LineCounter to .NET 7 and use OTel instead of AppInsights (Azu…
Browse files Browse the repository at this point in the history
…re#39913)

* Update LineCounter to .NET and use OTel instead of AppInsights
  • Loading branch information
lmolkova authored Nov 13, 2023
1 parent 6613379 commit cbc04f8
Show file tree
Hide file tree
Showing 16 changed files with 406 additions and 317 deletions.
146 changes: 0 additions & 146 deletions samples/linecounter/BlobProcessorHost.cs

This file was deleted.

102 changes: 48 additions & 54 deletions samples/linecounter/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,75 +1,69 @@

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Text;
using Azure.Messaging;
using Azure.Messaging.EventGrid;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
using Azure.Storage.Blobs;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Logging;

namespace LineCounter.Controllers
namespace LineCounter.Controllers;

[Route("/")]
public class HomeController : Controller
{
[Route("/")]
public class HomeController : Controller
private readonly ILogger<HomeController> _logger;
private readonly BlobContainerClient _blobContainerClient;
private readonly EventHubProducerClient _uploadsProducer;
private readonly EventGridPublisherClient _publisherClient;

public HomeController(
ILogger<HomeController> logger,
BlobServiceClient blobServiceClient,
IAzureClientFactory<EventHubProducerClient> clientFactory,
EventGridPublisherClient publisherClient)
{
private readonly ILogger<HomeController> _logger;
private readonly BlobContainerClient _blobContainerClient;
private readonly EventHubProducerClient _uploadsProducer;
private readonly EventGridPublisherClient _publisherClient;
_logger = logger;
_blobContainerClient = blobServiceClient.GetBlobContainerClient("uploads");
_uploadsProducer = clientFactory.CreateClient("Uploads");
_publisherClient = publisherClient;
}

public HomeController(
ILogger<HomeController> logger,
BlobServiceClient blobServiceClient,
IAzureClientFactory<EventHubProducerClient> clientFactory,
EventGridPublisherClient publisherClient)
{
_logger = logger;
_blobContainerClient = blobServiceClient.GetBlobContainerClient("uploads");
_uploadsProducer = clientFactory.CreateClient("Uploads");
_publisherClient = publisherClient;
}
public IActionResult Index()
{
return View();
}

public IActionResult Index()
{
return View();
}
[HttpPost("upload")]
public async Task<IActionResult> Upload()
{
var names = new List<string>();

[HttpPost("upload")]
public async Task<IActionResult> Upload()
foreach (var file in HttpContext.Request.Form.Files)
{
var names = new List<string>();

foreach (var file in HttpContext.Request.Form.Files)
{
var fileName = Guid.NewGuid().ToString("n") + " " + file.FileName;
var fileName = Guid.NewGuid().ToString("n") + " " + file.FileName;

var stream = file.OpenReadStream();
_logger.LogInformation("Uploading {fileName}", fileName);
await _blobContainerClient.CreateIfNotExistsAsync();
await _blobContainerClient.UploadBlobAsync(fileName, stream);
var stream = file.OpenReadStream();
_logger.LogInformation("Uploading {fileName}", fileName);
await _blobContainerClient.CreateIfNotExistsAsync();
await _blobContainerClient.UploadBlobAsync(fileName, stream);

using EventDataBatch eventBatch = await _uploadsProducer.CreateBatchAsync();
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(fileName)));
await _uploadsProducer.SendAsync(eventBatch);
using EventDataBatch eventBatch = await _uploadsProducer.CreateBatchAsync();
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(fileName)));
await _uploadsProducer.SendAsync(eventBatch);

names.Add(fileName);
}

return View("Index", names.ToArray());
names.Add(fileName);
}

[HttpGet("status/{name}.html")]
public async Task<string> Status(string name)
{
var properties = await _blobContainerClient.GetBlobClient(name).GetPropertiesAsync();
properties.Value.Metadata.TryGetValue("whitespacecount", out var count);
await _publisherClient.SendEventsAsync(new CloudEvent[] { new CloudEvent("https://www.contoso.com/LineCounter", "LineCounter.Status.Viewed", name) });
return count ?? "-1";
}
return View("Index", names.ToArray());
}

[HttpGet("status/{name}.html")]
public async Task<string> Status(string name)
{
var properties = await _blobContainerClient.GetBlobClient(name).GetPropertiesAsync();
properties.Value.Metadata.TryGetValue("whitespacecount", out var count);
await _publisherClient.SendEventsAsync(new CloudEvent[] { new CloudEvent("https://www.contoso.com/LineCounter", "LineCounter.Status.Viewed", name) });
return count ?? "-1";
}
}
20 changes: 11 additions & 9 deletions samples/linecounter/LineCounter.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>67ca5d37-051b-4ab1-b013-c74a2d4edb76</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.2.0" />
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.4.1" />
<PackageReference Include="Azure.Messaging.EventHubs.Processor" Version="5.4.1" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.13.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.18.0" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.0.0" />
<PackageReference Include="System.Text.Encodings.Web" Version="4.7.2" />
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.21.0" />
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.10.0" />
<PackageReference Include="Azure.Messaging.EventHubs.Processor" Version="5.10.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.0" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.1" />
<PackageReference Include="System.Text.Encodings.Web" Version="8.0.0-rc.2.23479.6" />
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.0.0-beta.8" />
</ItemGroup>
<ItemGroup>
<Folder Include="Connected Services\" />
<Folder Include="wwwroot\lib\" />
</ItemGroup>
</Project>
Loading

0 comments on commit cbc04f8

Please sign in to comment.