Skip to content

Commit ebf03e1

Browse files
committed
Add clarifying examples to SetTransactionName API doc
1 parent a69979b commit ebf03e1

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

src/content/docs/apm/agents/net-agent/net-agent-api/net-agent-api.mdx

+65-2
Original file line numberDiff line numberDiff line change
@@ -2334,7 +2334,7 @@ The following list contains the different calls you can make with the API, inclu
23342334

23352335
### Description
23362336

2337-
Sets the name of the current transaction. Before you use this call, ensure you understand the implications of [metric grouping issues](/docs/agents/manage-apm-agents/troubleshooting/metric-grouping-issues).
2337+
Sets the name (other than the initial `WebTransaction` or `OtherTransaction` base name) of the current transaction. Before you use this call, ensure you understand the implications of [metric grouping issues](/docs/agents/manage-apm-agents/troubleshooting/metric-grouping-issues).
23382338

23392339
If you use this call multiple times within the same transaction, each call overwrites the previous call and the last call sets the name.
23402340

@@ -2392,8 +2392,71 @@ The following list contains the different calls you can make with the API, inclu
23922392

23932393
### Examples
23942394

2395+
This example shows use of this API in an ASP.NET Core MVC controller. A transaction is created automatically by the agent's instrumentation for ASP.NET Core. The first part of the transaction name will continue to be `WebTransaction`.
2396+
2397+
```cs
2398+
public class HomeController : Controller
2399+
{
2400+
2401+
public IActionResult Order(string product)
2402+
{
2403+
2404+
// The commented-out API call below is probably a bad idea and will lead to a metric grouping issue (MGI)
2405+
// because too many transaction names will be created. Don't do this.
2406+
//NewRelic.Api.Agent.NewRelic.SetTransactionName("Other", $"ProductOrder-{product}");
2407+
2408+
// Do this instead if you want to record request-specific data about this MVC endpoint
2409+
var tx = NewRelic.Api.Agent.NewRelic.GetAgent().CurrentTransaction;
2410+
tx.AddCustomAttribute("productName", product);
2411+
2412+
// The default transaction name at this point will be: WebTransaction/MVC/Home/Order
2413+
2414+
// Set custom transaction name
2415+
NewRelic.Api.Agent.NewRelic.SetTransactionName("Other", "OrderProduct");
2416+
2417+
// Transaction name is now: WebTransaction/Other/OrderProduct
2418+
2419+
return View();
2420+
}
2421+
}
2422+
```
2423+
2424+
This example shows use of this API in a console application. Note the `[Transaction]` custom instrumentation attribute, which is necessary to create a transaction for the example method. The first part of the transaction name will continue to be `OtherTransaction`.
2425+
23952426
```cs
2396-
NewRelic.Api.Agent.NewRelic.SetTransactionName("Other", "MyTransaction");
2427+
using NewRelic.Api.Agent;
2428+
2429+
namespace SetApplicationNameConsoleExample
2430+
{
2431+
internal class Program
2432+
{
2433+
static void Main(string[] args)
2434+
{
2435+
Console.WriteLine("Hello, World!");
2436+
2437+
var start = DateTime.Now;
2438+
while (DateTime.Now - start < TimeSpan.FromMinutes(2))
2439+
{
2440+
DoSomething();
2441+
Thread.Sleep(TimeSpan.FromSeconds(5));
2442+
}
2443+
}
2444+
2445+
[Transaction] // Attribute-based custom instrumentation to create a transaction for this method
2446+
static void DoSomething()
2447+
{
2448+
Console.WriteLine("Doing something: " + Guid.NewGuid().ToString());
2449+
2450+
// Transaction name from default naming at this point is: OtherTransaction/Custom/SetApplicationNameConsoleExample.Program/DoSomething
2451+
2452+
NewRelic.Api.Agent.NewRelic.SetTransactionName("Console", "MyCustomTransactionName");
2453+
2454+
// Transaction name at this point is: OtherTransaction/Console/MyCustomTransactionName
2455+
2456+
// Note, however, that this transaction will still have a child segment (span) named "SetApplicationNameConsoleExample.Program.DoSomething"
2457+
}
2458+
}
2459+
}
23972460
```
23982461
</Collapser>
23992462

0 commit comments

Comments
 (0)