Skip to content

Commit a73d332

Browse files
feat: Updating semantic conventions (#148)
**Requirements** - [X] I have added test coverage for new or changed functionality - [X] I have followed the repository's [pull request submission guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests) - [ ] I have validated my changes against all supported platform versions This is just updating variable names in-line with the new update to the SDK standard: https://github.com/launchdarkly/sdk-specs/pull/104/files
1 parent 40e641b commit a73d332

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

pkgs/telemetry/src/TracingHook.cs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ namespace LaunchDarkly.Sdk.Server.Telemetry
1515
public class TracingHookBuilder
1616
{
1717
private bool _createActivities;
18-
private bool _includeVariant;
18+
private bool _includeValue;
1919
private string _environmentId;
2020

2121
internal TracingHookBuilder()
2222
{
2323
_createActivities = false;
24-
_includeVariant = false;
24+
_includeValue = false;
2525
_environmentId = null;
2626
}
2727

@@ -41,15 +41,28 @@ public TracingHookBuilder CreateActivities(bool createActivities = true)
4141
return this;
4242
}
4343

44+
/// <summary>
45+
/// The TracingHook will include the flag value in the current activity, if one exists.
46+
/// The value representation is a JSON string. Disabled by default.
47+
/// </summary>
48+
/// <param name="includeValue">true to include value, false otherwise</param>
49+
/// <returns>this builder</returns>
50+
public TracingHookBuilder IncludeValue(bool includeValue = true)
51+
{
52+
_includeValue = includeValue;
53+
return this;
54+
}
55+
4456
/// <summary>
4557
/// The TracingHook will include the flag variant in the current activity, if one exists.
4658
/// The variant representation is a JSON string. Disabled by default.
4759
/// </summary>
4860
/// <param name="includeVariant">true to include variants, false otherwise</param>
4961
/// <returns>this builder</returns>
62+
[System.Obsolete("IncludeVariant is deprecated. Use IncludeValue instead.")]
5063
public TracingHookBuilder IncludeVariant(bool includeVariant = true)
5164
{
52-
_includeVariant = includeVariant;
65+
_includeValue = includeVariant;
5366
return this;
5467
}
5568

@@ -77,7 +90,7 @@ public TracingHookBuilder EnvironmentId(string environmentId)
7790
/// <returns>the new hook</returns>
7891
public TracingHook Build()
7992
{
80-
return new TracingHook(new TracingHook.Options(_createActivities, _includeVariant, _environmentId));
93+
return new TracingHook(new TracingHook.Options(_createActivities, _includeValue, _environmentId));
8194
}
8295
}
8396

@@ -107,23 +120,25 @@ private static class SemanticAttributes
107120
{
108121
public const string EventName = "feature_flag";
109122
public const string FeatureFlagKey = "feature_flag.key";
110-
public const string FeatureFlagProviderName = "feature_flag.provider_name";
111-
public const string FeatureFlagVariant = "feature_flag.variant";
112-
public const string FeatureFlagContextKeyAttributeName = "feature_flag.context.key";
123+
public const string FeatureFlagProviderName = "feature_flag.provider.name";
124+
public const string FeatureFlagValue = "feature_flag.result.value";
125+
public const string FeatureFlagVariationIndex = "feature_flag.result.variationIndex";
126+
public const string FeatureFlagInExperiment = "feature_flag.result.reason.inExperiment";
127+
public const string FeatureFlagContextKeyAttributeName = "feature_flag.context.id";
113128
public const string FeatureFlagSetId = "feature_flag.set.id";
114129
}
115130

116131
internal struct Options
117132
{
118133
public bool CreateActivities { get; }
119-
public bool IncludeVariant { get; }
134+
public bool IncludeValue { get; }
120135

121136
public string EnvironmentId { get; }
122137

123-
public Options(bool createActivities, bool includeVariant, string environmentId = null)
138+
public Options(bool createActivities, bool includeValue, string environmentId = null)
124139
{
125140
CreateActivities = createActivities;
126-
IncludeVariant = includeVariant;
141+
IncludeValue = includeValue;
127142
EnvironmentId = environmentId;
128143
}
129144
}
@@ -179,7 +194,7 @@ public override SeriesData BeforeEvaluation(EvaluationSeriesContext context, Ser
179194

180195
/// <summary>
181196
/// Ends the activity created in BeforeEvaluation, if it exists. Adds the feature flag key, provider name, and context key
182-
/// to the existing activity. If IncludeVariant is enabled, also adds the variant.
197+
/// to the existing activity. If includeValue is enabled, also adds the variant.
183198
/// </summary>
184199
/// <param name="context">the evaluation parameters</param>
185200
/// <param name="data">the series data</param>
@@ -216,9 +231,19 @@ public override SeriesData AfterEvaluation(EvaluationSeriesContext context, Seri
216231
attributes[SemanticAttributes.FeatureFlagSetId] = context.EnvironmentId;
217232
}
218233

219-
if (_options.IncludeVariant)
234+
if (_options.IncludeValue)
235+
{
236+
attributes.Add(SemanticAttributes.FeatureFlagValue, detail.Value.ToJsonString());
237+
}
238+
239+
if (detail.Reason.InExperiment)
240+
{
241+
attributes.Add(SemanticAttributes.FeatureFlagInExperiment, detail.Reason.InExperiment);
242+
}
243+
244+
if (detail.VariationIndex.HasValue)
220245
{
221-
attributes.Add(SemanticAttributes.FeatureFlagVariant, detail.Value.ToJsonString());
246+
attributes.Add(SemanticAttributes.FeatureFlagVariationIndex, detail.VariationIndex.Value);
222247
}
223248

224249
Activity.Current?.AddEvent(new ActivityEvent(name: SemanticAttributes.EventName, tags: attributes));

pkgs/telemetry/test/TracingHookTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public void CanRetrieveActivitySourceName()
3737
[InlineData(false, true)]
3838
[InlineData(true, false)]
3939
[InlineData(true, true)]
40-
public void ConfigurationOptionsDoNotThrowExceptions(bool includeVariant, bool createSpans)
40+
public void ConfigurationOptionsDoNotThrowExceptions(bool includeValue, bool createSpans)
4141
{
4242
var hook = TracingHook.Builder()
43-
.IncludeVariant(includeVariant)
43+
.IncludeValue(includeValue)
4444
.CreateActivities(createSpans)
4545
.Build();
4646
var context = new EvaluationSeriesContext("foo", Context.New("bar"), LdValue.Null, "testMethod");
@@ -164,7 +164,7 @@ public void TracingHookCreatesChildSpans(bool createSpans)
164164
[Theory]
165165
[InlineData(true)]
166166
[InlineData(false)]
167-
public void TracingHookIncludesVariant(bool includeVariant)
167+
public void TracingHookIncludesVariant(bool includeValue)
168168
{
169169
ICollection<Activity> exportedItems = new Collection<Activity>();
170170

@@ -178,7 +178,7 @@ public void TracingHookIncludesVariant(bool includeVariant)
178178

179179
var testSource = new ActivitySource("test-source", "1.0.0");
180180

181-
var hookUnderTest = TracingHook.Builder().IncludeVariant(includeVariant).Build();
181+
var hookUnderTest = TracingHook.Builder().IncludeValue(includeValue).Build();
182182
var featureKey = "feature-key";
183183
var context = Context.New("foo");
184184

@@ -203,21 +203,21 @@ public void TracingHookIncludesVariant(bool includeVariant)
203203
Assert.Single(items);
204204
Assert.Equal("root-activity", items[0].OperationName);
205205

206-
if (includeVariant)
206+
if (includeValue)
207207
{
208208
// The idea is to check that the span has two events attached to it, and those events contain the feature
209209
// flag variants. It's awkward to check because we don't know the exact order of the events or those
210210
// events' tags.
211211
var events = items[0].Events;
212212
Assert.Single(events.Where(e =>
213-
e.Tags.Contains(new KeyValuePair<string, object>("feature_flag.variant", "true"))));
213+
e.Tags.Contains(new KeyValuePair<string, object>("feature_flag.result.value", "true"))));
214214
Assert.Single(events.Where(e =>
215-
e.Tags.Contains(new KeyValuePair<string, object>("feature_flag.variant", "\"default\""))));
215+
e.Tags.Contains(new KeyValuePair<string, object>("feature_flag.result.value", "\"default\""))));
216216
}
217217
else
218218
{
219219
// If not including the variant, then we shouldn't see any variant tag on any events.
220-
Assert.All(items, i => i.Events.All(e => e.Tags.All(kvp => kvp.Key != "feature_flag.variant")));
220+
Assert.All(items, i => i.Events.All(e => e.Tags.All(kvp => kvp.Key != "feature_flag.result.value")));
221221
}
222222
}
223223

0 commit comments

Comments
 (0)