Skip to content

Conversation

@rockdaboot
Copy link
Contributor

@rockdaboot rockdaboot commented Mar 6, 2025

Fixes open-telemetry/opentelemetry-collector#12554

Currently, it is pretty tedious and error-prone to generate valid test profiles.

This PR adds Go structs and transform functions for profiles types that allow creating valid test cases in a human readable way. It also takes care for some semantics that make a profile valid, for example that the first entry in the string table is the empty string.

Here is an example from profiles_test.go

Before (this produces an invalid profile, btw)

	p := pprofile.NewProfiles()
	rl := p.ResourceProfiles().AppendEmpty()
	rl.Resource().Attributes().PutStr("key1", "value1")
	l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
	attr := l.AttributeTable().AppendEmpty()
	attr.SetKey("scope-attr1")
	attr.Value().SetStr("value1")
	l.AttributeIndices().Append(0)
	l.SetProfileID(pprofile.NewProfileIDEmpty())
	rl2 := p.ResourceProfiles().AppendEmpty()
	rl2.Resource().Attributes().PutStr("key2", "value2")
	l2 := rl2.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
	attr = l2.AttributeTable().AppendEmpty()
	attr.SetKey("scope-attr2")
	attr.Value().SetStr("value2")
	l2.AttributeIndices().Append(0)
	l2.SetProfileID(pprofile.NewProfileIDEmpty())
	return p

After

	p := basicProfiles()
	p.ResourceProfiles = append(p.ResourceProfiles, ResourceProfile{
		Resource: Resource{
			Attributes: []Attribute{{"key2", "value2"}},
		},
		ScopeProfiles: []ScopeProfile{
			{
				Profile: []Profile{
					{
						Attributes: []Attribute{{"scope-attr2", "value2"}},
					},
				},
			},
		},
	})
	return p.Transform()

where basicProfiles() is a helper function that creates a basic test Profiles.

Planned a follow-up PR with a validator for profiles (working locally already).

@atoulme atoulme merged commit 36f3292 into open-telemetry:main Mar 7, 2025
156 checks passed
@github-actions github-actions bot added this to the next release milestone Mar 7, 2025
atoulme pushed a commit that referenced this pull request Mar 7, 2025
This is a fix for
#38430
.

It fixes adding sample locations to profiles.

cc @atoulme
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Simplify creating test data for profiles support

3 participants