func extract()

in collector/otlp/logs_transfer.go [266:322]


func extract(val *commonv1.AnyValue, depth int, maxdepth int) (value any, ok bool) {
	if val == nil {
		return nil, false
	}

	if depth > maxdepth {
		return "...", true // Just cut off here.
	}

	switch val.Value.(type) {
	case *commonv1.AnyValue_StringValue:
		return val.GetStringValue(), true
	case *commonv1.AnyValue_BoolValue:
		return val.GetBoolValue(), true
	case *commonv1.AnyValue_IntValue:
		return val.GetIntValue(), true
	case *commonv1.AnyValue_DoubleValue:
		return val.GetDoubleValue(), true
	case *commonv1.AnyValue_BytesValue:
		return val.GetBytesValue(), true
	case *commonv1.AnyValue_ArrayValue:
		arrayValue := val.GetArrayValue()
		if arrayValue == nil {
			return nil, false
		}

		ret := make([]any, 0, len(arrayValue.Values))
		for _, v := range arrayValue.Values {
			vv, ok := extract(v, depth+1, maxdepth)
			if !ok {
				continue
			}
			ret = append(ret, vv)
		}
		return ret, true
	case *commonv1.AnyValue_KvlistValue:
		kvList := val.GetKvlistValue()
		if kvList == nil {
			return nil, false
		}

		ret := map[string]any{}
		for _, kv := range kvList.Values {
			if kv == nil || !kv.HasValue() {
				continue
			}
			vv, ok := extract(kv.Value, depth+1, maxdepth)
			if !ok {
				continue
			}
			ret[kv.Key] = vv
		}
		return ret, true
	default:
		return nil, false
	}
}