Skip to content
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

feat: New Metrics metadata #175

Merged
merged 5 commits into from
Jul 29, 2024
Merged

feat: New Metrics metadata #175

merged 5 commits into from
Jul 29, 2024

Conversation

chriswk
Copy link
Member

@chriswk chriswk commented Jul 26, 2024

is included in client registration and client metrics

@chriswk chriswk requested a review from sighphyre July 26, 2024 10:43
@chriswk chriswk self-assigned this Jul 26, 2024
Copy link
Member

@sighphyre sighphyre left a comment

Choose a reason for hiding this comment

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

The code looks cool but what happened to specVersion?

config.go Outdated
return func(o *configOption) {
o.started = &startedAt
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Should started be configurable?

Copy link
Member Author

Choose a reason for hiding this comment

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

Mostly for tests here

Copy link
Member Author

Choose a reason for hiding this comment

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

I would've used #[cfg(test)] here, don't know what the same is in go.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, I've moved it into the test file where I used it instead.

Copy link
Member

Choose a reason for hiding this comment

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

I still don't like that we're polluting our config options with something that shouldn't be user-configurable and I'm still not super convinced of its value. Maybe we can relax our tests instead?

Copy link
Member

Choose a reason for hiding this comment

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

Something like this seems to work, but maybe there's a better way:

func clientDataMatcher() func(req *http.Request, ereq *gock.Request) (bool, error) {
	defaultStrategies := []string{
		"default",
		"applicationHostname",
		"gradualRolloutRandom",
		"gradualRolloutSessionId",
		"gradualRolloutUserId",
		"remoteAddress",
		"userWithId",
		"flexibleRollout",
	}
	return func(req *http.Request, ereq *gock.Request) (bool, error) {
		var data ClientData
		err := json.NewDecoder(req.Body).Decode(&data)
		if err != nil {
			return false, err
		}

		if data.Started.IsZero() {
			return false, nil
		}

		expectedData := ClientData{
			AppName:          mockAppName,
			InstanceID:       mockInstanceId,
			SDKVersion:       fmt.Sprintf("%s:%s", clientName, clientVersion),
			Strategies:       defaultStrategies,
			Interval:         0,
			PlatformVersion:  runtime.Version(),
			PlatformName:     "go",
			YggdrasilVersion: nil,
			SpecVersion:      specVersion,
		}

		return data.AppName == expectedData.AppName &&
			data.InstanceID == expectedData.InstanceID &&
			data.SDKVersion == expectedData.SDKVersion &&
			compareStringSlices(data.Strategies, expectedData.Strategies) &&
			data.Interval == expectedData.Interval &&
			data.PlatformVersion == expectedData.PlatformVersion &&
			data.PlatformName == expectedData.PlatformName &&
			data.YggdrasilVersion == expectedData.YggdrasilVersion &&
			data.SpecVersion == expectedData.SpecVersion, nil
	}
}

func compareStringSlices(a, b []string) bool {
	if len(a) != len(b) {
		return false
	}
	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}

func TestMetrics_ClientDataIncludesNewMetadata(t *testing.T) {
	assert := assert.New(t)
	defer gock.OffAll()

	gock.New(mockerServer).
		Post("/client/register").
		AddMatcher(clientDataMatcher()).
		Reply(200)

	client, err := NewClient(
		WithUrl(mockerServer),
		WithMetricsInterval(50*time.Millisecond),
		WithAppName(mockAppName),
		WithInstanceId(mockInstanceId),
	)

	assert.Nil(err, "Client should open without a problem")

	time.Sleep(100 * time.Millisecond)

	err = client.Close()

	assert.Nil(err, "Client should close without errors")

	st.Expect(t, gock.IsDone(), true)
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. I added your suggestion

Copy link
Member Author

Choose a reason for hiding this comment

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

And removed started from the options

@chriswk
Copy link
Member Author

chriswk commented Jul 29, 2024

The code looks cool but what happened to specVersion?

Don't you come here with your facts 🥇 . Added specVersion...

Copy link
Member

@sighphyre sighphyre left a comment

Choose a reason for hiding this comment

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

Noice! LGTM

metrics.go Outdated
started := time.Now()
if options.started != nil {
started = *options.started
}
Copy link
Member

Choose a reason for hiding this comment

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

I believe this can be reverted now, and we can set started: time.Now() like before.

}
}
return true
}
Copy link
Member

Choose a reason for hiding this comment

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

These are helper functions, not tests. Maybe we should move them to the top of the file or somewhere else that's not here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Why not here, they're used immediately after?

Copy link
Member

@nunogois nunogois Jul 29, 2024

Choose a reason for hiding this comment

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

Simply readability / convention / preference. I don't have strong feelings either way, and I understand the logic of keeping them near to where they're used, so I'm happy with keeping this.

Copy link
Member

@nunogois nunogois left a comment

Choose a reason for hiding this comment

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

LGTM - Thank you for considering my comments 😄

@chriswk chriswk merged commit 8024135 into v4 Jul 29, 2024
4 of 8 checks passed
@chriswk chriswk deleted the task/newMetricsMetadata branch July 29, 2024 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants