Skip to content

Instantly share code, notes, and snippets.

@jaycdave88
Last active September 24, 2020 00:01
Show Gist options
  • Select an option

  • Save jaycdave88/8dfef89ab2fd4b72bf6696c447e45c02 to your computer and use it in GitHub Desktop.

Select an option

Save jaycdave88/8dfef89ab2fd4b72bf6696c447e45c02 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Datadog.Trace; //Adding the Datadog.Trace nuget package (https://www.nuget.org/packages/Datadog.Trace)
namespace BlobServiceAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class BlobServiceController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
UploadToBlob();
return new string[] { "Upload to blob triggered" };
}
private async void UploadToBlob()
{
//Creating a new instance of Tracer object for Datadog .NET APM
using (var scope = Tracer.Instance.StartActive("blobServiceController.uploadToBlob"))
{
// adding additional metadata via tags to the spans.
var span = scope.Span;
span.Type = SpanTypes.Http;
span.ResourceName = "POST";
span.ServiceName = "blobserviceapi";
string storageConnectionString = Startup.AzureConnectionString;
CloudStorageAccount storageAccount;
if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
{
// If the connection string is valid, proceed with operations against Blob
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("products");
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("dd_test." + Guid.NewGuid().ToString() + ".txt");
//adding manual Datadog trace_id injection for custom log, (https://docs.datadoghq.com/tracing/connect_logs_and_traces/dotnet/?tab=serilog)
string msgbody = $"New blob added to {cloudBlobContainer.StorageUri.PrimaryUri} :: dd.trace_id={span.TraceId} dd.span_id={span.SpanId}";
await cloudBlockBlob.UploadTextAsync(msgbody);
}
else
{
// Otherwise, let the user know that they need to define the environment variable.
throw new Exception(
"A connection string has not been defined in the system environment variables. " +
"Add an environment variable named 'CONNECT_STR' with your storage " +
"connection string as a value.");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment