TelemetryClient.InstrumentationKey property determination is very confusing
TelemetryClient.InstrumenationKey only returns a value if a value was explicitly assigned; where in fact the instrumentation key that a client uses to send telemetry can be determined in one of three ways:
- Assigned directly using property setter
- Through environment variable.
- Through configuration file.
I would expect, at any time if I want to ascertain which instrumentation key would my telemetry be sent to when I use a particular client object, I should be able to inspect the property. Today, if I have not explicitly set the property but used one of the other methods to specify it, the telemetry still get sent but client is not able to tell me where.
Personally I think that, if possible, a read reference to an instrumentation key property should walk the tree of possible iKey sources and find and return the first instance it finds rather than null if that level/source was not where the iKey was explicitly set.
It was implemented this way in early versions. We changed it for the number of reasons.
First, the premise that you'll know which key will be used it wrong. One can submit an item with InstrumentationKey overridden. Key can be changed in one of TelemetryInitializers or even in processor.
Second, we didn't have many scenario when anybody will actually need it outside of SDK writers.
Third is a big confusion that get was returning something different from set. Assert in this code will fail:
TelemetryConfiguration.Active.InstrumentationKey = "A";
tc1 = new TelemetryClient();
tc2 = new TelemetryClient();
if (tc1.InstrumentationKey == "A")
{
tc1. InstrumentationKey = "B";
}
Assert.AreEqual("B", tc2.InstrumentationKey);
In the current design TelemetryConfiguration is responsible for context population, filtering and efficient channel. Clients are just producing data. Not reading it and not setting any global contexts. Only the context and identity for the data they produces. Like SqlConnection object do not allow to open connection or get the "already opened by the connection pool" connection.
So it is a bigger discussion along the same lines as "let me set role Instance or some custom property globally". Changing it in one place will bring more confusion than solve anything.
I'm not suggesting that setting it on a local context should propagate that to global. What I am suggesting is that if you try to read it locally and there is no local override value then it should try to find a value up the chain because not doing so is confusing for us SDK writers. The only change being proposed by me is that the logic which already does this when sending simply be exposed through the getters for iKey at different levels (if that doesn't make for ugly OO anti-patterns).
Logic that works on Track includes calling of all telemetry initializers in case they have an opinion which key telemetry item should have. So I don't know how partially exposed key will help. What scenario do you have in mind when somebody needs it?
I'm not suggesting that setting it on a local context should propagate that to global.
That is exactly the confusion I'm talking about. Setting it globally will confuse some, not setting - others.
Node sdk has a totally different interface. For one - the client doesn't allow you to get / set instrumentation key on it directly. The instrumentation key is on the config object that the client contains. This can be set in the constructor - not as a property. Finally, when you do a config.get_instrumenationKey, it does give you back the property value walking the entire set of options.
Should we in general be looking for a consistent experience across sdks?
My opinion is that the current implementation in the various .NET SDK's is confusing already.
@nizarq, yes we should. It just a historical reasons why it is working this way. Ideally need to align going forward
my 5 cents: I don't understand the scenario when someone needs to have multiple instrumentation keys in the same process and/or wants to override another one set in the settings/env variable. This kind of flexibility does not look valuable, quite hard to use and is prone to configuration errors.