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

Why can I not get the correct value of some custom-defined Tag? #66

Open
leee66 opened this issue Apr 13, 2017 · 3 comments
Open

Why can I not get the correct value of some custom-defined Tag? #66

leee66 opened this issue Apr 13, 2017 · 3 comments

Comments

@leee66
Copy link

leee66 commented Apr 13, 2017

Hi,
I custom-defined 5 tags—named as AppID, ServerIP, ProcessName, PageName and FunctionName respectively, and then I passed them to the method named as Metric.Histogram(). My code is as follows.
In the file name as MetricsHelper.cs:
static MetricsHelper()
{
string uri = ConfigurationManager.AppSettings["Metrics.DBUri"];
string user = ConfigurationManager.AppSettings["Metrics.UserName"];
string pass = ConfigurationManager.AppSettings["Metrics.Password"];
string database = ConfigurationManager.AppSettings["Metrics.Database"];

        Metric.Config
            .WithHttpEndpoint("http://localhost:1234/metrics/")
            .WithReporting(               
                i => i.WithInfluxDb(uri, user, pass, database, TimeSpan.FromSeconds(1))
                );
    }

private static Histogram Histogram(string metricsName, Unit unit)
{
return Histogram(metricsName, unit, null);
}

private static Histogram Histogram(string metricsName, Unit unit, params string[] tags)
{
string appIDTag = string.Format("AppID={0}", appID);
string serverIPTag = string.Format("ServerIP={0}", environment["IPAddress"]);
string processNameTag = string.Format("ProcessName={0}", environment["ProcessName"]);

        string[] metricsTagList = new[] { appIDTag, serverIPTag, processNameTag };
        if (tags != null && tags.Any())
        {
            metricsTagList = metricsTagList.Concat(tags.ToList()).ToArray();
        }          
        MetricTags metricsTags = new MetricTags(metricsTagList);

        return Metric.Histogram(metricsName, unit, tags: metricsTags);
    }

    private static readonly Dictionary<string, string> environment = AppEnvironment.Current.ToDictionary(e => e.Name, e => e.Value);
    private static readonly string appID = ConfigurationManager.AppSettings["AppID"];

public static Histogram SearchFlightTime(params string[] tags)
{
return Histogram("MetricsDemo.SearchFlightTime", Unit.Custom("ms"), tags);
}

In the file named as Program.cs:
static void SearchFlight1()
{
Stopwatch stopwatch = Stopwatch.StartNew();
Random random = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
var n = random.Next(1000);
Thread.Sleep(n);

        stopwatch.Stop();
        
        // Statistic the execute time  
        //Environment.SetEnvironmentVariable("Metrics.GlobalContextName", "MetricsDemo.SearchFlight1");
        string[] tags = new[] { string.Format("PageName={0}", "Flight1"), string.Format("FunctionName={0}", "GetFlight1Info") };            
        MetricsHelper.SearchFlightTime(tags).Update(stopwatch.ElapsedMilliseconds);
    }

static void SearchFlight2()
{
Stopwatch stopwatch = Stopwatch.StartNew();
Random random = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
var n = random.Next(1000);
Thread.Sleep(n);

        stopwatch.Stop();

        // Statistic the execute time  
        //Environment.SetEnvironmentVariable("Metrics.GlobalContextName", "MetricsDemo.SearchFlight2");
        string[] tags = new[] { string.Format("PageName={0}", "Flight2"), string.Format("FunctionName={0}", "GetFlight2Info") };           
        MetricsHelper.SearchFlightTime(tags).Update(stopwatch.ElapsedMilliseconds);
    }

static void Main()
{
SearchFlight1();

        int i = 1;
        while (i < 4)
        {
            SearchFlight2();
            ++i;
        }
    }

I executed the method SearchFlight1() only one time, and then executed the method SearchFlight2() three times. However, all the metric data is added to the tag PageName value Flight1 and the tag FunctionName value GetFlight1Info. Please see the following screenshot.
22

I think that the correct result is as follows.
Total Count--1, PageName--Flight1, FunctionName--GetFlight1Info;
Total Count--3, PageName--Flight2, FunctionName--GetFlight2Info.

Why can I not get the correct value of some custom-defined Tag? And how to modify my code so that I can get the correct result?

Look forward to your soon reply.

Thanks

@PaulParau
Copy link
Member

Hello,

First of all, your two methods, SearchFlight1 and SearchFlight2 both ultimately end up calling Metric.Histogram with the same name - "MetricsDemo.SearchFlightTime". What Metric.Histogram does, is if a histogram by that name does not exist, then it creates it with the specified parameters, otherwise it just returns the existing histogram (basically a Get or Add behavior). So when you call SearchFlight1, the histogram gets created with the specified tags, and when you cal SearchFlight2, the histogram previously created is simply returned and the new tags are ignored.

Secondly, the tags are simply metric-level metadata which can help with the organization of metrics, they do not represent sub-metrics of any kind.

Maybe you could use two different histograms to track the two methods?

@leee66
Copy link
Author

leee66 commented Apr 18, 2017

@PaulParau
If I use two different histograms to track the two methods, the two metric keys will be produced ------ one is named as MetricsDemo.SearchFlightTimeGetFlight1Info, and another is named as MetricsDemo.SearchFlightTimeGetFlight2Info. If I want to produce only one metric key ------- MetricsDemo.SearchFlightTime and then distinguish via WHERE clause of InfluxDB to track the two methods, is there any other way to implement this requirement? (I have such a requirement in my mind, as I think that only one metrics key is better than more than one metrics key.)

Thank you again.

@PaulParau
Copy link
Member

As far as I know there is currently no way of tracking two different methods with the same metric instance.

Regarding metric name/keys, you can have two different metric instances with the same name/key, but they have to be in different contexts.

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

No branches or pull requests

2 participants