Skip to content

Ad Hoc Profiles #2963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jan 26, 2024
Merged

Ad Hoc Profiles #2963

merged 23 commits into from
Jan 26, 2024

Conversation

aleks-p
Copy link
Contributor

@aleks-p aleks-p commented Jan 24, 2024

Adds APIs for uploading and retrieving "ad hoc" profiles. Profiles are stored in the tenant object storage, in a new directory (adhoc/) next to phlaredb/.

The code for parsing the data and doing conversions is copied (with minor adaptations) from OG Pyroscope to maintain feature parity with the original Ad Hoc implementation.

I've also added a new "sidekick" module that is meant to own this feature and perhaps others (tenant settings) in the future, as a means to simplify operations. An alternative could be to create a single deployment with multiple targets (e.g., -target tenant-settings,ad-hoc-profiles,admin). cc @cyriltovena, @simonswine

Edit: As of ad79c09 the module is just ad-hoc-profiles. Deployment considerations will be handled elsewhere.

The only validation done in this PR is to make sure the profile is readable. In the future, we could also add per-tenant limits (e.g., storage usage).

TODO:

  • tests

@aleks-p aleks-p requested a review from a team as a code owner January 24, 2024 15:49
Comment on lines 82 to 83
uid := ulid.MustNew(ulid.Timestamp(adHocProfile.UploadedAt), rand.Reader)
id := strings.Join([]string{uid.String(), adHocProfile.Name}, "-")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keys in the object storage will have the form 01HMXV8BF4EH71NBYZNPPVGJ2X-cpu.pprof, using ULID for the first segment to simplify embedding a timestamp. This is then used in the /List endpoint to return meaningful data without querying each individual profile.

type Sidekick struct {
services.Service

AdHocProfiles *adhocprofiles.AdHocProfiles
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could make AdHocProfiles a subservice perhaps, if we decide that having a Sidekick module still makes sense. This could come in in a second PR where we "move" tenant settings here.

return nil, connect.NewError(connect.CodeInvalidArgument, err)
}

bucket, err := a.getBucket(tenantID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you use here ?

bucket, err := a.getBucketFromContext(ctx)
	if err != nil {
		return nil, err
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using getBucket(tenant) saves a bit of work here, because we already extracted the tenantId a few lines above. Nothing major, but avoiding doing things twice.

id, err := ulid.Parse(s[0:separatorIndex])
if err != nil {
level.Warn(a.logger).Log("msg", "cannot parse ad hoc profile", "key", s, "err", err)
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: may be we can continue here ? Otherwise a tenant can get corrupted and will never be able to list the rest ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are inside bucket.Iter() here, returning nil continues the iteration


profiles := make([]*v1.AdHocProfilesProfileMetadata, 0)
err = bucket.Iter(ctx, "", func(s string) error {
separatorIndex := bytes.IndexByte([]byte(s), '-')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
separatorIndex := bytes.IndexByte([]byte(s), '-')
separatorIndex := strings.IndexRune(s, '-')

This is to avoid []byte conversion which copies the string, it's irrelevant here but I just wanted to let you know if you ever do that in a hot path be aware of string <-> []byte conversion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggested line is also shorter and more readable, thanks for that tip!

return a.getBucket(tenantID)
}

func (a *AdHocProfiles) getBucket(tenantID string) (objstore.Bucket, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to cache buckets you could create one child per request and throw it away.

but may be there's a reason ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason, I was not sure about the overhead of creating buckets. Since this is not in a hot path, I can definitely simplify it.

Copy link
Contributor

@cyriltovena cyriltovena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing LGTM !

@aleks-p aleks-p merged commit f59c959 into main Jan 26, 2024
@aleks-p aleks-p deleted the feat/ad-hoc-component branch January 26, 2024 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants