Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

add metric producer manager. #1039

Merged
merged 6 commits into from
Mar 4, 2019
Merged

Conversation

rghetia
Copy link
Contributor

@rghetia rghetia commented Feb 28, 2019

No description provided.


type manager struct {
mu sync.RWMutex
producers []Producer
Copy link
Contributor

@songy23 songy23 Feb 28, 2019

Choose a reason for hiding this comment

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

Consider using a map[string]Producer map[Producer]struct{}.

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 had considered that before but map cannot be unmodifiable. However slices are unmodifiable implicitly.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can implement your own immutable map/slice (also not sure that slices are immutable) by cloning the current map/slice and apply the mutation operation on the clone then swap the pointers to the map/slice.

return pm.producers
}

func (pm *manager) contains(producer Producer) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: if contains is only used in add, maybe it's not worth adding a separate method; otherwise contains should be guarded with mu.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed contains.

// used by exporter to read metrics from producers.
func GetAll() []Producer{
pm := getManager()
return pm.producers
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider returning a copy of producer slice, otherwise this may introduce synchronization issue. In Java we always return an unmodifiable snapshot.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

slices are unmodifiable implicitly. I have added a test for it.

Copy link
Contributor

Choose a reason for hiding this comment

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

That is not true:
I think this is a good article to read https://medium.com/@nitishmalhotra/uh-ohs-in-go-slice-of-pointers-c0a30669feee

Copy link
Contributor

Choose a reason for hiding this comment

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

I gave you the wrong link. The idea is that Slice is a struct {len, cap, pointerToData}. If passed by value all of these will get copied but the underlying memory where pointerToData points is shared between multiple copies of the slice.

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 got it. It is NOT immutable. I verified by a different test.

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 will change the implementation.

Copy link
Contributor

@songy23 songy23 left a comment

Choose a reason for hiding this comment

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

LGTM

@rghetia
Copy link
Contributor Author

rghetia commented Mar 1, 2019

@bogdandrutu and @songy23 PTAL again.

Copy link
Contributor

@songy23 songy23 left a comment

Choose a reason for hiding this comment

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

LGTM

return prodMgr
}

// Add adds the producer to the manager if it is not already present.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe define what is the "Manager"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

func (pm *manager) getAll() []Producer {
pm.mu.Lock()
defer pm.mu.Unlock()
producers := make([]Producer, len(pm.producers))
Copy link
Contributor

Choose a reason for hiding this comment

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

producers := make([]Producer, len(pm.producers))
copy(producers, pm.producers)

https://github.com/golang/go/wiki/SliceTricks#copy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

producers is slice while pm.producers is a map. So copy won't work.

@@ -19,6 +19,7 @@ import (
)

// Producer is a source of metrics.
// Deprecated in lieu of go.opencensus.io.metric.producer.Producer
Copy link
Contributor

Choose a reason for hiding this comment

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

Simply delete it, we don't use this anywhere correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

deleted.

@bogdandrutu
Copy link
Contributor

@rghetia ping me when this is ready for review.

@rghetia
Copy link
Contributor Author

rghetia commented Mar 3, 2019

@rghetia ping me when this is ready for review.

It is ready.

// Add adds the producer to the manager if it is not already present.
// The manager maintains the list of active producers. It provides
// this list to a reader to read metrics from each producer and then export.
func Add(producer Producer) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would like to have these function on a "ProducerManager" struct and have a "GetGlobalProducerManager". This way the exporters will get a "ProducerManager" as an option, this is a bit more flexible. We can do that in a separate PR just to make sure we make progress, but I think is a better design.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure.

@rghetia rghetia merged commit f305e5c into census-instrumentation:master Mar 4, 2019
@odeke-em
Copy link
Member

odeke-em commented Mar 4, 2019

Hey folks, thanks for the work on this @rghetia! However, I am a little lost on what these interfaces are for? Including #1045 this seems very Java-esque. What's going on?

@songy23
Copy link
Contributor

songy23 commented Mar 4, 2019

@odeke-em This is for the OpenCensus-Go main library to produce Metrics. Before we produce ViewData and ViewData is going to be replaced by Metrics.

@rghetia rghetia deleted the producer branch April 15, 2019 20:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants