Exemple #1
0
// ZeroValue works like types.Type.SafeZeroValue, but unlike the types.Type
// version, this method can also return zero values for types in this
// package.
func ZeroValue(t types.Type) (interface{}, error) {
	if t == types.Dist {
		return (*Distribution)(nil), nil
	} else {
		return t.SafeZeroValue()
	}
}
Exemple #2
0
func valueToTextString(
	value reflect.Value,
	t types.Type,
	u units.Unit,
	isValueAPointer bool) string {
	switch {
	case t == types.Bool:
		if value.Bool() {
			return "true"
		}
		return "false"
	case t.IsInt() && !isValueAPointer:
		return strconv.FormatInt(value.Int(), 10)
	case t.IsUint() && !isValueAPointer:
		return strconv.FormatUint(value.Uint(), 10)
	case t == types.Float32 && !isValueAPointer:
		return strconv.FormatFloat(value.Float(), 'f', -1, 32)
	case t == types.Float64 && !isValueAPointer:
		return strconv.FormatFloat(value.Float(), 'f', -1, 64)
	case t == types.String && !isValueAPointer:
		return "\"" + value.String() + "\""
	case t == types.GoTime:
		return timeToDuration(
			valueToTime(
				value, isValueAPointer)).StringUsingUnits(u)
	case t == types.GoDuration && !isValueAPointer:
		return duration.New(
			valueToGoDuration(value)).StringUsingUnits(u)
	default:
		panic(panicIncompatibleTypes)
	}
}
Exemple #3
0
// Register returns the correct MetricInfo instance from the pool for
// passed in metric and type. Register will always return a non nil value.
func (m *metricInfoStoreType) Register(
	metric *metrics.Value, kind, subType types.Type) (
	result *MetricInfo) {
	if kind == types.Unknown {
		panic("Got Unknown type")
	}
	if kind.UsesSubType() && subType == types.Unknown {
		panic("Got unknown sub-type when it is required")
	}
	var ranges *Ranges
	var isNotCumulative bool
	if kind == types.Dist {
		// TODO: Maybe later this shouldn't be tricorder specific,
		// but for now tricorder is the only thing we know that
		// has distributions.
		distribution := metric.Value.(*messages.Distribution)
		isNotCumulative = distribution.IsNotCumulative
		upperLimitSlice := distExtractUpperLimits(distribution)
		ranges = m.rangesCache.Get(metric.Path, upperLimitSlice)
	}
	infoStruct := MetricInfo{
		path:            metric.Path,
		description:     metric.Description,
		unit:            metric.Unit,
		kind:            kind,
		subType:         subType,
		ranges:          ranges,
		isNotCumulative: isNotCumulative,
		groupId:         metric.GroupId}
	result, alreadyExists := m.ByInfo[infoStruct]
	if alreadyExists {
		return
	}
	result = &infoStruct
	m.ByInfo[infoStruct] = result
	m.ByName[infoStruct.path] = append(m.ByName[infoStruct.path], result)
	return
}
Exemple #4
0
func valueToHtmlString(
	value reflect.Value,
	t types.Type,
	u units.Unit,
	isValueAPointer bool) string {
	switch {
	case t.IsInt() && !isValueAPointer:
		switch u {
		case units.Byte:
			return iCompactForm(
				value.Int(), 1024, byteSuffixes)
		case units.BytePerSecond:
			return iCompactForm(
				value.Int(), 1024, bytePerSecondSuffixes)
		default:
			return iCompactForm(
				value.Int(), 1000, suffixes)
		}
	case t.IsUint() && !isValueAPointer:
		switch u {
		case units.Byte:
			return uCompactForm(
				value.Uint(), 1024, byteSuffixes)
		case units.BytePerSecond:
			return uCompactForm(
				value.Uint(), 1024, bytePerSecondSuffixes)
		default:
			return uCompactForm(
				value.Uint(), 1000, suffixes)
		}
	case t == types.GoDuration && !isValueAPointer:
		d := duration.New(valueToGoDuration(value))
		if d.IsNegative() {
			return valueToTextString(
				value, t, u, isValueAPointer)
		}
		return d.PrettyFormat()
	case t == types.GoTime:
		t := valueToTime(value, isValueAPointer).UTC()
		return t.Format("2006-01-02T15:04:05.999999999Z")
	default:
		return valueToTextString(value, t, u, isValueAPointer)
	}
}